Programming Design Patterns

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

Creational Patterns (2) CS350/SE310 Fall, Lower the Cost of Maintenance Economic Goal Coupling-Cohesion, Open-Close, Information-Hiding, Dependency.
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related.
Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
Chapter 7 Testing Class Hierarchies. SWE 415 Chapter 7 2 Reading Assignment  John McGregor and David A. Sykes, A Practical Guide to Testing Object-Oriented.
More about classes and objects Classes in Visual Basic.NET.
Informatics 122 Software Design II Lecture 5 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Brittany Johnson CSCI360 Abstract Factory Design Pattern.
1 Software Testing and Quality Assurance Lecture 28 – Testing Class Hierarchies.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Inheritance using Java
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Setting up for TTD in Visual Studio 2012 Project | Manage NuGet Packages Select the online tab Search for Nunit Select the Nunit package Follow these instructions.
Creational Patterns (1) CS350, SE310, Fall, 2010.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
Software Components Creational Patterns.
Abstract Factory Abstract Factory using Factory Method.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
By James Sheets. An object creational pattern. Separates the construction of a complex object from its representation so that the same construction process.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
CS 210 Review Session October 5 th, Head First Design Patterns Chapter 4 Factory Pattern.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CS 415 N-Tier Application Development By Umair Ashraf June 22,2013 National University of Computer and Emerging Sciences Lecture # 3 Design Patterns (Observer,Factory,Singleton)
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Religious Studies 313 – Advanced Programming Topics.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
Advanced Object-oriented Design Patterns Creational Design Patterns.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
SE 461 Software Patterns. ABSTRACT FACTORY PATTERN.
SE 461 Software Patterns. FACTORY METHOD PATTERN.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Orientated Analysis, Design and Programming
Abstract Factory Pattern
Unit II-Chapter No. : 5- design Patterns
MPCS – Advanced java Programming
Low Budget Productions, LLC
Factory Patterns 1.
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Software Design and Architecture
Factory pattern Unit of Work
object oriented Principles of software design
Abstract Factory Pattern
The Object-Oriented Thought Process Chapter 08
Factory Method, Abstract Factory, and More
Intent (Thanks to Jim Fawcett for the slides)
Programming Design Patterns
Abstract Factory Pattern
Behavioral and Structural Patterns
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Introduction to Behavioral Patterns (2)
CS 350 – Software Design Principles and Strategies – Chapter 14
Object Oriented Design & Analysis
Design Patterns: A Place to get Design Ideas
Presentation transcript:

Programming Design Patterns Dependency Inversion Principle Design Principle Depend upon abstractions. Do not depend upon concrete classes. Stronger than “program to an interface, not an implementation”. High-level Components should NOT depend on low level components. Both should depend on abstractions. Last week: Factory method example Which one is the high level component Pizza Implementations (NYStyleCheezePizza) low level components What is the common abstraction? Homework Common abstraction is Pizza class Programming Design Patterns 11/13/2018 1 1

Programming Design Patterns Object dependency diagram Page 138: chapter 4 Page 140: chapter 4 Inversion in “Dependency inversion principle” The top-to-bottom dependency diagram in page 138 has now been inverted Common abstraction is Pizza class Programming Design Patterns 11/13/2018 2 2

Programming Design Patterns Guidelines for Dependency Inversion principle No variable should hold a reference to a concrete class Why? No class should derive from a concrete class No method should override an implementation of any of its base class Use factories instead of reference to a concrete class derive from abstract classes instead Do not override base class method, spoils the abstraction Programming Design Patterns 11/13/2018 3 3

Programming Design Patterns Guidelines for Dependency Inversion principle (2) No variable should hold a reference to a concrete class Sure, it is impossible to program this way Use factories whenever possible Ok to instantiate classes that will never change Example: String No class should derive from a concrete class Reduce dependency on a concrete class Derive from an abstraction (abstract interface) Common abstraction is Pizza class Programming Design Patterns 11/13/2018 4 4

Programming Design Patterns Guidelines for Dependency Inversion principle (3) No method should override an implementation of any of its base class methods If you need to override then the base class method wasn’t really an abstraction to start with. The idea of a base class method is to share the implementation with all the subclasses Common abstraction is Pizza class Programming Design Patterns 11/13/2018 5 5

Programming Design Patterns Builder Pattern Encapsulate construction of a product Enforce a series of steps Suppose an OOCar company builds two models of cars: OOCamry and OOCorolla The car manufacturing process needs to follow steps in this order: buildEngine, buildBody, buildExterior, buildInterior Write object oriented code to build these cars The driver code should not change if the manufacturing steps change Programming Design Patterns 11/13/2018 6 6

Programming Design Patterns OOCar Builder OOCar ooCamry = new OOCamry(v6engine, sedanBody, noLeatherInterior, blueColorExterior); // params are enums OOCar ooCorolla = new OOCorolla(...); // workshop constructs the car by putting // together the various parts in the right order OOWorkshopInterface shop = new OOWorkshop(); shop.construct(ooCamry); shop.construct(ooCorolla); Programming Design Patterns 11/13/2018 7 7

Programming Design Patterns Car Workshop public class OOWorkShop implements WorkshopInterface { // force the order of building process // Obviously, this is a dummy workshop! public void construct(OOCar cb) { cb.buildEngine(); hb.buildBody(); cb.buildInterior(); cb.buildExterior(); } Programming Design Patterns 11/13/2018 8 8

Programming Design Patterns Steps to build a car public abstract class OOCar { String carInfo; void setDescription(String carInfoIn); // the order of these methods is not important abstract public void buildInterior(); abstract public void buildExterior(); abstract public void buildEngine(); abstract public void buildBody(); } [Could use an interface instead of relying on inheritance] Programming Design Patterns 11/13/2018 9 9

Programming Design Patterns Build OOCamry public class OOCamry extends OOCar { public OOCamry(String description) { setDescription(description); } public void buildInterior() { // Camry specific } public void buildExterior(){ // Camry specific } public void buildEngine(){ // Camry specific } public void buildBody(){ // Camry specific } Programming Design Patterns 11/13/2018 10 10

Programming Design Patterns Build OOCorolla public class OOCorolla extends OOCar { public OOCamry(String description) { setDescription(description); } public void buildInterior() { // Corolla specific } public void buildExterior(){ // Corolla specific } public void buildEngine(){ // Corolla specific } public void buildBody(){ // Corolla specific } Programming Design Patterns 11/13/2018 11 11

Programming Design Patterns OOCar Builder OOCar ooCamry = new OOCamry(v6engine, sedanBody, noLeatherInterior, blueColorExterior); // params are enums OOCar ooCorolla = new OOCorolla(...); // workshop constructs the car by putting // together the various parts in the right order OOWorkshopInterface shop = new OOWorkshop(); shop.construct(ooCamry); shop.construct(ooCorolla); Programming Design Patterns 11/13/2018 12 12

Programming Design Patterns Builder Pattern Encapsulate the construction of a product and allow it to be constructed in steps Encapsulates the way a complex object is created Multi-step construction process Note that factory is a one step process Internal implementation hidden from client Client just sees the abstract interface, and so the underlying implementations can be changed Programming Design Patterns 11/13/2018 13 13

Programming Design Patterns Back to making Pizzas with factories Use factories to allow specialized pizzas to have their own ingredient variations example: fresh tomatoes/canned- tomatoes Programming Design Patterns 11/13/2018 14 14

Programming Design Patterns Original Pizza class public abstract class Pizza { String name, dough, sauce, cheese; void prepare() { … } void bake() { Force specializations to prepare() using their own ingredients. How can this be done? Programming Design Patterns 11/13/2018 15 15

Programming Design Patterns Pizza class: enforce use of specialized prepare() public abstract class Pizza { String name, dough, sauce; abstract void prepare(); void bake() { … } Programming Design Patterns 11/13/2018 16 16

Programming Design Patterns Pizza class: enforce use of factory to implement prepare() public abstract class Pizza { String name, dough, sauce; // Classwork: force use of special factories // to define the prepare method void prepare(); void bake() { … } Programming Design Patterns 11/13/2018 17 17

Programming Design Patterns Classwork: PizzaIngredientFactory Define an interface PizzaIngredientFactory Add a method to create each ingredient Idea is to have subclasses provide specialized implementations for ingredients Programming Design Patterns 11/13/2018 18 18

Programming Design Patterns Interface for ingredients public interface PizzaIngredientFactory { public Dough createDough(); public Sauce createSauce(); public Cheese createCheese(); public Veggies[] createVeggies(); } Programming Design Patterns 11/13/2018 19 19

Programming Design Patterns NYStylePizza’s ingredients public NYPizzaIngredientFactory implements PizzaIngredientFactory { public Sauce createSauce() { … } public Veggies[] createVeggies() { Programming Design Patterns 11/13/2018 20 20

Programming Design Patterns Cheese Pizza using factory (1) public class CheesePizza extends Pizza { // constructor should take an ingredient factory // the prepare method should create // specialized dough, sauce, cheese } Programming Design Patterns 11/13/2018 21 21

Programming Design Patterns Cheese Pizza using factory (2) public class CheesePizza extends Pizza { private PizzaIngredientFactory ingredientFactory; Private Dough dough; // rest of the ingredients public CheesePizza(PizzaIngredientFactory ingFactory) { ingredientFactory = ingFactory; } public void prepare(// parameterized if needed) { dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); Programming Design Patterns 11/13/2018 22 22

Programming Design Patterns Abstract Factory Pattern Provides an interface for creating families of related or dependent objects without specifying their concrete classes Variety of factories can be used for different contexts Decouples driver code from the actual factory that creates products Revisit PizzaIngredientFactory: it calls createDough(), createSauce(), etc. This is the family of related objects Each of these create() methods could use a simple factory Not surprisingly the create() call looks like a factory call Programming Design Patterns 11/13/2018 23 23

Programming Design Patterns Abstract Factory Pattern and Factory method patterns What are the similarities? What are the differences? Which one creates objects? Which one decouples client code from concrete objects? Which design principle is followed in them? Which is one is better for maintenance? Programming Design Patterns 11/13/2018 24 24

Programming Design Patterns Abstract Factory Pattern and Factory method patterns (2) Both create objects Both decouple client code from concrete types Factory Method uses inheritance Abstract Factory uses composition Provides interface to create a family of products Subclasses (those implementing the interface) decide how concrete objects are created Instantiate a factory and pass it to code written against the factory interface Programming Design Patterns 11/13/2018 25 25

Programming Design Patterns Abstract Factory Pattern and Factory method patterns (3) Abstract Factory: If you need to add new products Need to change the interface! Oh! Also, the interface is usually big (lots of methods) Factory Method Usually has just one method (orderPizza(…)) Implements code in the abstract creator that makes use of the concrete types the subclasses create Programming Design Patterns 11/13/2018 26 26