Transcript Document

Design Patterns
By
Archana Munnangi
(17040771)
S R Kumar
(17040037)
Module Name - Object Oriented Modeling
Utkarsh Batwal
(17040842)
What are Design Patterns ?

It addresses a problem that occurs repeatedly in a
variety of contexts and suggests the solution to the
problem.
Pattern Elements




Pattern Name
Problem
Solution
Consequences
Design Pattern Space
Purpose
Scope
Creational
Structural
Behavioral
Class
Factory Method
Adapter Method
Interpreter
Template Method
Object
Abstract Factory
Builder
Prototype
Singleton
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
Chain of Responsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Visitor
Purpose – Reflects what a pattern does. Patterns can have either a Creational,
Structural or Behavioral purpose
Scope – Specifies whether the pattern applies primarily to the Classes or to the
Objects
Creational Pattern




Abstract the instantiation process
Separate the system from object creation
Encapsulate information about concrete classes in
the system
Hide how instances are created
Examples : Factory Method, Abstract Factory,
Builder, Prototype, Singleton
Prototype (Creational Pattern)

Intent - Specify the kinds of objects to create using a
prototypical instance, and create new objects by copying this
prototype.
 Hides complexities of making
new instances from the client
 In some cases copying an
existing object can be more
efficient than creating a new
object.
 Provides the option for client to
generate objects whose type is
not known.
Example of a Prototype Pattern - Mitotic Cell Division in animal cells.
The Animal cell corresponds to the Prototype,
as it has an “interface” for cloning itself

A specific instance of a cell corresponds to the
ConcretePrototype

Prototype pattern in the original cell takes an
active role in creating a new instance of itself


Sample code to clone an Animal cell
public class SingleCellOrganism {
public static void main(String[] args)
{
AnimalCell cell = new AnimalCell();
// create a clone
AnimalCell newAnimalCell =
(AnimalCell)cell.split();
}
}// End of class
Here split() implements the clone() function
in the Animal cell

Use of Prototype –
 In Complex hierarchies prototype should be considered
for creating new object of many types
Consequences –
 Add/Remove products at runtime
 Specifying new object by varying objects
 Specifying new object by varying structure
 Reduced subclassing
Disadvantages –

Making a copy of the objects is sometimes very
complicated.
Structural Pattern



Structural patterns describe how classes and
objects can be combined to form larger structures.
Structural class patterns use inheritance to
compose Interfaces or Implementations.
Structural object patterns define ways to compose
objects to obtain new functionality.
Examples : Adapter , Bridge, Composite,
Decorator, Facade, Flyweight, Proxy
Decorator (Structural Pattern)

Intent – Attach additional responsibilities to an object
dynamically. Provide a flexible alternative to subclassing for
extending functionality . They are also known as Wrapper.
 A set of decorator classes are
used to wrap the Concrete (core)
components.
 Decorators change the properties
of the Concrete components by
adding new functionality.
 Follows the Open-Close design
principle
Example of Decorator pattern :
Use Inheritance?
Problem with Inheritance !!!!

Solution –
 Design Principle – The Classes
must be open for extension but
closed for modification.
Solution :
Sample Code for Cost Implementation of CoffeaArabia with Mocha
Total Cost = 59 + 39 = 98 Rupees
Use of Decorator –


Add/Withdraw responsibilities to/from the individual objects
dynamically
When extension by subclassing is impractical
Consequences –
 Flexibility
 Offers a “pay as you go” approach
 Decorators and its component are not identical
 Lots of little objects are created
Disadvantages –
 May add many classes, makes package hard to understand
─ Like I/O streams
 Creating new objects could be more complex
Behavioral Pattern



Deals with the algorithms and the assignment of
responsibilities between the objects
Behavioral class patterns use inheritance to describe
algorithms and flow of control between classes.
Behavioral object pattern use the composition of objects
to perform some task that no single object can perform
alone.
Examples : Chain of Responsibility, Command, Iterator,
Mediator, Memento, Observer, State, Strategy, Visitor,
Interpreter, Template Method
Strategy (Behavioral Pattern)

Intent – Define a family of algorithms, encapsulate
each one, and make them interchangeable. Strategy lets
the algorithm vary independently from clients that use
it.
 The Strategy pattern is often used
as an alternative to inheritance,
where we can end up spreading out
the way we handle specific task over
many class files.
 Change the behavior at
runtime (by using Composition
Has - A relationship)
Example of Strategy pattern in a game :




Favor Composition over Inheritance
HAS-A (Composition) can be better than IS-A (Inheritance)
Allows changing behavior at run time
The Character class will delegate its Weapon Behavior instead of implementing these itself
Calling the setWeapon()
Sample Code for getting new weapon
public abstract class Character {
private WeaponBehaviour weapon;
private WalkBehaviour walk;
public void fight() {
weapon.useWeapon();
// delegation of fight behaviour
}
public void setWeapon(WeaponBehaviour w){
weapon = w;
}
...
abstract void display();
}
public class Mona extends Character
{
public Mona()
{
weapon = new Sniper(); // The character Mona gets the
Sniper weapon
…
}
public void display() {
...
}
}
Uses of a Strategy Pattern –
Strategy is used:
 To configure a class with one of many behaviors
 When we want to change the algorithm that we use at runtime
Consequences –
 Families of related algorithms
 An alternative to subclassing
 Strategies eliminate conditional statements
 Choice of different implementations
Disadvantages –
 Strategy pattern can only be used when the variation in behavior
is relevant to clients
Patterns learned:



Prototype ~ Creational Pattern
Decorator ~ Structural Pattern
Strategy ~ Behavioral Pattern
Advantages of Design Patterns






They capture expertise and make it accessible to nonexperts
Reusing design knowledge
They form a vocabulary that helps developers
communicate better
They help people understand a systems more quickly when
it is documented with the patterns it uses
Focus is on developing flexible, maintainable programs
Using design patterns in the early life of software system
design prevents later refactorings
Drawbacks of Patterns

Patterns do not lead to direct code reuse

Individual Patterns are deceptively simple

Composition of different patterns can be very complex

Teams may suffer from pattern overload

Patterns are validated by experience and discussion rather than
by automated testing

Integrating patterns into a software development process is a
humanintensive activity
CONCLUSION
THANK YOU
?