43
Design Pattern By Julie Iskander MSc. Communication and Electronics

Design Pattern lecture 2

Embed Size (px)

DESCRIPTION

Lecture two, An introduction to Design Pattern History Pattern Language, Categorization according to GoF MVC Creational Design Patterm Factory Method Abstract Factory Singleton Builder

Citation preview

Page 1: Design Pattern lecture 2

Design PatternBy Julie Iskander

MSc. Communication and Electronics

Page 2: Design Pattern lecture 2

Outlines Lecture 2

• What is Design Patterns?

• History of Design Patterns

• How to learn Design Patterns?

• Design Patterns Classifications acc. to GoF

• Creational DP:• Singleton• Factory Method• Abstract Method• Builder• Prototype

Page 3: Design Pattern lecture 2

What is Design Pattern

• A pattern is a solution to a common problem,

•NOT a code solution ready for copy-and-paste but a best practice, a useful abstraction, and a template for solving categories of problems.

Page 4: Design Pattern lecture 2

What is Design Pattern

•Design Patterns are frequently used ways of organizing objects to make solutions easier to extend and modify

•Design Patterns name and describe effective solutions for common problems

•Design Pattern help a designer get a design right faster.

• Never reinvent the wheel.

Page 5: Design Pattern lecture 2

What is Design Pattern

• “Each pattern describes a problem which occurs over and over again, and then describes the core of the solution in such a way that you can use this solution many times without ever doing it the same way twice”, Christopher Alexander

Page 6: Design Pattern lecture 2

History of Design Pattern

• Dr. Christopher Alexander (in civil engineering), in1977, A Pattern Language.

• In 1978–79, Prof. Trygve Reenskaug (in computer science), wrote “The design pattern: Model-View–Controller (MVC)”.

• In 1995, a catalog of 23 design patterns by Drs. Gamma, Helm, Johnson, and Vlissides “Gang of Four” (GoF), “Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995)”.

Page 7: Design Pattern lecture 2

How to learn Design patterns ?

The best way to use patterns is to load your brain with them and then recognize places in your designs and existing applications where you can apply them

Page 8: Design Pattern lecture 2

“Designing OO software is hard and designing reusable OO software even harder”, Erich Gamma

Experience Reusing solutions that worked before

Knowing patterns that worked in the past make designer more productive and creating flexible and reusable designs

Page 9: Design Pattern lecture 2

Instead of code reuse, with design patterns you get experience reuse

Page 10: Design Pattern lecture 2

Advantages of learning Design Pattern

• It helps us to communicate in an efficient manner, creates a common vocabulary

• It enables us to discuss low-level implementation in a high-level, abstract manner.

• It enables us to apply a proven solution (experience reuse)

• It helps extendibility and avoid solution redesign

• It help present design alternatives if found.

• It can help improve maintenance and documentation

Page 11: Design Pattern lecture 2

Design Pattern elements

•Pattern Name

•Problem : When to apply? Problem and Context

•Solution

•Consequence : results, trade offs of applying it

Page 12: Design Pattern lecture 2

Model-View-ControllerMVC

Page 13: Design Pattern lecture 2
Page 14: Design Pattern lecture 2

MVC

• What , Where• a requirement to vary perspectives or views

(output) without changing the underlying information source (model).

• Why• it is more manageable and less expensive to

encapsulate change in a set of controller objects than it is to disfigure the model object through constant reengineering.

Page 15: Design Pattern lecture 2

MVC

•How• Encapsulating the client requirements in a

controller object that transposes the information from the model object into the variety of formats required by the client. • a model object (model) • a view interface (IView), or abstract class, if you

prefer; • Deriving any number of views (GraphView and

BarView classes) from the interface; • creating a controller object (controller), to represent

each view; and then wiring up the functionality. • The client accesses the views it wants from the

controllers.

Page 16: Design Pattern lecture 2

Design Patterns according to the

GOF

Page 17: Design Pattern lecture 2

DP Classifications

•According to scope

Class Object

Deals with relationships between classes and their subclasses

Done by inheritance

Deals with objects relationships that can be changed at runtime, dynamically

Page 18: Design Pattern lecture 2

DP Classifications

•According to purpose

Creational Structural Behavioral

Gives program the flexibility in deciding which object to create.When and how

to create objects

Deals with the composition of

classes and objects to create larger structures

