GoF Design Patterns (Ch. 26)

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Nov R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (section 23.3 has a Simple.
Design Patterns CS is not simply about programming
NJIT More GRASP Patterns Chapter 22 Applying UML and Patterns Craig Larman Prepared By: Krishnendu Banerjee.
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
NJIT Applying GOF Design Patterns Chapter 26 Applying UML and Patterns Craig Larman Presented By : Naga Venkata Neelam.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Larman, chapters 25 and 26 CSE432 Object-Oriented Software Engineering Glenn D. Blank, Lehigh University Pure Fabrication and “Gang of Four” Design Patterns.
Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements.
Design Patterns.
GRASP Principles. How to Design Objects The hard step: moving from analysis to design How to do it? –Design principles (Larman: “patterns”) – an attempt.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSSE 374: Introduction to Gang of Four Design Patterns
CSSE 374: 3½ Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Factory, Singleton & Strategy All References and Material From: Applying UML and Patterns, 3 rd ed, chpt 26 & 13 BTS530: Major Project Planning and Design.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Pure Fabrication and “Gang of Four” Design Patterns
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Oct R McFadyen1 Facade P Problem: There are a set of classes, a subsystem, that you need to interact with for some purpose, but you don’t.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
ECE450S – Software Engineering II
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CSC 480 Software Engineering Design With Patterns.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Software Engineering 1 Object-oriented Analysis and Design Chap 24 Iteration 2 More Patterns.
OO Methodology Elaboration Iteration 2 - Design Patterns -
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Elaboration: Iteration 2. Elaboration: Iteration 2 Basics Iteration 1 ends with : All the software has been tested: The idea in the UP is to do early,
Unit IV: GoF Design Patterns
Chapter 10 Design Patterns.
Presentation on GoF Design Patterns Submitted by WWW. ASSIGNMENTPOINT
Software Design Patterns
GoF Patterns (GoF) popo.
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Conception OBJET GRASP Patterns
Introduction to Design Patterns
Design Patterns with C# (and Food!)
Design Patterns (GoF) contains the creational patterns:
object oriented Principles of software design
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Presented by Igor Ivković
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
CSC 480 Software Engineering
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
GoF Design Patterns (Ch. 26)
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
CSC 480 Software Engineering
GoF Patterns Ch. 26.
Presentation transcript:

GoF Design Patterns (Ch. 26)

The “gang of four” (GoF) Design Patterns book catalogs 23 different patterns Solutions to different classes of problems, in C++ & Smalltalk Problems and solutions are broadly applicable, used by many people over many years Patterns suggest opportunities for reuse in analysis, design and programming GOF presents each pattern in a structured format What do you think of this format? Pros and cons?

Elements of Design Patterns Design patterns have 4 essential elements: Pattern name: increases vocabulary of designers Problem: intent, context, when to apply Solution: UML-like structure, abstract code Consequences: results and tradeoffs

Three Types of GoF Patterns Creational patterns: Deal with initializing and configuring objects Structural patterns: Composition of classes or objects Decouple interface and implementation of classes Behavioral patterns: Deal with dynamic interactions among societies of objects How they distribute responsibility

Creational Patterns (Singleton): Guarantee access to a singular (sole) instance Simple Factory: Create specialized, complex objects Abstract Factory: Create a family of specialized factories (Factory Method): Define an interface for creating an object, but let subclasses decide which class to instantiate Builder: Construct a complex object step by step Prototype: Clone new instances from a prototype Lazy initialization: Delay costly creation until it is needed

Structural Patterns (Adapter): (Bridge): Composite: Decorator: Converts interface of a class into one that clients expect (Bridge): Links abstraction with many possible implementations Composite: Represents part-whole hierarchies as tree structures Decorator: Attach additional responsibilities to object dynamically Facade: Simplifies the interface for a subsystem Flyweight: Shares many fine-grained objects efficiently Proxy: Provides a surrogate or placeholder for another object to control access to it

Behavioral Patterns (cont.) (Observer): Observers update automatically when observed object changes State: Object whose behavior depends on its state Strategy:***** Abstraction for selecting one of many algorithms Template Method: Algorithm with some steps supplied by derived class Visitor: Operations applied to elements of a heterogeneous object structure

GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)

Adapter Pattern (26.1) Problem: How to resolve incompatible interfaces, or how to provide a stable interface to similar components with different interfaces. Solution: Convert the original interface of a component into another interface, through an intermediate adapter object. Note: the Adapter pattern is an application of Polymorphism

