structural design patterns by quontra solutions

20
www.quontrasolutions.co.uk [email protected] [email protected] Structural Design Patterns Online Training Class

Upload: quontra-solutions

Post on 20-Jul-2015

85 views

Category:

Education


6 download

TRANSCRIPT

www.quontrasolutions.co.uk [email protected]

[email protected]

Structural Design Patterns Online Training Classes

Decorator Pattern

The Decorator Pattern is used for adding

additional functionality to a particular object as

opposed to a class of objects.

A Decorator, also known as a Wrapper, is an

object that has an interface identical to an object

that it contains.

www.quontrasolutions.co.uk [email protected]

[email protected]

• Any calls that the decorator gets, it relays

to the object that it contains, and adds its

own functionality along the way, either

before or after the call.

• This gives you a lot of flexibility, since

you can change what the decorator does

at runtime, as opposed to having the

change be static and determined at

compile time by sub classing.

www.quontrasolutions.co.uk [email protected]

[email protected]

Decorator Pattern

A Decorator is a concrete instance of the

abstract class and is indistinguishable from the

object that it contains.

This can be used to great advantage, as you

can recursively nest decorators without any

other objects being able to tell the difference,

allowing a near infinite amount of

customization.

www.quontrasolutions.co.uk [email protected]

[email protected]

• Decorators add the ability to dynamically

alter the behavior of an object.

• Good idea to use in a situation to change

the behavior of an object repeatedly during

runtime.

• It helps to add behavior or responsibilities

to an object.

www.quontrasolutions.co.uk [email protected]

[email protected]

Decorator Patternwww.quontrasolutions.co.uk [email protected]

[email protected]

Command pattern

A Command pattern is an object behavioral

pattern used to achieve complete decoupling

between the sender and the receiver.

A sender is an object that invokes an

operation, and a receiver is an object that

receives the request to execute a certain

operation.

www.quontrasolutions.co.uk [email protected]

[email protected]

• With decoupling, the sender has no

knowledge of the Receiver's interface.

• The term request here refers to the

command that is to be executed.

• The Command pattern allows to vary when

and how a request is fulfilled thus providing

flexibility as well as extensibility.

www.quontrasolutions.co.uk [email protected]

[email protected]

Command Pattern

Intent:

encapsulate a request in an object

allows the parameterization of clients with different

requests

allows saving the requests in a queue

Implementation:

The Client asks for a command to be executed.

The Invoker takes the command, encapsulates it

and places it in a queue, in case there is

something else to do first, and the

ConcreteCommand that is in charge of the

requested command, sending its result to the

Receiver.

www.quontrasolutions.co.uk [email protected]

[email protected]

Command Patternwww.quontrasolutions.co.uk [email protected]

[email protected]

Factory Pattern

Factory Method is a creational pattern which

helps to model an interface for creating an

object which at creation time can let its

subclasses decide which class to instantiate.

Factory Pattern is responsible for

"Manufacturing" an Object.

It helps to instantiate the appropriate Subclass

by creating the right Object from a group of

related classes.

www.quontrasolutions.co.uk [email protected]

[email protected]

• The Factory Pattern promotes loose coupling

by eliminating the need to bind application-

specific classes into the code.

• The use of factories gives the programmer

the opportunity to abstract the specific

attributes of an Object into specific

subclasses which create them.

• Define an interface for creating an object, but

let the subclasses decide which class to

instantiate. The Factory method lets a class

defer instantiation to subclasses.

Factory Patternwww.quontrasolutions.co.uk [email protected]

[email protected]

Singleton Pattern

Singleton involves only one class which is

responsible to instantiate itself, to make sure it

creates not more than one instance; in the

same time it provides a global point of access

to that instance.

The same instance can be used from

everywhere, being impossible to invoke directly

the constructor each time.

Intent:

Ensure that only one instance of a class is

created.

Provide a global point of access to the object.

Singleton Pattern

Implementation:

It involves a static member in the "Singleton"

class, a private constructor and a static public

method that returns a reference to the static

member.

The Singleton Pattern defines a getInstance

operation which exposes the unique instance

which is accessed by the clients.

Constructor is not accessible from outside of

the class.

www.quontrasolutions.co.uk [email protected]

[email protected]

Singleton: Specific Implementation

issues Thread-safe implementation: Singletons can be

used specifically in multi-threaded application to make sure the reads/writes are synchronized.

Lazy instantiation: Synchronization can be very expensive considering the performance. When the singleton object is already created it should be returned without using any synchronized block.

Such optimization consist of checking in an unsynchronized block if the object is null and if not to check again and create it in an syncronizedblock. This is called double locking mechanism.

www.quontrasolutions.co.uk [email protected]

[email protected]

Singleton: Specific Implementation

issues

Early instantiation using static field: The

singleton object is instantiated when the class

is loaded and not when it is first used, due to

the fact that the instance member is declared

static.

Classes loaded by different classloaders: If

a class(same name, same package) is loaded

by 2 different classloaders they represents 2

different clasess in memory.

www.quontrasolutions.co.uk [email protected]

[email protected]

• Serialization: If Singleton class implements

java.io.Serializable interface, when a

singleton is serialized and then deserialized

more than once, there will be multiple

instances of Singleton created. It can be

avoided by implementing the readResolve

method.

• Abstract Factory and Factory Methods

implemented as singletons: Having 2

factories might have undesired effects when

objects are created. Hence, to ensure that a

factory is nique it should be implemented as

a singleton, thus avoiding to instantiate the

Singleton Patternwww.quontrasolutions.co.uk [email protected]

[email protected]

www.quontrasolutions.co.uk [email protected]

[email protected]

Visit: http://www.quontrasolutions.co.uk/

Email: [email protected]

Call Now : 20-3734-1498