// create some behaviors SwimBehavior csb = new CircularSwimming(); QuackBehavior sqb = new StandardQuacking(); SwimBehavior rsb = new RandomFloating();

Slides:



Advertisements
Similar presentations
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance issues SE-2811 Dr. Mark L. Hornick 1.
1 CS2200 Software Development Polymorphism II A. O’Riordan, 2008 K. Brown,
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Abstract Factory Design Pattern making abstract things.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Dependency Injection Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 ©
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
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.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Systems Requirements SE 3821 Design? Algorithms CS 2852.
SE-2811 Software Component Design Week 1, Day 2 Making teams Warm-up exercise Design pattern defined SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
Systems Requirements SE 3821 Design? Algorithms CS 2852.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
OOP Basics Classes & Methods (c) IDMS/SQL News
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-2811 Software Component Design Week 1, Day 1 Design pattern defined Code that needs a design pattern… SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
Advanced Programming in Java
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
University of Central Florida COP 3330 Object Oriented Programming
Low Budget Productions, LLC
Week 2, Day 1: The Factory Method Pattern
Inheritance and Polymorphism
Software Design and Architecture
SE-2811 Software Component Design
SE-2811 Software Component Design
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Object-Oriented Programming
CS18000: Problem Solving and Object-Oriented Programming
SE-2811 Software Component Design
Java Inheritance.
14. Factory Pattern SE2811 Software Component Design
Chapter 14 Abstract Classes and Interfaces
14. Factory Pattern SE2811 Software Component Design
Chapter 5 Classes.
Presentation transcript:

// create some behaviors SwimBehavior csb = new CircularSwimming(); QuackBehavior sqb = new StandardQuacking(); SwimBehavior rsb = new RandomFloating(); // daffy has circular swimming, std quacking Waterfowl daffy = new Duck(“daffy”, csb, sqb); // donald has random floating, std quacking Waterfowl donald = new Duck(“donald”, rsb, sqb); daffy.swim(); donald.quack();

SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style); }

SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style)); }

SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style)); } But what happens when we have more behaviors?

SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style)); } But what happens when we have more behaviors? Operation? Class?

method solution

public Duck createDuck(Scanner in) { String swimmer = in.next(), quacker = in.next(), name = in.next(); SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; return new Duck(name, swim_style, quack_style); }

method solution Alternative: create a class DuckFactory public class DuckFactory { public Duck grow(String swim_type, String quack_type) { … }

method solution Alternative: create a class DuckFactory public class DuckFactory { public Duck grow(String swim_type, String quack_type) { … } Yes, just pushed problem into another object But, have one place to maintain duck creation Alternative: static methods – but can’t subclass

Creation is not done via constructor methods because constructor methods cannot be overridden The interface consists of Factory Methods

The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker)

There are two Products being built here by the USMoneyMint Factory – DollarCoinMaker and DollarBillMaker The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker) But the client still has to create the Factory (USMoneyMint) that creates the CurrencyMakers

There are two Products being built here by the USMoneyMint Factory – DollarCoinMaker and DollarBillMaker The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker) But the client still has to create the Factory (USMoneyMint) that creates the CurrencyMakers Why not move the if statement to the constructor?

There are two Products being built here by the USMoneyMint Factory – DollarCoinMaker and DollarBillMaker The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker) But the client still has to create the Factory (USMoneyMint) that creates the CurrencyMakers Why not move the if statement to the constructor? Constructor doesn’t “return” an object! It just initializes the current object

The products created by are sufficiently different to warrant separate Factories (USMoneyMint and CanadianMoneyMint), each of which “knows” which products to make The abstract factory (MoneyMint) defines the Factory Method implemented by the concrete factories, which is used by the client to create CurrencyMakers But the client still has to create the concrete Factories (USMoneyMint), but can refer to them abstractly (via MoneyMint references)

Here, the Abstract Factory (MoneyMint) exposes a static factory method (createMint) that can be called to create the concrete factories (the US and Canadian Mints). Client forced to use createMint since the concrete classes have protected constructors. The client app only has to reference the abstract classes in this configuration.