Adapter Pattern (26.1) Example: POS needs to adapt several kinds of external third-party services: tax calculators, credit authorization services, inventory systems, accounting systems. Each has a different API which can’t be changed.

Fig. 26.1

Fig. 26.2

Adapter Pattern (26.1) Note: Adapter pattern follows GRASP principles: Polymorphism, Protected Variation, Indirection See Fig. 26.3 for conceptual connection among GRASP principles and Adapter pattern

Fig. 26.3

Factory Pattern (26.4) Problem: Who should be responsible for creating objects when there are special considerations such as complex creation logic, a desire to separate creation responsibilities for better cohesion, etc.? Solution: Create a Pure Fabrication object called a Factory that handles the creation.

Factory Pattern (26.4) Technically not a GoF pattern A variation of GoF Abstract Factory pattern

Fig. 26.5

Factory Pattern (26.4) Note: In Fig. 26.5 the implementation of ServicesFactory illustrates data-driven design – a form of Protected Variation

Factory Pattern (26.4) Idea: Define an object whose purpose is to create objects Benefits: Separate the responsibility of complex creation into cohesive helper objects Can provide object caching (e.g. having only one random number generator)

Singleton Pattern (26.5) Problem: Exactly one instance of a class is allowed. Objects need a global and single point of access. Solution: Define a static method of the class that returns the singleton: getInstance()

Singleton Pattern (26.5) Consider the factory and how it is accessed – who creates the factory? Only want one instance of the factory Methods may need to be called from various places => how to make single instance of the factory globally visible Could pass the ServicesFactory instance around as a parameter whenever visibility is required or initialize all objects that need it with a permanent reference to it Singleton – supports global visibility or a single access point to a single instance

Fig. 26.6

Singleton Pattern (26.5) Note: concurrency control in ServicesFactory – making getInstance() synchronized Fig. 26.8 shows how Adapter, Factory, Singleton patterns are used in design

Fig. 26.8

Strategy Pattern (26.7) Problem: How to design for varying but related algorithms or policies? How to design for the ability to change these algorithms or policies? Solution: Define each algorithm/strategy in a separate class with a common interface

Strategy Pattern (26.7) Example: How to provide more complex pricing logic, e.g. store-wide discount, senior citizen discount, employee discount, etc. A pricing strategy for a sale can vary, how do we design for these varying pricing algorithms?

Fig. 26.9

Strategy Pattern (26.7) Example: Create multiple SalePricingStrategy classes each with a polymorphic getTotal() operation Note: each getTotal() operation takes a Sale object as a parameter so that the strategy object can find the pre-discount price from the Sale The implementation of each getTotal() will differ A strategy object is attached to a context object – the object to which it applies the algorithm, e.g. Sale

Fig. 26.10

Strategy Pattern (26.7) Example: When a getTotal() message is sent to a Sale it delegates work to its strategy object It is common for the context object to pass a reference to itself to the strategy object so that the strategy object has parameter visibility to the context object

Fig. 26.11

Strategy Pattern (26.7) Example: Who should create the strategy? Apply the Factory pattern: a PricingStrategyFactory The PricingStrategyFactory is a singleton and accessed via the Singleton pattern

Fig. 26.12

Fig. 26.13

Strategy Pattern (26.7) Note: Strategy is based on Polymorphism and provides Protected Variation with respect to changing algorithms Strategies are often/usually created by a Factory

Composite Pattern (26.8) Problem: How to treat a group or composition structure of objects the same way (polymorphically) as a non-composite (atomic) object? Solution: Define classes for composite and atomic objects so that they implement the same interface.

Composite Pattern (26.8) Design problem: How to handle multiple conflicting pricing policies? Example: 20% senior discount Preferred customer discount of 15% off sales over $400 On Monday, there is a $50 off purchases over $500 Buy one case of Darjeeling tea, get 15% discount off everything

Composite Pattern (26.8) Example (cont.): Pricing strategies determined by Time period (Monday) Customer type (senior) Line item product (Darjeeling tea) In addition to pre-discount price May have multiple co-existing strategies Customer type and type of product may need to be known at time strategy is created (i.e. known by StrategyFactory)

Composite Pattern (26.8) Example (cont.): Now how do we design so that the Sale object does not know if it is dealing with one or many pricing strategies and also offer a design for the conflict resolution Composite pattern!

