KEMBAR78
Design Patterns | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
16 views40 pages

Design Patterns

Uploaded by

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

Design Patterns

Uploaded by

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

Design Patterns

CSE327: Software Engineering


Design Patterns
 Design patterns are typical solutions to
commonly occurring problems in software
design.
 They are like pre-made blue- prints that you

can customize to solve a recurring design


problem in your code.

2
Components of a Design Pattern
 Intent of the pattern briefly describes both
the problem and the solution.
 Motivation further explains the problem and

the solution the pattern makes possible.


 Structure of classes shows each part of the

pattern and how they are related.


 Code example in one of the popular

programming languages makes it easier to


grasp the idea behind the pattern.
3
Classification of Design
Patterns
 Creational patterns provide object creation
mechanisms that increase flexibility and
reuse of existing code.
 Structural patterns explain how to assemble

objects and classes into larger structures,


while keeping the structures flexible and
efficient.
 Behavioral patterns take care of effective

communication and the assignment of


responsibilities between objects. 4
Creational Design Patterns
 Creational design patterns are
concerned with the way of
creating objects. These
design patterns are used
when a decision must be
made at the time of
instantiation of a class (i.e.
creating an object of a class).

5
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.
6
Factory Method Pattern

7
Advantages of Factory Pattern
 Creational logic need not to be repeated.
 Adding a new class will extend the Factory

class/interface. The new subclass has no


effect on the driver class/client code. It
confirms the Open/Closed Principle.

8
Singleton Pattern
 This pattern involves a class that creates only
a single object.
 This class provides a way to access its only

object that can be accessed without


instantiating it.

9
Singleton Pattern

10
Advantages of Singleton
Pattern
 Saves memory because object is not created
at each request. Only single instance is
reused again and again.
 Singleton pattern is mostly used in multi-

threaded and database applications. It is


used in logging, caching, thread pools,
configuration settings etc.

11
Structural Pattern
 Structural design patterns are concerned with
how classes and objects can be composed,
to form larger structures.
 The structural design patterns simplifies the

structure by identifying the relationships.


 These patterns focus on, how the classes

inherit from each other and how they are


composed from other classes.

12
Types of Structural Pattern
 Adapter Pattern
 Adapting an interface into another according to client

expectation.
 Bridge Pattern
 Separating abstraction (interface) from implementation.

 Composite Pattern
 Allowing clients to operate on hierarchy of objects.

 Decorator Pattern
 Adding functionality to an object dynamically.

 Facade Pattern
 Providing an interface to a set of interfaces.

 Flyweight Pattern
 Reusing an object by sharing it.

 Proxy Pattern
 Representing another object.
13
Adapter Pattern
 It is a structural design pattern that allows
object with incompatible interfaces to
collaborate.
 It acts as a bridge between two incompatible

interfaces.
 An Adapter Pattern says that just "converts

the interface of a class into another interface


that a client wants".

14
Adapter Pattern

15
Advantages of Adapter Pattern
 It allows two or more previously incompatible
objects to interact.
 It allows reusability of existing functionality.

16
Decorator Design Pattern
 It allows developers to add new
functionalities to an existing object without
altering its structures.
 It creates a decorator class that wraps the

original class and provides new


functionalities without changing the existing
structure.
 A Decorator Pattern says that just "attach a

flexible additional responsibilities to an object


dynamically". 17
Decorator Pattern

18
Advantages 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.
19
Behavioural Design Patterns
 Behavioral design patterns are concerned
with the interaction and responsibility of
objects.

 Inthese design patterns, the interaction


between the objects should be in such a way
that they can easily talk to each other and still
should be loosely coupled.

20
Types of Behavioural Patterns
 Chain of  State Pattern
Responsibility Pattern  Strategy Pattern
 Command Pattern  Template Pattern
 Interpreter Pattern  Visitor Pattern
 Iterator Pattern  Null Object
 Mediator Pattern
 Memento Pattern
 Observer Pattern

21
Strategy Pattern
 Itcontrols the class behaviour or its
algorithms which can be changed during
runtime.
 The Strategy Pattern is also known as Policy.

22
Strategy Pattern

23
Advantages of Strategy
Pattern
 It provides a substitute to subclassing.
 It defines each behavior within its own class,

eliminating the need for conditional


statements.
 It makes it easier to extend and incorporate

new behavior without changing the


application.

24
Model-View-Controller

25
MVC
 Theclassic design pattern
 Used for data-driven user applications
 Such apps juggle several tasks:
 Loading and storing the data – getting it in/out of storage on request
 Constructing the user interface – what the user sees
 Interpreting user actions – deciding whether to modify the UI or data
 These tasks are largely independent of each
other
 Model, view, and controller each get one task
Model
talks to data source
Which database table is
to retrieve and store the requested data
stored in?
data

What SQL query will get


me the data
I need?
View
asks model for data Would this text look
and presents it in a better blue or red? In the
bottom corner
user-friendly format or front and center?

Should these items go in a


dropdown list or radio
buttons?
Controller
listens for the user The user just clicked the
to change data or “hide details” button. I
better tell the view.
state in the UI,
notifying the model
or view accordingly
The user just changed the
event details. I better let the
model know to update the
data.
Benefits of MVC
 Organization of code
 Maintainable, easy to find what you need
 Ease of development
 Build and test components independently
 Flexibility
 Swap out views for different presentations of the same data (ex:
calendar daily, weekly, or monthly view)
 Swap out models to change data storage without affecting user
MVC Flow in Theory

View

Model

Controller
MVC Flow
 In theory…
 Pattern of behavior in response to inputs (controller) are independent of
visual geometry (view)
 Controller contacts view to interpret what input events should mean in
the context of the view
 In practice…
 View and controller are so intertwined that they almost always occur in
matched pairs (ex: command line interface)
 Many architectures combine the two
MVC Flow in Practice

View
Model
Controller
Push vs. Pull

View

Model

Controller
Push vs. Pull Architecture
 Push architecture
 As soon as the model changes, it notifies all of
the views

 Pull architecture
 When a view needs to be updated, it asks the
model for new data
Push vs. Pull Architecture
 Advantages for push
 Guaranteed to have latest data in case something
goes wrong later on
 Eager implementation

 Advantages for pull


 Avoid unnecessary updates, not nearly as
intensive on the view
 Lazy implementation
A Simple Example

37
MVC Example – Traffic Signal
Traffic Signal
 Model
 Stores current state of traffic flow
 Knows current direction of traffic
 Capable of skipping a light cycle
 Stores whether there are cars and/or pedestrians waiting

 View
 Conveys information to cars and pedestrians in a specific

direction

 Controller
 Aware of model’s current direction

 Triggers methods to notify model that state should change


Traffic Signal Code
 Model
 TrafficModel – keeps track of which lights should be on and off

 View
 CarLight – shows relevant state of TrafficModel to cars

 PedestrianLight – shows relevant state of TrafficModel to pedestrians

 Controller
 PedestrianButton – notifies TrafficModel that there is a pedestrian

waiting
 CarDetector – notifies TrafficModel that there is a car waiting

 LightSwitch – enables or disables the light

 Timer – regulates time in some way, possibly to skip cycles

You might also like