Chapter 26 GoF Design Patterns. The Adapter Design Pattern.

Slides:



Advertisements
Similar presentations
Object Design Examples with GRASP
Advertisements

Oct Ron McFadyen Visibility Visibility: the ability of one object to see or have a reference to another object. e.g. When a register object.
Oct R McFadyen1 Recall UML Class Diagram BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..*
Feb R. McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m Function:
Nov R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (section 23.3 has a Simple.
Chapter 6: Using Design Patterns
March R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (in Larman) Prototype Singleton.
Chapter 15 Interaction Diagrams. Most Common Sequence Diagram Communication Diagram Sequence Diagrams illustrate interactions between classes of a program.
February Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Pronunciation: -"fi-z&m.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
Fall 2009ACS-3913 R McFadyen1 Singleton Problem: Exactly one instance of a certain object is required (this object is called a singleton). We must ensure.
NJIT Applying GOF Design Patterns Chapter 26 Applying UML and Patterns Craig Larman Presented By : Naga Venkata Neelam.
Object-Oriented Analysis and Design
Fall 2009ACS-3913 Ron McFadyen1 From the Merriam-Webster’s online dictionary ( Main Entry: an·thro·po·mor·phism Date: 1753 an interpretation.
March R McFadyen1 Figure 30.2 Layers in NextGen They only have three layers in this architecture Each layer is shown as a UML Package No separate.
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.
TK2023 Object-Oriented Software Engineering CHAPTER 6 SYSTEM SEQUENCE DIAGRAMS.
Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative Development Part III Elaboration Iteration I – Basic1.
GRASP Design Patterns: Designing Objects with Responsibilities
GRASP Principles. How to Design Objects The hard step: moving from analysis to design How to do it? –Design principles (Larman: “patterns”) – an attempt.
GRASP Patterns Presented By Dr. Shazzad Hosain. Patterns A pattern describes a problem and solution, and given a name. Examples are Singleton, Adapter,
Chapter 18 Object Design Examples with GRASP. Objectives Design use case realizations –A use-case realization describes how a particular use case is realized.
Chapter 17. GRASP General Responsibility Assignment Software Patterns (Principles) OOD: after identifying requirements, create domain model, define responsiblities.
1 Chapter 17 GRASP Design Patterns: Designing Objects with Responsibilities.
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.
Chapter 7: Object Design Examples with GRASP. Objective Design use case realizations. Apply GRASP to assign responsibilities to classes. Apply UML to.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
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.
GRASP: Designing Objects With Responsibilities
Object Design Examples with GRASP (Ch. 18)
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Chapter 17 GRASP: Designing Objects with Responsibilities. 1CS6359 Fall 2011 John Cole.
Object-Oriented Design Part 2
Next Gen POS Example GRASP again. Same Patterns Different Example!
Chapter 4: UML Interaction Diagrams. Objective Provide a reference for frequently used UML interaction diagram notation- sequence and communication diagrams.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Chapter 36 More Object Design with GoF Patterns 1CS6359 Fall 2011 John Cole.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Elaboration Iteration 3 – Part 3 - Persistence Framework -
GRASP: Designing Objects With Responsibilities
OO Methodology Elaboration Phase Iteration 1- Part 3.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
The Singleton Pattern (Creational)
Oct 3, Ron McFadyen1 GRASP Patterns 1.Expert 2.Creator 3.Controller 4.Low Coupling 5.High Cohesion.
OO Methodology Elaboration Iteration 3 – Part 3 - More Design Patterns -
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
OO Methodology Elaboration Phase Iteration 1- Part 2.
Unit IV: GoF Design Patterns
Object Design Examples with GRASP
Presentation on GoF Design Patterns Submitted by WWW. ASSIGNMENTPOINT
GoF Patterns (GoF) popo.
MPCS – Advanced java Programming
Conception OBJET GRASP Patterns
Singleton Pattern Command Pattern
Design Patterns (GoF) contains the creational patterns:
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
GoF Design Patterns (Ch. 26)
GRASP Design Patterns: Designing Objects with Responsibilities
Singleton Pattern Pattern Name: Singleton Pattern Context
GoF Design Patterns (Ch. 26)
5. Strategy, Singleton Patterns
Next Gen POS Example GRASP again.
GoF Patterns Ch. 26.
Presentation transcript:

Chapter 26 GoF Design Patterns

The Adapter Design Pattern

Interaction Diagram for “Adapter”

Sometimes we update domain models to reflect our more refined understanding of domain

The Factory Pattern

Advantages: –Separate the responsibility of complex object creation into cohesive helper objects –Hide potentially complex creation logic –Allow optimizations to enhance performance Object caching: Create objects beforehand and keep them in memory for quick use. Re-cycling: Pool of connection threads in a web-server. ServicesFactory: –Data-driven design: Class loaded dynamically (at run time) based on system property. –Can create other adapter classes by changing property value

The Singleton Pattern Issues with the factory pattern: –Who creates the factory itself? –How do we get access to the factory class from everywhere?

Accessing the Singleton public class Register { public void initialize() {... aa = ServicesFactory.getInstance().getAccountingAdapter();... }

The Singleton Pattern: Implementation and Design Issues Lazy initialization: public static synchronized ServicesFactory getInstance() { if (instance == null) instance = new ServicesFactory(); return instance; } Eager initialization: ???

The Singleton Pattern: Implementation and Design Issues Lazy initialization: public static synchronized ServicesFactory getInstance() { if (instance == null) instance = new ServicesFactory(); return instance; } Eager initialization: public class ServicesFactory { private static ServicesFactory instance = new ServicesFactory(); public static ServicesFactory getInstance() { return instance; }

Singleton Issues Lazy vs. eager initialization. Which one is better? –Laziness, of course! Creation work (possibly holding on to expensive resources) avoided if instance never created Why not make all the service methods static methods of the class itself? –Why do we need an instance of the factory object and then call its instance methods?

Singleton Issues Why not make all the service methods static methods of the class itself? –To permit subclassing: Static methods are not polymorphic, don’t permit overriding. –Object-oriented remote communication mechanisms (e.g. Java RMI) only work with instance methods Static methods are not remote-enabled. –More flexibility: Maybe we’ll change our minds and won’t want a singleton any more.

Design Patterns in Interaction Diagrams

The Strategy Pattern Issue: Provide more complex pricing logic. –Pricing strategy varying over time –Example: Different kinds of sales.

Interaction diagram for “Strategy” Context object

Context object has attribute visibility to its strategy

Factory for Strategy Object

Factory Creates Strategy Object on Demand

The “Composite” Design Pattern

public abstract class CompositePricingStrategy implements ISalePricingStrategy { protected List strategies = new ArrayList(); public add (ISalePricingStrategy s) { strategies.add(s); } public abstract Money getTotal( Sale sale ); } public class ComputeBestForCustomerPricingStrategy extends CompositePricingStrategy { Money lowestTotal = new Money( Integer.MAX_VALUE ); for (Iterator i = strategies.iterator(); i.hasNext(); ) { ISalePricingStrategy strategy = (ISalePricingStrategy) i.next(); Money total = strategy.getTotal( sale ); lowestTotal = total.min( lowestTotal); } return lowestTotal; }

Collaboration with a Composite

Abstract Classes and Methods in the UML

Creating a Composite Strategy

Creating the Pricing Strategy for a Customer (Part 1)

Creating the Pricing Strategy for a Customer (Part 2)

WHY?

The “Façade” Design Pattern Additional requirement in new iteration of POS: Pluggable business rules Example –New sale might be paid by gift certificate. Only one item can be purchased by a gift certificate –If sale paid by gift certificate, invalidate all payments with the type of “change due back to customer” different from gift certificate. –Some sales might be charitable donations by the store. Limited to less than $250 each. Manager must be logged in as cashier for this transaction. One of the concerns: What happens to enterItem?

enterItem and Low Impact of Change Suppose architect wants low impact of change in pluggable business rules. Suppose also that the architect is not sure how to implement the pluggable business rules. –Wants to experiment with different ways of implementing them. Solution: The “Façade” design pattern –Problem: Need a common, unified interface to a disparate set of implementations or interfaces –Solution: Define a single point of contact to the subsystem containing the implementations This façade object has a unified interface Internal implementation details hidden –Example: The use-case controller objects in your project.

The “Façade” Design Pattern

Façade code example public class Sale { public void makeLineItem( ProductDescription desc, int quantity) { SalesLineItem sli = new SalesLineItem( desc, quantity); // call to the Façade. Notice Singleton pattern if (POSRuleEngineFacede.getInstance().isInvalid(sli,this) ) return; lineItems.add(sli); } //.. }

Observer/Publish-Subscribe/Delegation Event Model Another requirement: GUI window refreshes its display of the sales total when the total changes –Later: GUI updates display when other data changes as well What’s wrong with the following? –When the Sale object changes its total, it sends a message to a window, asking it to refresh the display?

Publish-Subscribe Pattern What’s wrong with the following? –When the Sale object changes its total, it sends a message to a window, asking it to refresh the display? Answer: The Model-View Separation principle discourages such solutions –Model objects (objects in the domain) should not know of UI objects –If UI changes, model objects should not need to change

The Problem

Fig

Fig

Fig

Fig

Fig