Upload
julie-iskander
View
823
Download
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
Design PatternBy Julie Iskander
MSc. Communication and Electronics
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
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.
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.
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
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)”.
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
“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
Instead of code reuse, with design patterns you get experience reuse
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
Design Pattern elements
•Pattern Name
•Problem : When to apply? Problem and Context
•Solution
•Consequence : results, trade offs of applying it
Model-View-ControllerMVC
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.
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.
Design Patterns according to the
GOF
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
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
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
Creational Patterns
Singleton(object – creational dp)
Singleton (object – Creational DP)
•What, Why• A class has one and only one instance
created
•How• private constructor
Singleton (object – Creational DP)
•Notes:•Can permit a variable # of
instances
Singleton (object – Creational DP)
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.
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.
Factory method (Class – Creational DP)
•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)
Factory method (Class – Creational DP)
•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)
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 .
Abstract Factory DP (object– Creational DP)
Abstract Factory DP (object– Creational DP)
Abstract Factory DP (object– Creational DP)
•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)
•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)
Builder DP (object– Creational DP)
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.
Builder DP (object– Creational DP)
Builder DP (object– Creational DP)
•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)
Report #1:Prototype DP with code example
N.B. Hand Written