Home > J2EE > Design Pattern

Design Pattern

Adapter Pattern:-

Converts the interface of the class to another one that client expects.

eg: Converting a Turkey into a Duck.

we wrap Turkey into a TurkeyAdapter and send the adapter to the client.

Client has a Target Interface,

Adapter implements Target Interface and has a Adaptee.

Two types of adapter:-

1. object adapter

2. class adapter

1. Object Adapter:- is what we read so far.

2. Class Adapter:- Require multiple inheritance by sub-classing both target and adaptee.

Difference between Object and Class Adapter:-

Object Adapter, based on composition, can adapt the adaptee or any of its sub class.

Class Adapter, can override behavior

Real world adapter example:-

Adapting an Enumeration to an Iterator.

Bridge Pattern

Use Bridge Pattern to vary not only your implementations, but your abstractions.

example:- Universal remote and controller and various TV implementation.

Both TV remote and TV implementation.

Decorator Pattern
Inheritance: behavior is set statically at compile time.
All sub class inherit the same behavior.
Composition: dynamically composing objects, we can add functionality in the new code, rather than altering existing code.

Design Principal: Classes should be open for extension but closed for modification.

Definition: Decorator pattern attaches additional responsibility to an object dynamically. It provides a flexible alternative to sub classing for extending the functionality.

Component is the base class, eg Beverage
Concrete component & Decorator, both extends Component, eg. Expresso & CondimentDecorator (for type matching, not for inheriting behavior.
We acquire behavior by composing objects together.
With composition, we can mix and match behavior as we want.)
Decorator also has a Component eg. Condiment also has a Beverage.
Concerete Decorators extend Decorator, eg. Milk & Soya extend Condiment.

Similar objects doing similar operations, abstract class Beverage, abstract method cost(), requirement is to call cost method on some object in hierarchy dynamically is a candidate for such pattern

Requirement:
Write code as per abstract component, writing to concrete will brake decorator.

Cost in implementing decorator:
Many small classes
Following type is necessary
Increase code complexity that factory pattern can help to reduce.

Adapter is used to match with the client requirement for an adaptee, while facade is used to simplify the interface

The facade pattern provide a unified interface to a set of interfaces. It defines a higher level of interface that make the subsystem easy to use.

Design principle : principle of least knowledge, talk only to your immediate friends.

Flyweight: one single instance of a class can be used to provide many virtual instances, in this way you are able to flw away the memory weight of creation of multiple instance

Its Approach is to collect the state of many object and rather centralize the state at one location and form virtual objects out of one object.

It is used when class has many state and can be controlled identically.
Single logical instance of a class can not behave independently from other

Example : large tree plantation project

Proxy pattern provides a surrogate or placeholder for another object to control access to.

Another object, which may be remote, expensive to create or in the need of securing.

Both RealSubject and the Proxy implement Subject interface.
This allows any client to treat the proxy just like the real subject.

Proxy keep the reference to the real object, so it can forward the requests to the subject when necessary. it also often instantiate or handles the creation of the realsubject
Realsubject is usually the object the does most of the work.

Composite pattern: allows to compose objects into tree structure to represent part-whole hierarchies.
It let client treat individual & composition of objects uniformly.
We can apply same operations over both composite & individual object.

Eg: menu & sub menu

Client has a component, which is extended by leaf & composite. Composite contains component ( recursive )

Categories: J2EE
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.