Codementor Events

Separation of Concerns In Practical

Published Sep 04, 2017Last updated Sep 05, 2017
Separation of Concerns In Practical

A key principle of software development and architecture is the notion of separation of Separation of Concerns. At a low level, this principle is closely related to the Single Responsibility Principle of object oriented programming. The general idea is that one should avoid co-locating different concerns within the design or code.

For instance, if your application includes business logic for identifying certain noteworthy items to display to the user, and your application formats such items in a certain way to make them more noticeable, it would violate separation of concerns if both the logic for determining which items were noteworthy and the formatting of these items were in the same place. The design would be more maintainable, less tightly coupled, and less likely to violate the Don’t Repeat Yourself principle if the logic for determining which items needed formatted were located in a single location (with other business logic), and were exposed to the user interface code responsible for formatting simply as a property.

Separation of Concerns at Use

Validation of Value Objects (a Value Object is an immutable type that is distinguishable only by the state of its properties) should not take place in their constructor. Constructors as a rule should not include logic, but should simply assign values. If validation is required, it should be moved to a factory method, and indeed it is a common pattern to make Value Objects’ constructors private, and provide one or more public static methods for creating the Value Object. This achieves separation of concerns, since constructing an instance from a set of values is a separate concern from ensuring the values are valid.

public class MailSubjectValueObject implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String NULL_CHARACTER = "\u0000";

    @NotNull
    private String subject;

  //Private Constructor
    private MailSubjectValueObject(
            String subject) throws InvalidMailSubjectException {
        super();
        this.subject = 		  
        RemoveEvilCharactersFromString.removeNullCharacterFromString(subject);
        this.validate();
    }

  //Factory Method for Separation of Concers, here being filtering null
    //characters from the input string
    public static MailSubjectValueObject filterString(
            String subject) throws InvalidMailSubjectException {
        return new MailSubjectValueObject(subject);
    }

    public String getSubject() {
        return subject;
    }

    private void validate() throws InvalidMailSubjectException {

        Validator validator = 
        Validation.buildDefaultValidatorFactory().getValidator();
        if (!validator.validate(this).isEmpty()) {
            throw new InvalidMailSubjectException();
        }

        if (subject.contains(NULL_CHARACTER)) {
            throw new InvalidMailSubjectException();
        }
    }
}
Discover and read more posts from Sushant Saurabh
get started
post commentsBe the first to share your opinion
Show more replies