Title: Structural Design Patterns by Quontra Solutions
1info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
Structural Design Patterns Online Training Classes
info_at_quontrasolutions.co.uk
2Decorator Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
3info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
4Decorator Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
5info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
6Decorator Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
info_at_quontrasolutions.co.uk
7Command pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
8info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
9Command Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
10Command Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
info_at_quontrasolutions.co.uk
11Factory Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
12- 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.
13Factory Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
info_at_quontrasolutions.co.uk
14Singleton 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.
15Singleton Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
16Singleton Specific Implementation issues
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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
syncronized block. This is called double locking
mechanism.
info_at_quontrasolutions.co.uk
17Singleton Specific Implementation issues
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
- 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.
info_at_quontrasolutions.co.uk
18- 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 class before using it.
19Singleton Pattern
info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
info_at_quontrasolutions.co.uk
20info_at_quontrasolutions.co.uk
www.quontrasolutions.co.uk
For More Details Contact us
Quontra Solutions
Visit http//www.quontrasolutions.co.uk/ Email
info_at_quontrasolutions.co.uk Call Now
20-3734-1498
info_at_quontrasolutions.co.uk