Monday, March 14, 2011

JAVA Design patterns part-1

Java Design Patterns

Design is blue print of something so it can be defined as creation of something in mind.

Pattern, we can define it as guideline.

Design pattern is a widely accepted solution to a recurring design problem.

Java design patterns are devided into 3 types

1)Structural Patterns

2)Behavioral Patterns

3)Creational Patterns

Structural Patterns:

Structural Patterns describe how objects and classes can be combined to form structures. The difference is that class patterns describe relationships and structures with the help of inheritance. Object patterns, describe how objects can be associated and aggregated to form structures by using object composition.

In Structural Pattern we have 7 types. Which are

1)Adapter Pattern:

Adapter - Match interfaces of different classes.

The Adapter pattern is used to translate the interface of one class into another interface.A class adapter uses multiple inheritance to adapt one interface to another.There are two ways to do this: by inheritance, and by object composition (aggregation).The UML diagram is



And the general understandable view is



Benefits: Allows two or more incompatible object to communicate and interact Improves reusability of older functionality

2)Bridge Pattern: Decouple an abstraction or interface from its implementation so that the two can vary independently.The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.

The collections class framework in the Java API provides several examples of use of the bridge pattern. Both the ArrayList? and LinkedList? concrete classes implement the List interface. The List interface provides common, abstract concepts, such as the abilities to add to a list and to ask for its size. The implementation details vary between ArrayList? and LinkedList?, mostly with respect to when memory is allocated for elements in the list.

Example:

The electric equipments you have at home and their switches. For e.g., the switch of the fan. The switch is the interface and the actual implementation is the Running of the fan once its switched-on. Still, both the switch and the fan are independent of each other. Another switch can be plugged in for the fan and this switch can be connected to light bulb.



And the UML diagram is




Benefits: Implementation can be selected or switched at run-time. The abstraction and implementation can be independently extended or composed.

3)Composite Pattern:

Composite pattern allows treating different objects in a similar fashion. In order to treat objects in a uniformed manner we need to inherit them from a common interface.We

represent a part-whole relationship in a tree structure.

Following figure shows how different objects are called in a uniform manner.



And the UML diagram is




Benefits

Define class hierarchies consisting of primitive objects and composite objects.

Makes it easier to add new kind of components.

4) Decorator Pattern: The Decorator pattern lets you attach additional responsibilities and modify instance functionality dynamically.The decorator pattern can be used to make it possible to extend (decorate) the functionality of a class at runtime. This is achieved by designing a new decorator class that wraps the original class. This wrapping could be achieved by the following sequence of steps:

1. Subclass the original "Component" class into a "Decorator" class (see UML diagram).

2. In the Decorator class, add a Component pointer as a field.

3. Pass a Component to the Decorator constructor to initialize the Component pointer.

4. In the Decorator class, redirect all "Component" methods to the "Component" pointer.

5. In the Decorator class, override any Component method(s) whose behavior needs to be modified.

The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time whereas decorating can provide new behavior at runtime.

Here is the UML diagram of the Decorator pattern followed by a description of the various involved components.



Component: Defines the interface for objects that can have responsibilities added to them dynamically.
ConcreteComponent: Defines an object to which additional responsibilities can be attached.
Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.

ConcreteDecorator: adds responsibilities to the component.

Benefits

More flexibility than static inheritance.

Avoids feature-laden classes high up in the hierarchy.

Simplifies coding because you write a series of classes each targeted at a specific part of the functionality rather than coding all behavior into the object.

Enhances the object's extensibility because you make changes by coding new classes.

5)Facade Pattern (Face or Front Appearance): The Fa├žade pattern allows you to simplify this complexity by providing a simplified interface to these subsystems. This simplification may in some cases reduce the flexibility of the underlying classes, but usually provides all the function needed for all.


This design pattern provides an integrated (combined) interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use. A facade is an object that provides a simplified interface to a larger body of code, such as a class library.

Example: Facade as the name suggests means the face of the building. The people walking past the road can only see this glass face of the building. They do not know anything about it, the wiring, the pipes and other complexities. The face hides all the complexities of the building and displays a friendly face.





Benefits

The main benefit with the Facade pattern is that we can combine very complex method calls and code blocks into a single method that performs a complex and recurring task. It also reduces code dependencies between libraries or packages.



6)Flyweight Pattern: Fly weight pattern is useful where we need to create many objects and these entire objects share some kind of common data.

Consider following figure. We need to print visiting card for all employees in the organization. We have two parts of data one is the variable data i.e. the employee name and the other is static data i.e. address. We can minimize memory by just keeping one copy of the static data and referencing the same data in all objects of variable data. So we create different copies of variable data, but reference the same copy of static data. With this we can optimally use the memory.



Benefits

Reduce the number of objects created, decrease memory footprint and increase performance.

7) Proxy Pattern:

A proxy is a class functioning as an interface to something else. The proxy could interface to the resource that is expensive or impossible to duplicate. This actual object can be a huge image or a data object which is very large and cannot be duplicated. So you can create multiple proxies and point towards the actual object (memory consuming object) and perform operations. This avoids duplication of the heavy object and thus saving memory. Proxies are references which points towards the actual object.

There are several cases where a Proxy can be useful. Few of them as follow.

1. If an object, such as a large image, takes a long time to load.

2. If the object is on a remote machine and loading it over the network may be slow, especially during peak network load periods.

3. If the object has limited access rights, the proxy can validate the access permissions for that user.

In proxy pattern, typically one instance of the complex object is created, and multiple proxy objects are created, all of which contain a reference to the single original complex object. Any operations performed on the proxies are forwarded to the original object. Once all instances of the proxy are out of scope, the complex object's memory may be deallocated.



Benefits:

Gives the ability to control access to an object, whether it's because of a costly creation process of that object or security issues.

No comments:

Post a Comment