Thursday 3 May 2007

Anti-Patterns

In layman term, anti-patterns are pitfalls. Solutions which are needed to be created again and again to overcome a problem.

I would be going into detail of anti-patterns in my course work as to how anti-patterns have evolved and how they have benefited the software industry.


Goodday.

Decorator Design Pattern

A decorator pattern allows additional responsibilities added to the object without subclassing. Subclassing means a class that inherits some properties from the superclass and decorator pattern is alternative to subclassing.[1]

Why is decorator design pattern alternative to subclassing or inheritance?
  1. Because subclassing adds behaviour during compilation and decorator pattern adds new responsibility or behaviour during run time.
  2. It's done without modifying the interface of the original object.
  3. Decorator pattern allows to add or remove functionality as and when required. [2]

Advantages of using Decorator Design Pattern over Subclassing are:

  1. Because adding functionalities are done during run time therefore it gives more flexibility than just straight inheritance.
  2. It saves time and simplifies coding because you need not write coding from the start and just code the behaviour into the object.
  3. Unnessary functionalites need not be added. It can be done upon request.
  4. In any case when you do not have access to the source code but you need to add the functionalities to the product, then you can wrap this object into the decorator object to provide the missing or new functionalities.[3]

---------------------------------------------------------------------------------------------------

REFERENCE

[1], [2] & [3] http://en.wikipedia.org/wiki/Decorator_pattern

Strategy Design Pattern

I missed this lecture as I was attending hospital appoinment. Information which I have gathered from lecturer's notes and a bit of self stuyding did however allow me to outline my understanding as below:-

Strategy Pattern is basically used to achieve different behaviours. In simple terms, an object and its behaviour are separated into two different classes. Each behaviour encapsulated into its own class and it's called Strategy.[1]

What is the advantage of using Strategy Pattern?

  1. It is much simpler to keep track of different behaviours required from an object as separate classes will be created.
  2. Any amendements need to be made on the behaviour of an object can be achieved quickly as you will keep track of those classes.
  3. In some cases, you will come across of several objects that are similar but different in behaviour. Therefore, using Strategy Pattern becomes very useful.
  4. It is much easier to make an extension on the object with new behaviours without recoding the application.
  5. Using this patterns also means eliminating the need of many conditional statements.

Besides the advantages, I think one must know also know the different strategies available in order to reduce creating of unnessary startegies. If the knowledge is lacking than one might end up creating too many classes which can create problems of maintaining them.

Is Strategy Design Pattern the best to be used?

Well, I think that every pattern has it own uniqueness and is designed to address a specific issue.

---------------------------------------------------------------------------------------------------

Reference - Web Link

[1] http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/StrategyPattern.htm