Composite Pattern (26.8) Key feature: The composite object contains a list of inner objects and both the composite object and the inner objects implement the same interface

Fig. 26.14

Composite Pattern (26.8) The Sale object treats a Composite Strategy that contains other strategies as any object that implements ISalePricingStrategy (i.e., calls its getTotal(s) operation) See code pp. 455-456 Notation for abstract classes and abstract methods – see Fig. 26.16

Fig. 26.16

Façade Pattern (26.9) Problem: A common unified interface to a disparate set of implementations or interfaces – such as within a subsystem – is required. There may be undesirable coupling to many things in the subsystem, or the implementation of the subsystem may change. What to do? Solution: Define a single point of contact to the subsystem – a façade that wraps the subsystem. This façade object presents a single unified interface and is responsible for collaborating with the subsystem components.

Façade Pattern (26.9) A façade object serves as a “front-end” object that is the single point of entry for the services of a subsystem Façade provides Protected Variation from changes in the implementation of the subsystem

Façade Pattern (26.9) Example: A “rule engine” subsystem – responsible for evaluating a set of rules against an operation and indicating if any rules invalidate the operation Example of a rule: If the sale is paid by a gift certificate, invalidate all payment types of change due back to the customer except for another gift certificate. Also known as pluggable business rules

Façade Pattern (26.9) Example (cont): Define a façade object to this subsystem: POSRuleEngineFacade Calls to this façade placed near the start of methods that are points for pluggable rules, see code p. 462 The hidden subsystem could contain any number of rules

Fig. 26.20

Façade Pattern (26.9) Notes: Façade is usually accessed via Singleton Similar to Adapter but Adapter applies to varying interfaces not hiding implementation of subsystem

Observer Pattern (26.10) Also known as Publish-Subscribe Pattern Problem: Different kinds of subscriber objects are interested in state changes or events of a publisher object and want to react in their own unique way when the publisher generates an event. The publisher wants to maintain low coupling to the subscribers. Solution: Define a subscriber or listener interface. Subscribers implement the interface. The publisher can dynamically register subscribers who are interested in an event and notify them when an event occurs.

Observer Pattern (26.10) Example: Want a GUI window to refresh (update) its display of sale total when the total changes See Fig. 26.21

Fig. 26.21

Observer Pattern (26.10) Example (cont): Simple solution – when Sale changes its total the object sends a message to the window telling it to refresh its display Problem – high coupling between domain objects and UI objects

Observer Pattern (26.10) Example (cont): Want to be able to easily replace UI objects or even add other UI objects that can be notified of this event That is, want model-view separation Model objects shouldn’t know about view objects => Protected Variations with respect to a changing user interface

Fig. 26.22

Observer Pattern (26.10) Steps (p. 465) Define an interface PropertyListener with operation onPropertyEvent Define window (SaleFrame1) to implement the interface When SaleFrame1 is initialized pass it the Sale instance from which it is displaying the total SaleFrame1 registers to the Sale instance for notification of “property events” via the addPropertyListener message Note that the Sale does not know about SaleFrame1 objects; it only knows about objects that implement the PropertyListener interface. (This lowers the coupling of the Sale to the window – the coupling is only to an interface, not to a GUI class.) The Sale instance is the publisher of “property events”. When the total changes, it iterates across all subscribing PropertyListeners, notifying each

Observer Pattern (26.10) Notes: The SaleFrame1 object is the observer/subscriber/listener Sale is the publisher of property events Sale adds SaleFrame1 object to its list of PropertyListener subscribers – Fig. 26.23

Fig. 26.23

Observer Pattern (26.10) Notes: When the Sale total changes it iterates over all registered subscribers and sends the onPropertyEvent message to each – Figs. 26.24, 26.25

Fig. 26.24

Fig. 26.25

Who is the observer, listener, subscriber, and publisher? Fig. 26.26 Who is the observer, listener, subscriber, and publisher?

Observer Pattern (26.10) Notes: Sale is coupled to view object but only loosely since it only knows subscribers as implementing the PropertyListener interface In Java this was called Delegation Event Model Observer pattern is basis for GUI widget event handling in both Java (AWT & Swing) and .NET Widgets are publishers of events, other objects can register as listeners

Observer Pattern (26.10) Example of AlarmClock: AlarmClock is publisher of alarm events Different types of objects can register as listeners and react to same alarm event in their own ways

Fig. 26.27