Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All of these methods have been used, and there's no way to offer a "context independent" best practice. The best answer in Software Engineering is usually "it depends." That's said, let's analyze each:</p> <ol> <li>The KISS approach at its finest. I do this for all my basic "print to console, make sure things are working" kind of thing. If you have a specific format you can expect for addresses, this is the low-hanging fruit/easy win solution. You can always override this or print out the object differently in one off situations.</li> <li>This is the most extensible solution, in that it will nicely allow for localization and custom formatting. Whether it is appropriate depends on how often you expect addresses to be shown in different formats. Do you really need that Death Star to knock out a fly, or is the ability to change to all uppercase or change between languages pivotal to your app?</li> <li>I would not suggest this approach, as it generally started to bleed "view level" logic into the Domain, which is usually best handled by other tiers (in a class MVC approach). One could argue that toString() does the same thing, but toString() can also be thought of as the "name" or "essence" of how an object appears to the external world, so I'd say it's more than just presentational.</li> </ol> <p>Hope this helps, and kudos for thinking about Clean Code, Decoupling, and Maintainability from the beginning.</p> <p>For an example of principle #2 in action--using the <a href="http://www.oodesign.com/strategy-pattern.html" rel="nofollow">Strategy Pattern</a>, adhering to the <a href="http://www.objectmentor.com/resources/articles/srp.pdf" rel="nofollow">Single Responsibility Principle</a>, the <a href="http://www.objectmentor.com/resources/articles/ocp.pdf" rel="nofollow">Open/Closed Principle</a> and allowing for <a href="http://martinfowler.com/articles/injection.html" rel="nofollow">Inversion of Control via Dependency Injection</a>-- compare the following approach (graciously provided by @SteveJ):</p> <pre><code>public class Address { private String streetAddress; private int number; private String postalCode; private String city; private String state; private String country; public String toLongFormat(){ return null; // stitch together your long format } public String toShortFormat(){ return null; // stitch together your short format } public String toMailingLabelFormat(){ return null; // stitch together your mailing label format } @Override public String toString(){ return toShortFormat(); // your default format } } } </code></pre> <p>With this one (in "mostly correct" Groovy):</p> <pre><code>public interface AddressFormatter { String format(Address toFormat) } public class LongAddressFormatter implements AddressFormatter { @Override public String format(Address toFormat){ return String.format("%sBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAH%n%s", toFormat.streetAddress, toFormat.postalCode) } } public class ShortAddressFormatter implements AddressFormatter { @Override public String format(Address toFormat){ return String.format("%d", toFormat.number) } } public class Address { private String streetAddress; private int number; private String postalCode; private String city; private String state; private String country; public AddressFormatter formatter = new ShortAddressFormatter(); // just to avoid NPE public void setFormatter(AddressFormatter fr) { this.formatter = fr; } @Override public String toString(){ return formatter.format(this); // your default format } } def addrr = new Address(streetAddress:"1234 fun drive", postalCode:"11223", number:1) addr.setFormatter(new LongAddressFormatter()); println "The address is ${addrr}" addr.setFormatter(new ShortAddressFormatter()); println "The address is ${addrr}" </code></pre> <p>As @SteveJ has observed:</p> <blockquote> <p>" So the you have different formatting "strategies" and you can switch between them...I had this idea that you would set the formatting once and be stuck with it...AND if you want to add another formatting style, you don't have to open up and rewrite the address class, but write a new separate style and inject it when you want to use it."</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload