CSSE 374: Introduction to Gang of Four Design Patterns

Slides:



Advertisements
Similar presentations
Copyright W. Howden1 Singleton, Wrapper, and Facade Patterns CSE 111.
Advertisements

18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Nov R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (section 23.3 has a Simple.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
March R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (in Larman) Prototype Singleton.
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Fall 2009ACS-3913 Ron McFadyen1 idea was first put forth by Christopher Alexander (1977) in his work on architectural design principles a pattern is a.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.
Larman, chapters 25 and 26 CSE432 Object-Oriented Software Engineering Glenn D. Blank, Lehigh University Pure Fabrication and “Gang of Four” Design Patterns.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Software Waterfall Life Cycle Requirements Construction Design Testing Delivery and Installation Operations and Maintenance Concept Exploration Prototype.
CSSE 374: Final Perspectives on Software Architecture and Design These slides derived from Shawn Bohner, Curt Clifton, Alex Lo, and others involved in.
樣式導向設計 (Pattern-Oriented Design) 課程簡介 Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
CSSE 374: More Object Design with Gang of Four Design Patterns Steve Chenoweth Office: Moench Room F220 Phone: (812)
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
CSSE 374: 3½ Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Pure Fabrication and “Gang of Four” Design Patterns
CSSE 374: More GRASP’ing for Object Responsibilities
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
Creational Patterns
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns Spring 2017.
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Presentation on GoF Design Patterns Submitted by WWW. ASSIGNMENTPOINT
樣式導向設計 (Pattern-Oriented Design) 課程簡介
GoF Patterns (GoF) popo.
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
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.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
GoF Design Patterns (Ch. 26)
Informatics 122 Software Design II
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
DESIGN PATTERNS : Introduction
GoF Design Patterns (Ch. 26)
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
GoF Patterns Ch. 26.
Presentation transcript:

CSSE 374: Introduction to Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374. CSSE 374: Introduction to Gang of Four Design Patterns Q1.True or False (circle one): A design pattern often combines multiple GRASP principles. Q1

Learning Outcomes: Patterns, Tradeoffs Identify criteria for the design of a software system and select patterns, create frameworks, and partition software to satisfy the inherent trade-offs. First Design Studios this week Introduce Gang of Four Concepts Describe and use GoF Patterns Adapter Factory Singleton Strategy NOTE: New approach to Design Studio… Q3

So, why bother to learn design patterns? Think for 15 seconds… Turn to a neighbor and discuss it for a minute - communicating with other developers - quickly moving to a principled solution

Gang of Four (GoF) http://www.research.ibm.com/designpatterns/pubs/ddj-eip-award.htm Ralph Johnson, Richard Helm, Erich Gamma, and John Vlissides (left to right)

Gang of Four Design Patterns Behavioral Interpreter Template Method Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Creational Factory Method Abstract Factory Builder Prototype Singleton Structural Adapter Bridge Composite Decorator Façade Flyweight Proxy Q2: What are the three main categories of Gang of Four Design Patterns? Q2

Adapter: Structural Pattern Problem: How do we provide a single, stable interface to similar components with different interfaces? How do we resolve incompatible interfaces? Solution: Use an intermediate adapter object to convert calls to the appropriate interface for each component One of the most useful and used GoF patterns… Q3: With what sorts of variations does Adapter help us deal? Hide the incompatible/unstable interface behind the adapter’s interface Client collaborates with stable adapter Adapter relays messages to unstable interface Uses Protected Variations, Polymorphism, Indirection 1. Adapter Q3

Guideline: Use pattern names in type names Adapter Examples Guideline: Use pattern names in type names Larman’s lousy example does not show adapting outside code Q4: True or False (circle one): Adaptors use interfaces and polymorphism to add a level of indirection to varying APIs in other components. Working with your project team, identify a situation in your project where Adapter might be applicable. If no such situation exists, try to come up with an extension to your system (perhaps an external system?) that might use Adapter. Clients send message to Adapter; Enables Plug n’ Play with respect to 3rd party software! Notice adherence to good naming conventions 1. Adapter Q4

Example of Adapter 1. Adapter Register posts the sale to an Adapter Adapter communicates with the SAP accounting system SAP accounting system exposes functionality as a web service 1. Adapter

Which GRASP Principles in Adapter? Low coupling High cohesion Information Expert Creator Controller Polymorphism Pure Fabrication Indirection Protected Variations Get students to argue whether or not adapter embodies each of these principles. Q5: Which GRASP principles are embodied in Adapter (check all that apply)? 1. Adapter Q5

GoF Adapter mapped to GRASP Q5, cntd: Which GRASP principles are embodied in Adapter (check all that apply)? 1. Adapter Q5

Factory: Creational Pattern Problem: Who should be responsible for creating objects when there are special considerations like: Complex creation logic Separating creation to improve cohesion A need for caching Solution: Create a “Pure Fabrication” called a “Factory” to handle the creation Software factory humor… Simplification of Abstract Factory Also known as Simple Factory or Concrete Factory 2. Factory Cartoon from www.cartoonbank.com. All rights reserved.

Factory Example 2. Factory ServicesFactory creates adapter objects for many 3rd party services Each get.???.Adapter() method checks to make sure no instance already exists The concrete adapter class is read from an external property file Class.forName(..) returns a reference to a class given its name:string First call to get.???.Adapter() creates adapter, subsequent calls access it 2. Factory

Abstract Factory: Creational Pattern Provides an interface to create and return one of several families of related objects without needing to specify their concrete classes. Assign creation responsibility to a pure fabrication Factory object Commonly implemented via the Singleton pattern 2. Factory

Another Factory Example From JDK 1.4… This is just an example of how the patterns terminology is pervasive. 2. Factory

Advantages of Factory Puts responsibility of creation logic into a separate, cohesive class —separation of concerns Hides complex creation logic Allows performance enhancements: Object caching Recycling Q6: List a couple of advantages of using a Factory object versus just distributing the logic for deciding what objects to create. 2. Factory Q6

Who creates the Factory? Several classes need to access Factory methods Options: Pass instance of Factory to classes that need it Provide global visibility to a Factory instance Dependency Injection •• call-outs Singleton 2. Factory

Singleton: Creational Pattern Problem: How do we ensure that exactly one instance of a class is created and is globally accessible? Solution: Define a static method in the class that returns the singleton instance Created only once for the life of the program (a non-creational pattern?) Provides single global point of access to instance Similar to a static or global variable variable Problem: Exactly one instance of a class is needed/allowed Other objects need single, global point of access to it Solution: Define a static method of a class that returns the Singleton The static method can only create one instance Issues Who should create the Factory? The Singleton! Provides global visibility via static method Avoids passing factory reference to many clients 3. Singleton

Singleton Example 3. Singleton All clients have global visibility to static method getInstance() First call to getInstance() creates a ServicesFactory singleton Subsequent calls just access the singleton 3. Singleton

Lazy vs. Eager Initialization private static ServicesFactory instance; public static synchronized Services Factory getInstance() { if (instance == null) instance = new ServicesFactory(); return instance; } Eager: private static ServicesFactory instance = new ServicesFactory(); public static ServicesFactory getInstance() { return instance; } • build in eager version Lazy initialization: - saves resources by only creating if needed - allows for more complex initialization logic Eager: - simple, little or no conditional logic Pros and cons? 3. Singleton

Why don’t we just make all the methods static? Instance methods permit subclassing Instance method allow easier migration to “multi-ton” status Can swap singleton implementations at runtime (ie: testing, different applications) Q7: Please list a couple of reasons why we use instance methods in a singleton, instead of just declaring static methods. 3. Singleton Q7

Singleton Considered Harmful? Favor Dependency Injection Hides dependencies by introducing global visibility Hard to test since it introduces global state (also leaks resources) A singleton today is a multi-ton tomorrow Responsibilities get heaped on it because it is convenient to get at Low cohesion — class is responsible for domain duties and for limiting number of instances Singleton is probably the most overused pattern. Many consider Singleton a bad code smell. Q8: List four ways that using singleton can lead to problems: •••••• Animated bullets and call-outs Instead, use Factory to control instance creation http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx http://tech.puredanger.com/2007/07/03/pattern-hate-singleton/ 3. Singleton Q8

Strategy: Behavior Pattern Problem: How do we design for varying, but related, algorithms or policies? How do we design for the ability to change these? Solution: Define each algorithm or policy in a separate class with a common interface. Define a family of algorithms that are interchangeable and vary independently from the client that uses them Q9: With what sorts of variations does Strategy help us deal? 4. Strategy Q9

Strategy Example 4. Strategy pdt = s.getPreDiscountTotal(); if (pdt < threshold) return pdt; else return pdt - discount; Context Object Each pricing strategy subclass implements a different pricing algorithm All implement the getTotal(..) method Strategy object attached to a context object (Sale, in this case) •• code for method bodies return s.getPreDiscountTotal() * percentage; 4. Strategy

Strategy Example (cont.) Client tells Sale to getTotal(), Sale gets subtotals from all SaleLineItems Sale passes ‘this’ reference to its strategy object so strategy can obtain needed data from Sale, its context object 4. Strategy

Where does the PricingStrategy come from? Q9: Where does the PricingStrategy come from? [when a Sale is created, it requests the PricingStrategy from PricingStrategyFactory] • build in sequence diagram 4. Strategy Q9

Examples of Change and Patterns What Varies Design Pattern Algorithms Strategy, Visitor Actions Command Implementations Bridge Response to change Observer Interactions between objects Mediator Object being created Factory Method, Abstract Factory, Prototype Structure being created Builder Traversal Algorithm Iterator Object interfaces Adapter Object behavior Decorator, State

Working for Google 2. Factory • secondary punchline • arrow pointing at “factory” I hear once you've worked there for 256 days they teach you the secret of levitation.

+/∂ Feedback We’ll do in class. On a separate piece of paper – Please give us some team-level-anonymous feedback! Possible topics – Class Quizzes Homework Book Projects Schedule … Anything else + = things going well ∂ = things to fix Thanks – We’ll show results next class!