Saturday, 5 May 2007

Lesson Learnt

Before I endeavoured into this journey of learning about design patterns, I thought that design pattern is something which is associated with software design only but during the course of this expedition, I came across altogether a different perspective about design pattern. Unknowingly, that even I follow patterns. When we talk about computing terms we think that it’s just to do with computers but patterns is something followed in daily life as well. We all have sleeping pattern, travelling patterns, studying patterns etc.

The more I learnt about patterns, the more I came across stuff that I was already aware of and was following in programming one way or the other. For example; strategy pattern, which I was following in websites not realizing that I was following a pattern. Another example I can think of is of Adapter pattern; In Java programming I have used database connection, now Java can not directly talk to database, it has to go through ODBC connection, so in my opinion, that particular code we use to connect to database acts like an Adapter pattern.

Our lecturer, Dr. Sri was very cooperative through out the course, he answered my questions or should I say my stupid questions as my friends refer to them, helped me in understanding the concepts of pattern. The good thing about him is his real world analogies, he picks up examples from real life which everyone is familiar with for example; he gave an example of coffee maker for explaining Abstract factory, that helps really helps students those have very basic background knowledge. I would like to acknowledge the efforts of our lecturer who helped us in every aspect and guided us throughout the unit.

Sometimes I was indolent and couldn’t give the required time to this unit but the experience of studying patterns was worthy. At the end I would like to say that I have learnt quite a few things from this unit and it was exciting 12 weeks.

Saturday, 28 April 2007

Strategy Design pattern

In today's class, we discussed one of the most important design pattern called strategy design pattern. Certainly there are some patterns that are more useful in programming terms compared to other patterns.
Strategy design pattern is whereby different algorithms can be selected on-the-fly at run time [1].

Difficult to understand, I know, well to put it in simple words strategy pattern, creates different algorithms used in an application, encapsulate them and make them interchangeable. Each algorithm is handled as a separate object. Strategy pattern lets the algorithm vary independent from the client.

Perhaps an example would make it more simpler; I would like to quote an example which all of us have used unknowingly that we are following strategy design pattern; [2]
All of us have worked with websites and created forms in it. While making a registration page or contact us page we usually put different functions in order to validate the input from the user and based on that action is taken. [2]

Now putting this example is design pattern language; first we select a context where corresponding form validation is going to take place and then we apply a data checking strategy to establish pragmatically how different type of entries from users will be verified. This is indeed how strategy design pattern is implemented. [2]

In strategy pattern there is a predefined context that decides what course of action should be taken in accordance with the requirement of a specific strategy. [2]

There are several advantages of using strategy pattern [3];
  • If there is an object that is required to perform several behaviors, by using strategy pattern it becomes much simpler to keep track of each behaviour if it is dealt as a separate class. Adding more responsibilities to that class later on becomes much easier.
  • Changing behavior of object becomes easy as we just need to change the strategy followed by object.
  • Eliminates the need of having several conditional statements.
  • Number of classes are available and they differ only in their behaviour, then strategy pattern creates only one class and it uses different strategies, thus it results in easily manageable class.

References

[1] Strategy Design pattern
Available from URL
http://en.wikipedia.org/wiki/Strategy_pattern

[2] Alejandro Gervasio (2007) Introduction to Strategy Pattern
Available from URL
http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/

[3] Antonio Garcia, The Strategy Design pattern
Available from URL
http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm

Saturday, 14 April 2007

Anti Patterns (Week - 7)

The topic under discussion in today's class was Anti-Patterns. Anti-Patterns are basically problems that come along with adaptation of a certain pattern, for example; if we are using adapter pattern to find a solution for a reoccurring problem, now while using adapter a certain type of problem arises every time , that problem is called Anti-pattern.
We can also call Anti-patterns as shortcomings or pitfalls of pattern.

Anti-pattern= (Problem +Ant-pattern Solution+Re factory Method) in a context

Anti-pattern Solution = Finding the root cause of that reoccurring problem
Re factory Method = Getting rid of this Anti-patterns

I am not going into the details of Anti-patterns; i wanted to talk about the importance of Anti-patterns, root causes, how they are discovered and different types of Anti-patterns but since this task is in coursework, so it would be better to explain my viewpoints there and avoid any kind of plagiarism.

Thursday, 29 March 2007

Decorator Pattern (Week -6)

In today's lecture we diverted our attention to structural pattern (Decorator). In this blog, i would explain decorator from my understanding.

Decorator patter is also known as wrapper, the purpose of using this pattern is to attach additional responsibilities to an object dynamically. Sometimes user would want to attach additional responsibilities to an object, there are two ways to go by;
  • this first way to add responsibilities to an object is via inheritance
  • the second way is to use Decorator pattern

