Unit II-Chapter No.
: 5- design Patterns
Design Patterns
Topic Learning Outcomes
1. Explain java design patterns
2. Apply design pattern to solve a given problem
3. Apply suitable design pattern solution for a given problem
specification
School of Computer Science & Engineering
Content
1. Introduction to design patterns
2. Creational Patterns: Singleton pattern, Factory method pattern
3. Behaviour pattern: Strategy pattern, Template method pattern
4. Structural pattern: Adapter pattern, Composite pattern.
School of Computer Science & Engineering
Introduction
• The design pattern is a general reusable solution to a commonly
occurring problem in software design.
• OR , The design pattern are just a formal language to define
common ways to solve software engineering problems.
• We (most people)designed solutions (class diagrams) using
design patterns, without knowing that they are.
• People will naturally come up with solutions that will fit into a
design pattern on their own, but the design patterns help to
define terminology and standard ideas that can be helpful.
• The main benefit of design patterns is that, they are ideas that
can be defined in ways that are repeatedly useful (reusable).
School of Computer Science & Engineering
Design Patterns: Limitations
• One of the limitation (due to reusability) Is that, the design
patterns stop people from coming up with proper solution to a
specific problem.
• Instead of thinking about how to solve that particular problem,
people try applying one design pattern after the other.
• However, if we know language of design patterns (UML) and
standard design patterns, we can take advantage of reusability and
we can design our own solutions.
School of Computer Science & Engineering
Creational Design Pattern:
• In software engineering, creational design patterns
are design patterns that deal with object creation
mechanisms, trying to create objects in a manner
suitable to the situation.
– Singleton pattern,
– Factory method pattern
School of Computer Science & Engineering
Singleton pattern
• Singleton pattern is one of the simplest design patterns in Java,
as this pattern provides one of the best ways to create an object.
• This pattern involves a single class which is responsible to create
an object while making sure that only single object gets created.
• This class provides a way to access its only object which can be
accessed directly without need to instantiate the object of the
class.
School of Computer Science & Engineering
Singleton pattern: implementation
• create a SingleObject class, it has its constructor as
private and have a static instance of itself.
• SingleObject class provides a static method to get its
static instance to outside world.
• SingletonPatternDemo class will use SingleObject class
to get a SingleObject object.
School of Computer Science & Engineering
Singleton pattern: class diagram
School of Computer Science & Engineering
Singleton pattern: code
• Step 1
– Create a Singleton Class.
– [Link]
School of Computer Science & Engineering
• Step 2
– Get the only object from the singleton class.
– [Link]
School of Computer Science & Engineering
• Step 3
– Verify the output.
– Hello World!
School of Computer Science & Engineering
Design Pattern - Factory Pattern
• Factory pattern is one of most used design pattern in
Java.
• In Factory pattern, we create object without exposing
the creation logic to the client and refer to newly
created object using a common interface.
School of Computer Science & Engineering
Factory Pattern: class diagram
School of Computer Science & Engineering
Factory Pattern: Implementation
• create a Shape interface and concrete classes implementing the
Shape interface.
• A factory class ShapeFactory is defined as a next step.
• FactoryPatternDemo, our demo class will use ShapeFactory to get
a Shapeobject.
• It will pass information (CIRCLE / RECTANGLE / SQUARE)
toShapeFactory to get the type of object it needs.
School of Computer Science & Engineering
• Step 1
– Create an interface.
[Link]
public interface Shape { void draw();}
• Step 2
– Create concrete classes implementing the same interface.
• [Link]
public class Rectangle implements Shape {
public void draw() {
[Link]("Inside Rectangle::draw() method."); } }
School of Computer Science & Engineering
Factory Pattern: implementation
[Link]
public class Square implements Shape {
public void draw() { [Link]("Inside
Square::draw() method."); }}
[Link]
public class Circle implements Shape {
public void draw() { [Link]("Inside
Circle::draw() method."); }}
School of Computer Science & Engineering
[Link]
• Step 3: Create a Factory to generate object of concrete class
based on given information.
School of Computer Science & Engineering
• Step 4
– Use the Factory to get object of concrete class by passing an
information such as type.
[Link]
School of Computer Science & Engineering
Factory Pattern: output
• Step 5
• Verify the output.
– Inside Circle::draw() method.
– Inside Rectangle::draw() method.
– Inside Square::draw() method.
School of Computer Science & Engineering
Behaviour Pattern: strategy pattern
• In Strategy pattern, a class behaviour or its algorithm can be
changed at run time.
– Example
– Base class reference variable/interface reference variable can be assigned
sub class object
– The type of the object assigned decides which version (object) method has
to be called.
• In Strategy pattern, we create objects which represent various
strategies and a context object whose behavior varies as per its
strategy object.
• The strategy object changes the executing algorithm of the
context object.
School of Computer Science & Engineering
Strategy pattern: class diagram
School of Computer Science & Engineering
Strategy pattern: implementation
Steps
1. create a Strategy interface defining an action
2. Create concrete strategy classes implementing
the Strategy interface
3. Context is a class which uses a Strategy.
4. Create StrategyPatternDemo, a demo class, that will
use Context and strategy objects to demonstrate change
in Context behaviour based on strategy it deploys or
uses.
School of Computer Science & Engineering
Strategy pattern: code
School of Computer Science & Engineering
Strategy pattern: code
School of Computer Science & Engineering
Strategy pattern: code
School of Computer Science & Engineering
Behaviour pattern: Template pattern
• In Template pattern, an abstract class exposes defined
way(s)/template(s) to execute its methods
• Its subclasses can override the method implementation as per
need but the invocation is to be in the same way as defined by an
abstract class
School of Computer Science & Engineering
Template pattern: class diagram
School of Computer Science & Engineering
Template pattern: implementation
School of Computer Science & Engineering
Template Pattern
School of Computer Science & Engineering
Template Pattern
School of Computer Science & Engineering
Structural pattern: Adapter Pattern
• Adapter pattern works as a bridge between two
incompatible interfaces.
• this pattern combines the capability of two
independent interfaces.
• This pattern involves a single class which is responsible
to join functionalities of independent or incompatible
interfaces
• A real life example could be a case of card reader which acts as
an adapter between memory card and a laptop. You plugin the
memory card into card reader and card reader into the laptop so
that memory card can be read via laptop.
School of Computer Science & Engineering
Adapter Pattern
• Consider the following scenario:
• We have a MediaPlayer interface and a concrete class
AudioPlayer implementing the MediaPlayer interface
and AudioPlayer can play mp3 format audio files by
default.
• We are having another
interface AdvancedMediaPlayer and concrete classes
implementing the AdvancedMediaPlayer interface.
These classes can play vlc and mp4 format files.
School of Computer Science & Engineering
Adapter Pattern
• Now we wanted to make AudioPlayer to play other
formats as well. To attain this, we have created an
adapter class MediaAdapter which implements
the MediaPlayerinterface and
uses AdvancedMediaPlayer objects to play the required
format.
• AudioPlayer uses the adapter
class MediaAdapter passing it the desired audio type
without knowing the actual class which can play the
desired format. AdapterPatternDemo, our demo class
will use AudioPlayer class to play various formats.
School of Computer Science & Engineering
Adapter Pattern: class diagram
School of Computer Science & Engineering
Adapter Pattern: implementation
School of Computer Science & Engineering
Adapter Pattern: implementation
School of Computer Science & Engineering
Adapter Pattern : implementation
School of Computer Science & Engineering
Adapter Pattern : implementation
School of Computer Science & Engineering
Adapter Pattern
School of Computer Science & Engineering
Adapter Pattern
School of Computer Science & Engineering
Structural Pattern: Composite Pattern
• Composite pattern is used where we need to treat a
group of objects in similar way as a single object.
• Composite pattern composes objects in term of a tree
structure to represent part as well as whole hierarchy.
• This pattern creates a class that contains group of its
own objects.
• This class provides ways to modify its group of same
objects.
School of Computer Science & Engineering
Structural Pattern: class diagram
School of Computer Science & Engineering
Composite Pattern: Implementation
Step 1: Create Employee class having list Employee objects.
[Link]
School of Computer Science & Engineering
Composite Pattern
School of Computer Science & Engineering
Composite Pattern
School of Computer Science & Engineering
Composite Pattern
School of Computer Science & Engineering
Assignment
Identify suitable design pattern for the following
scenario and draw class diagram
• A company pays its employees on a weekly basis. The employees
are of four types: Salaried employees are paid a fixed weekly
salary regardless of the number of hours worked, hourly
employees are paid by the hour and receive overtime pay (i.e., 1.5
times their hourly salary rate) for all hours worked in excess of 40
hours, commission employees are paid a percentage of their sales
and base salaried commission employees receive a base salary
plus a percentage of their sales. For the current pay period, the
company has decided to reward salaried-commission employees
by adding 10% to their base salaries. Write a java application that
performs payroll calculations.
•
School of Computer Science & Engineering
Adapter Pattern
School of Computer Science & Engineering
Adapter Pattern
School of Computer Science & Engineering