Or add functionalities

Characterizes the way classes

and objects interact and

distribute responsibilities

Page 19: Design Pattern lecture 2

DP Classifications

•According to purposeCreational Structural Behavioral

SingletonFactory Method (Class Pattern) Abstract Factory

BuilderPrototype

Adapter (Class Pattern)

BridgeCompositeDecorator

FaçadeFlyweight

Proxy

Chain of Responsibility

CommandIterator(Class

Pattern)MediatorMementoObserver

StateStrategy

Template Method (Class Pattern)

VisitorInterpreter

Page 20: Design Pattern lecture 2

Creational Patterns

Page 21: Design Pattern lecture 2

Singleton(object – creational dp)

Page 22: Design Pattern lecture 2

Singleton (object – Creational DP)

•What, Why• A class has one and only one instance

created

•How• private constructor

Page 23: Design Pattern lecture 2

Singleton (object – Creational DP)

Page 24: Design Pattern lecture 2

•Notes:•Can permit a variable # of

instances

Singleton (object – Creational DP)

Page 25: Design Pattern lecture 2

Factory method (Class – Creational DP)

There is more to making objects than just using the new operator.

Instantiation is an activity that shouldn’t always be done in public and can often

lead to coupling problems.

Page 26: Design Pattern lecture 2

Factory method (Class – Creational DP)

•What• A design that defines an interface for creating

objects and delegates the choice of what objects to create to subclasses.

• A creator class is required to be decoupled from creating specific objects.

•Why• Flexibility in creating a variety of objects

•How• Subclasses, with overridden factory method

create specific types, which in turn create content objects.

Page 27: Design Pattern lecture 2

Factory method (Class – Creational DP)

Page 28: Design Pattern lecture 2

•Example• An application with multiple

documents• Document type to create depends on

specific application• Drawing Application Drawing

Documents• Writing Application Writing Documents

Factory method (Class – Creational DP)

Page 29: Design Pattern lecture 2

Factory method (Class – Creational DP)

Page 30: Design Pattern lecture 2

•Another implementation:• Parametrized Factory Method

• Parameter specifies the type of product to instantiate

• No inheritance in creator class• All Products must implement the

product interface

Factory method (Class – Creational DP)

Page 31: Design Pattern lecture 2

Abstract Factory DP (object– Creational DP)

• What• Client deals with a high-level abstraction for creating

families of products while leaving the factory abstraction to deal with coupling the implementation.

• A requirement not to couple the client to an implementation that creates sets or families of objects. The pattern enables the client to commit to an interface and avoid commitment to an implementation.

• Why• Enhancing the client’s ability to switch between a set or

family of objects.

• How• Client code has an association with the interface of the

factory class. The factory objects create sets of objects .

Page 32: Design Pattern lecture 2

Abstract Factory DP (object– Creational DP)

Page 33: Design Pattern lecture 2

Abstract Factory DP (object– Creational DP)

Page 34: Design Pattern lecture 2

Abstract Factory DP (object– Creational DP)

Page 35: Design Pattern lecture 2

•Another Example• Web application that renders a set of

controls appropriate for a given web browser type or version.

Abstract Factory DP (object– Creational DP)

Page 36: Design Pattern lecture 2

•Factories can be singletons

•Factory Method for each Product, each ConcreteFactory class overrides it choosing the type of products to create • a new concrete factory subclass for each

product family

• If many product families used Prototype DP

Abstract Factory DP (object– Creational DP)

Page 37: Design Pattern lecture 2

Builder DP (object– Creational DP)

Page 38: Design Pattern lecture 2

Builder DP (object– Creational DP)

•What• Separate Construction of complex objects

from the representation of its parts• Algorithm for creating a complex object

should be independent of the parts that make up the object and how they're assembled.

Page 39: Design Pattern lecture 2

Builder DP (object– Creational DP)

Page 40: Design Pattern lecture 2
Page 41: Design Pattern lecture 2

Builder DP (object– Creational DP)

Page 42: Design Pattern lecture 2

•Example• A Schedule Builder where there are

two types of Schedules(Inhouse or OnSite).

• A RTF Converter where there may be text or Images (convert to PDF or ASCII text or……)

Builder DP (object– Creational DP)

Page 43: Design Pattern lecture 2

Report #1:Prototype DP with code example

N.B. Hand Written