Inheritance would add up responsibilities to every subclass instances, another problem with inheritance is; the choice of border would is made statically rather than dynamic, which is our purpose. [1]

Using decorator pattern would be more feasible as it adapts a flexible approach, decorator encloses the component in another object, the enclosing component is called decorator. [1]Decorator pattern adds responsibilities to an object at run time while sub classing adds responsibilities at compile time. [2]

This pattern is used when;

  • user wants to add responsibilities to an object dynamically
  • when there is a need of withdrawing responsibilities

References

[1] Design Patterns by GoF
Decorator Pattern
Page - 175 - 184

[2] Decorator Pattern

Available from URL
http://en.wikipedia.org/wiki/Decorator_pattern

Saturday, 24 March 2007

Adapter Pattern (Week - 5)

Today's lecture was about the first structural pattern called Adapter pattern. The lecture was divided into two sessions; first session was about introduction to Adapter pattern and second session was about some practical examples of Adapter pattern.

From my understanding of the pattern I think; the basic purpose of adapter pattern is to make two incompatible interfaces compatible. It makes different classes to work together even if they are not compatible with each other. This pattern is also known as wrapper as it wraps itself around the class interface in order to convert into another interface which client expects. The adapter is also responsible for handling any logic necessary to transform data into a form that is useful for the consumer. For instance, if multiple Boolean values are stored as a single integer but your consumer requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. [1]

A very good example was given by Dr. Sri; consider a laptop being bought from another country and is brought in UK, now that laptop has two pin plug and works with two pin socket. We have to use an adapter in order to make it compatible with three pin socket that are used in Britain.

This pattern is used when;

  • User wants to use existing class but the interface doesn't match user needs
  • Creating a class that communicates with unrelated classes

There are two types of this pattern; Object Adapter pattern and Class Adapter pattern

Object Adapter Pattern

In this pattern the adapter hold the instance of the class it wraps and makes call to the instance of the wrapped object.

Class Adapter Pattern

The class adapter pattern uses the concept of multiple inheritance. The adapter is created inheriting interfaces from both the interface that is expected and the interface that is pre-existing. [1]

The object adapter pattern is more often used than class adapter pattern. Class adapter pattern can not be used with Java because of the concept of multiple inheritance, that is not supported by Java.

Reference

[1] Adapter Pattern
Available from URL
http://en.wikipedia.org/wiki/Adapter_pattern

Saturday, 17 March 2007

PAFSD Coursework (Week -4)

Today in the morning, Ma'am Jane Chandler came from university of Portsmouth, she spoke about different aspects of software design patterns and gave some examples of real life patterns, also she gave the coursework for our batch.

In my opinion, the coursework has enough time span to be completed and it seems okay, not very easy and not very difficult either.

In Task-1, we have to design a small prototype which creates, stores and updates customer records, then we have to make some UML diagrams using three patterns. Only thing that is worrying in this task is to evaluate the effectiveness of each pattern.

Task-2, we have to come up with the code of above UML diagram, I am going to skip this task.

Task-3, seems to be easy, as we have to complete a pattern draft, the thing that worries me is choosing the pattern preserving technique, because if i chose GoF (which I intend to), i have to drill down deep into any particular Temporal pattern.

Task-4 is a bit difficult task as we have to come up with a really good essay on Anti patterns, and I have no clue about what they are.

Task-5 (my favourite one) is the easiest of them all, that is we have to make a blog (which i m currently doing) about our understanding of the design patterns. We have to explain every lecture of our class in our own words where by we have to explain, what we understood from today's lecture.

Abstract Factory (Week - 4)

In today's session, we studied about first design pattern of GoF Design Pattern catalogue i.e. Abstract Factory.
From my understanding of the concept, Abstract factory creates interfaces for different objects that are related to each other or depend on each other, such that there is no specific concrete class. In simple words, it encapsulates a group of individual factories having same essence.

The client uses same interface to generate objects that are part of it, the client does not know about which concrete objects it gets its requests served because of generic interfaces. This patterns separates the details of implementation of set of objects from clients.

This pattern is used when;
  • When a system is independent of how it is implemented
  • system needs to configure with one of multiple families of products

Uses of using this pattern are;

  • Isolation of concrete classes; they becoming independent of how the system is implemented from client side
  • exchanging of different families of products because easy because they all belong to same set

Perhaps an analogy of abstract factory would make it easy to understand; Taking example of Electric hair cutting machine, it has different grades. Short, Medium and large. The amount of hair person would like to cut depends on the grade being used. For example; if you want short hairs, you might use grade 1, if you want medium then maybe grade 3.

Each different grade acts like a factory and the machine acts like a class. As soon we plug the grade in to the machine then grade inherits the properties of abstract (machine). The machine doesn't care about which grade you are using, its sole purpose is to cut hairs, only the grade would decide the amount of hairs its going to cut.