0% found this document useful (0 votes)
15 views5 pages

OOPUnit 7 TotalNote BIT 1684464495531 1708351073972

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

OOPUnit 7 TotalNote BIT 1684464495531 1708351073972

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit 7: Design Pattern

Introduction to design pattern


A design patterns are well-proved solution for solving the specific problem/task. But
remember one-thing, design patterns are programming language independent strategies for
solving the common object-oriented design problems. That means, a design pattern represents
an idea, not a particular implementation.
By using the design patterns you can make your code more flexible, reusable and
maintainable. It is the most important part because java internally follows design patterns.
To become a professional software developer, you must know at least some popular solutions
(i.e. design patterns) to the coding problems.

Advantage of design pattern:


 They are reusable in multiple projects.
 They provide the solutions that help to define the system architecture.
 They capture the software engineering experiences.
 They provide transparency to the design of an application.
 They are well-proved and testified solutions since they have been built upon the
knowledge and experience of expert software developers.
 Design patterns don?t guarantee an absolute solution to a problem. They provide
clarity to the system architecture and the possibility of building a better system.

Singleton, factory, abstract factory


Singleton Pattern says that just"define a class that has only one instance and provides a global
point of access to it".
In other words, a class must ensure that only single instance should be created and single
object can be used by all other classes.
There are two forms of singleton design pattern
 Early Instantiation: creation of instance at load time.
 Lazy Instantiation: creation of instance when required.
Advantage of Singleton design pattern
Saves memory because object is not created at each request. Only single instance is reused
again and again.
Usage of Singleton design pattern
Singleton pattern is mostly used in multi-threaded and database applications. It is used in
logging, caching, thread pools, configuration settings etc.

1
Factory Method Pattern
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract
class for creating an object but let the subclasses decide which class to instantiate. In other
words, subclasses are responsible to create the instance of the class.
The Factory Method Pattern is also known as Virtual Constructor.

Advantage of Factory Design Pattern


 Factory Method Pattern allows the sub-classes to choose the type of objects to create.
 It promotes the loose-coupling by eliminating the need to bind application-specific
classes into the code. That means the code interacts solely with the resultant interface
or abstract class, so that it will work with any classes that implement that interface or
that extends that abstract class.

Usage of Factory Design Pattern


 When a class doesn't know what sub-classes will be required to create
 When a class wants that its sub-classes specify the objects to be created.
 When the parent classes choose the creation of objects to its sub-classes.

Abstract Factory Pattern


Abstract Factory Pattern says that just define an interface or abstract class for creating
families of related (or dependent) objects but without specifying their concrete sub-
classes.That means Abstract Factory lets a class returns a factory of classes. So, this is the
reason that Abstract Factory Pattern is one level higher than the Factory Pattern.
An Abstract Factory Pattern is also known as Kit.

Advantage of Abstract Factory Pattern


 Abstract Factory Pattern isolates the client code from concrete (implementation)
classes.
 It eases the exchanging of object families.
 It promotes consistency among objects.

Usage of Abstract Factory Pattern


 When the system needs to be independent of how its object are created, composed,
and represented.
 When the family of related objects has to be used together, then this constraint needs
to be enforced.
 When you want to provide a library of objects that does not show implementations
and only reveals interfaces.
 When the system needs to be configured with one of a multiple family of objects.

2
Adapter Pattern
An Adapter Pattern says that just "converts the interface of a class into another interface that
a client wants".
In other words, to provide the interface according to client requirement while using the
services of a class with a different interface.
The Adapter Pattern is also known as Wrapper.
Advantage of Adapter Pattern
 It allows two or more previously incompatible objects to interact.
 It allows reusability of existing functionality.
Usage of Adapter pattern:
It is used:
 When an object needs to utilize an existing class with an incompatible interface.
 When you want to create a reusable class that cooperates with classes which don't
have compatible interfaces.
 When you want to create a reusable class that cooperates with classes which don't
have compatible interfaces.

Composite Pattern
A Composite Pattern says that just "allow clients to operate in generic manner on objects that
may or may not represent a hierarchy of objects".
Advantage of Composite Design Pattern
 It defines class hierarchies that contain primitive and complex objects.
 It makes easier to you to add new kinds of components.
 It provides flexibility of structure with manageable class or interface.
Usage of Composite Pattern
It is used:
 When you want to represent a full or partial hierarchy of objects.
 When the responsibilities are needed to be added dynamically to the individual
objects without affecting other objects. Where the responsibility of object may vary
from time to time.

Decorator Pattern
A Decorator Pattern says that just "attach a flexible additional responsibilities to an object
dynamically".

3
In other words, The Decorator Pattern uses composition instead of inheritance to extend the
functionality of an object at runtime.
The Decorator Pattern is also known as Wrapper.
Advantage of Decorator Pattern
 It provides greater flexibility than static inheritance.
 It enhances the extensibility of the object, because changes are made by coding new
classes.
 It simplifies the coding by allowing you to develop a series of functionality from
targeted classes instead of coding all of the behavior into the object.
Usage of Decorator Pattern
It is used:
 When you want to transparently and dynamically add responsibilities to objects
without affecting other objects.
 When you want to add responsibilities to an object that you may want to change in
future.
 Extending functionality by sub-classing is no longer practical.

Chain Of Responsibility Pattern


In chain of responsibility, sender sends a request to a chain of objects. The request can be
handled by any object in the chain.
A Chain of Responsibility Pattern says that just "avoid coupling the sender of a request to
its receiver by giving multiple objects a chance to handle the request". For example, an
ATM uses the Chain of Responsibility design pattern in money giving process.
In other words, we can say that normally each receiver contains reference of another receiver.
If one object cannot handle the request then it passes the same to the next receiver and so on.
Advantage of Chain of Responsibility Pattern
 It reduces the coupling.
 It adds flexibility while assigning the responsibilities to objects.
 It allows a set of classes to act as one; events produced in one class can be sent to
other handler classes with the help of composition.
Usage of Chain of Responsibility Pattern:
It is used:
 When more than one object can handle a request and the handler is unknown.
 When the group of objects that can handle the request must be specified in dynamic
way.

4
Observer Pattern
An Observer Pattern says that "just define a one-to-one dependency so that when one object
changes state, all its dependents are notified and updated automatically".
The observer pattern is also known as Dependents or Publish-Subscribe.
Benefits:
 It describes the coupling between the objects and the observer.
 It provides the support for broadcast-type communication.
Usage:
 When the change of a state in one object must be reflected in another object without
keeping the objects tight coupled.
 When the framework we writes and needs to be enhanced in future with new
observers with minimal chamges.

You might also like