Tuesday, September 1, 2015

Software Design Patterns

Design pattern is a best practice to write a code. So what makes your code more efficient? I think best usage of memory. Yes memory is a basic thing that developer should take care about. Additionally, based on requirement what kind of approach will enhance the throughput, security and many more are also need to account.

Generally design pattern has relation between classes and related methods. You can say it is a structured approach of software designing. So before writing object oriented programming code, sound knowledge of the design patterns helps you to improve your coding standards. On the other side these design approach doesn't work for functional programming languages, which is not purely object oriented. As example, software design approach with mutable state will not make any sense in functional programming languages for their immutable states.

Index

1)
Factory Design Pattern
2)
Abstract Factory Design Pattern
3)
Observer Design Pattern
4)
Object Pool Design Pattern

1) Factory Design Pattern:

It is a creational design pattern that helps in object creation without exposing details of creation logic. Factory name suggest producing something, here based on input parameter factory class will return specific objects. So we can say that factory design pattern uses to produce objects based on specific input parameters.

Why is it needed?
  • Client program doesn't require to know all details of the class object.
  • Minimize if..else at the client program, thus if any change occurs in the concrete classes later, each client code doesn't need to customize. Simply modification in factory class will solve the issue.
  • Choose class object at run time.
  • If you don't want to let user know about all class objects. In other words to encapsulate object creation.
How does it work?

Example: Game in which based on random number generation, specific type of enemies gets generated.

[Factory pattern UML diagram with example]

Classes involved in example.
  1. Client.java
  2. EnemyShipFactory.java
  3. UFOEnemyShip.java
  4. RocketEnemyShip.java
  5. EnemyShip.java <<interface/abstract class>>

2) Abstract Factory Design Pattern:

Abstract factory pattern is the abstraction on top of the collection of factory patterns. In other words, in previous example of factory method, we focused on only one kind of object (Enemies). Now think a bit beyond that, you are building entire game. So you can have other entities like hero, heroin along with enemy. Having knowledge of factory design pattern you will create 3 factory classes for each. Now think again...... How will you access those? Client will need to know that how many factory class do we have in the system. Here comes the Abstract Factory Design Pattern. That makes it easy for client by adding one more layer on top of those all factory classes. That layer will have one more factory class which will be called as abstract factory class. Each factory class will inherit abstract factory class. Abstract factory class will return specific factory class object based on input parameter given by client.

Why is it needed?
  • You can model anything that you can imagine and have those objects interact through common interface.
  • Allows to access families of related objects. (factory class)
  • Use when you have many objects within a system, that can be added or changed dynamically at the run time.
How does it work?

Example: Go through the same example of the game. And will add additional entities like hero and heroine in system design and try to access all of them with abstract factory design pattern.

3) Observer Design Pattern:

Observer design stands for observing subject object by other dependent objects. In other words in system there exists many inter dependent objects, if we want to update or notify objects on specific modification of dependent object we should use observer design pattern.

Why is it needed?
  • You can avoid the manual updating of the object on specific event on depended object.
  • You can add n number of observers on specific subject object. And it becomes easy to manage them within a single subject class.
How does it work?

In observer design pattern system will have two interfaces, first is for subject and second is for observer. Subject interface has main three methods 1) Register observer 2) Unregister observer 3) Notify observer. Observer interface has one method called update. Any action class implements subject interface. On any action or update event, notify observer method calls. It will iterate through the list of registered observers of the object and will call respective update method. Each observer class will implement observer interface and override update method. And each overridden update method has its own logic to update value as per requirement.

Thus on updation of main subject class object each dependent observer class object gets notified.

Example: If hero kills enemy, it gets additional health and ammo. Each hero user object will have health and ammo objects depended on it. Here on kill_enemy event health and ammo object gets notified.

4) Object Pool Design Pattern:

Object pool design pattern defines to have pool of the objects in system. User can pick the available object from the pool and also can put object back in the pool when it is done with task. Object pool size is predefined and based on it system can generate new objects in the pool.

Why is it needed?

Objects like connection in java are very expensive(in terms of time) to create. You initialize these kind of objects in advance inside a object pool that when it is required you can easily pick it up from the pool and put it back once you are done with the task.

How does it work?

Object pool design pattern has one interface and one base class implementing interface.
  1. ObjectPool <interface>
  2. ObjectPoolClass (singleton)
ObjectPool interface has two methods 1) aquireObject : Object 2) releaseObject : void
ObjectPoolClass is a singleton class that implements ObjectPool interface. It will have main 4 methods. 1) getInstance: ObjectPoolClass 2) CreateObject : Object 3) aquireObject: Object 4) releaseObject: void

Here if your application is multi-threaded then you should take care of all methods by making them synchronized to avoid object access problem at the time of context switching between different threads. You can make getInstance method public static synchronized, createObject method private synchronized, and aquireObject and releaseObject methods public synchronized.

Example: Database connection class in java is a best example for the object pool method. Here ObjectPoolClass will return instance of the object pool having n number of connection objects.