November 14th, 2009

Simple and clear code – what does it look like? Basically, besides comprehensible clear naming we expect it to be highly granular and to have as little as possible branching. Indeed, such code is easy to read, to understand its purpose and required test coverage is optimal.

Well, clear naming has nothing special to talk about. High granularity is all about code decomposition (The Single Responsibility principle can also be applied here).

One of frequent reasons for the code branching is a ramification of logic depending on some object’s internal state (normally, it is a domain class object). Such branching can be “switch”-style – when you compare a particular state variable with its possible values. However, a more common and basic is the binary branching, which operates on boolean conditions. E.g., if object is in this state then do this else do that.

Often there is a need to repeat such logic in many places throughout the application code. Additionally, such logic may happen to be more intricate than one conditional if statement. To simplify it one may define appropriate isXXX() method(s) to the examined object’s class. But what if the object state is fairly complex or domain class is not aware of its possible states, or the logic examining its state is not limited to several cases, but more cases can be developed in future. Or even, you can be trapped in a situation, when you are not allowed to change the domain object class.

So here is an elegant solution… it’s the Matcher pattern! This is a pretty simple design pattern that consists of three classes in general case. The pattern’s class diagram is following:

Matcher UML Diagram

There are the Entity class which is the specific domain class, the Matcher which is an abstract class or interface with a single abstract method that represents the matching algorithm, and the ConcreteMatcher which is a specific implementation of Matcher.

When implementing the pattern, Java generics can be leveraged to provide more flexibility with less code.

public interface Matcher {
    boolean matches(T object);
}
 
public class EuropeanCountryMatcher implements Matcher<Country> {
    // the list of EU countries – can be injected
    private Set countryCodes;
 
    // Checks if the given country is a EU currency
    public boolean matches(Country country) {
        return (countryCodes.contains(country.getCode()));
    }
}

What is even more beautiful about the Matcher is that it can be injected into classes that rely on its logic. E.g.

public class DeliveryStrategy {
    private Matcher<Country> specialPatnerCountryMatcher;
 
    public void setSpecialPatnerCountryMatcher(Matcher<Country> matcher) {
        specialPatnerCountryMatcher = matcher;
    }
 
    public Priority getDeliveryPriorityForClient(Customer customer) {
        if (specialPatnerCountryMatcher.matches(customer.getAddress().getCountry())) {
            return Priority.HIGHEST;
        } else {
            return Priority.STANDARD;
        }
    }
}

Besides logic reuse and simplification, another nice use of Matcher is collection filtering.

I believe, similar “Matcher” approaches have been used in many projects independently. One described here can have pretty big number of various applications, so is worthy of being stated as a design pattern.

November 8th, 2009

Business requirements define functionality of a system, whereas system architecture allows to implement this functionality. The more sophisticated business requirements are, the more flexible architecture is required. The more flexible architecture is, the more future business needs it may cover. The more specific selected architecture is, in a better way it serves current business needs. The more focused on current business requirements architecture is, the less time to market is achieved. So there should a compromise be found. I would say, fifty-fifty approach will fit most of small to medium projects, while more flexibility to less dedicated solution will fit big projects or projects that will potentially grow in future. Either extreme will go phut eventually and end up with totally new solution. There is nothing new about it, I just wanted to emphasize the importance of such choice, no matter how philosophic the question is.

November 8th, 2009

Design Patterns are a great piece of knowledge, that gives a possibility to learn how to create a good design instead of researching it. From the other side, agile development practices is another mainstream that for ones means flexible though stable organizational methodology and for others a way to save some money by reducing time (in reality by reducing paper work etc. while going directly into the development). To me these two mainstreams are highly coupled. The later makes great use of the former, and from the other side represents a set of organizational patterns itself. So I will talk about patterns in general having in mind agile practices and design patterns as pat of it. Patterns are great because you gain a scheme for your solution and do not need to invent a wheel from scratch. Generally saying, patterns are considered to be “best practices”, and likewise they are.

For these and other similar reasons the design patterns and agile methodologies are “mainstream” now, and that’s why are severely required to follow in many organizations (perhaps, you have heard something like “this is not compliant with this or that design pattern or best practice, so is wrong!”). However, while thinking of patterns against real life, you may find that they are merely common recommendations and do not provide “every hole fitting” solutions – ’cause simply it’s (almost) impossible – and then you spent long time searching for a pattern that fits your problem the best or how to apply that given pattern to it. There are so many general design patterns, so that in some cases they overlap and provide confusion. In the end, we’ve got lots of anti-pattens. Doesn’t it sound a bit of non-sense?

Thinking of pattens further, I find them restricting a developer’s fantasy (one, who is able to think and generate his own ideas). Moreover, patterns are simply a set of terms or names for widely known notions which were (and are being!) forged in many organizations. With time new patterns appear, evolve, replace older ones, and it is normal process since new problems to be solved appear. Patterns are being “invented” and shouted out and sold (indirectly).

Concluding, in my opinion, the design patterns or principles should not be considered as a rigorous tool and used blindly, though do not forget that patterns are your basic and fast knowledge and don’t afraid improve them… so sometimes you will have to make a choice: to drag in a mainstream (for the “it’s cool!” sake), or start your own one!

October 20th, 2009

Hello World!

Although it does not mean I’m inviting the whole world (maybe just in my dreams I see visits counter raising to thousands per day ;) ), but for sure the part of it, that can be interested in the content of this blog.

This blog is going to be about software creation, mainly with Java, and different aspects of it, however, design and development will make up the most. It will cover different kinds of software from mobile and desktop to enterprise systems. I will be sharing my experience, thoughts and opinions, that have right to be subjective, however, I will try to keep them first of all useful and interesting.

Welcome!

TOP