Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6735655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:02:18+00:00 2026-05-26T11:02:18+00:00

So I got the Address class: class Address { private String streetAddress; private int

  • 0

So I got the Address class:

class Address 
{
    private String streetAddress;
    private int number;
    private String postalCode;
    private City city;
    private State state;
    private Country country;
}

And I want to get its readable version to, lets say, show in a grid column.

Whats the best and concise way to implement this?

  1. toString method inside class Address (I personally don’t like this approach, as ‘toString’ is not directly related to an Address)
  2. class ReadableAddressFormatter
    • ReadableAddressFormatter(Address addressToFormat)
    • public String getFormatted()
  3. Previous class but getFormmated would be static, receiving the Address instance and returning the string
  4. Other? Suggestions please.

I’m looking for a good design, focusing also in Clean Code, Decoupling and Maintainability.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:02:18+00:00Added an answer on May 26, 2026 at 11:02 am

    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:

    1. 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.
    2. 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?
    3. 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.

    Hope this helps, and kudos for thinking about Clean Code, Decoupling, and Maintainability from the beginning.

    For an example of principle #2 in action–using the Strategy Pattern, adhering to the Single Responsibility Principle, the Open/Closed Principle and allowing for Inversion of Control via Dependency Injection— compare the following approach (graciously provided by @SteveJ):

    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
            }
        }
    
    }
    

    With this one (in “mostly correct” Groovy):

    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}"
    

    As @SteveJ has observed:

    ” 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.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a field defined like this country = ChoiceField(initial='CA', choices=COUNTRIES, widget=Select(attrs={'class':'address country'})) Notice
I got classes: @Entity @Table(name=users) public class User{ private Integer id; private String name;
I've got my model which contains some members: public class Address { public Street
I have a user defined class, say import java.util.Calendar; public class Employee{ private String
Consider the following ViewModel property: private string _slowProperty; public string SlowProperty { get {
Let's say I've got a class called House with the two fields name address
I've got a property in a class defined as an int. I retrieve the
We've got a test site hosted only by IP address. We really need to
I've got a classification problem in my hand, which I'd like to address with
Got a class that serializes into xml with XMLEncoder nicely with all the variables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.