Factory Method, Abstract Factory, and More

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Reusable Classes.  Motivation: Write less code!
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Object Oriented Software Development
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Design Patterns. OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Object Oriented Programming
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Decorator Pattern ¢ Design: Which classes must be included?  Story As a user I want a weekend planner so that I can have more fun  Scenario Given.
Making the Concrete Abstract Adapter, Factory, and More 1.
Façade and the DP Wrap-Up 1. Which pattern does this class diagram from the Factory chapter call out for? A.Strategy B.Decorator C.Adapter D.Factory 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Introduction to Software Design
Design Patterns: MORE Examples
Sections Inheritance and Abstract Classes
Abstract Factory Pattern
MPCS – Advanced java Programming
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Low Budget Productions, LLC
Week 2, Day 1: The Factory Method Pattern
Factory Patterns 1.
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Behavioral Design Patterns
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
object oriented Principles of software design
Abstract Factory Pattern
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Programming Design Patterns
Chapter 9 Inheritance and Polymorphism
Decorator Pattern Richard Gesick.
Advanced Java Programming
Review: Design Pattern Structure
OO Design with Inheritance
Java Programming, Second Edition
Java Inheritance.
Structural Patterns: Adapter and Bridge
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Adapter Design Pattern
Object Oriented Analysis and Design
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
European conference.
Object Oriented Design & Analysis
Presentation transcript:

Factory Method, Abstract Factory, and More Making the Concrete Abstract

Open/Closed Principle Brain surgery is not necessary when putting on a hat Yes, you can have it both ways

Open/Closed Principle (OCP) Classes should be open to extension, but closed to modification Extension Composition of abstract types, not concrete classes Constructors and setters also take those abstract types Composer delegates to the composed Inheritance from those abstract classes and interfaces Modification Editing – allowed for fixing bugs, but not adding behavior Danger: propagation of changes to numerous dependents (subclasses, aggregators) Costly invalidation of test cases (have to rewrite) “Closure” is really creating openings for extension There is no “close” operation, but can use version control

OCP in Strategy Also, a constructor that initially sets tires field. Note that KiaSoul delegates to Tire Open to infinity of tires without modification <<interface>> _____Tire_____ int size() bool perf() String season() _MichelinPilot_ … _DunlopSP_ … … _____KiaSoul_____ Tire tires_________ int tireSize() bool tirePerf() String tireSeason()

OCP for Observer Also, Display constructor that takes Subject, and Subject register method that takes Observer Subject delegates to the Observer Open to adding observers without modification composes abstract type <− inherits abs. type

OCP for Decorator Open to addition of new drinks and condiments without modification to support all combinations Inheritance from abstract types Composition of abstract types Decorator delegates to abstract superclass

Does this diagram indicate OCP? Yes (and why?) No (why not?)

Discussion Best answer: A. Because Client composes an abstract interface, it can accept any implementation of Target. Thus, it is possible to add new Adapter/Adaptee pairs to the design without modifying any existing classes. Not B. Some people noted that Adapter, a concrete class refers to a concrete class. But that is not the part of the design we need “open”. I can always code new Adapter/Adaptee pairs, if necessary.

Factory Pattern: Motivation Correctly making objects is complex Especially making collection of related objects Parts of a car Look-and-feel of a window: canvas, scrollbar, etc. The correct making of objects is not easily centralized in one place Often do it all over code wherever object is needed Violates SRP and DRY DP angle is that “new” names and makes a concrete class; should be referring to abstract concrete class has to be named, but we can at least hide that from most of system encapsulating class will violate OCP, but no others will

Factory is the answer Actually a collection of patterns Simply factory (idiom) Factory Method Abstract Factory (not today) Concrete class; Mystery how to make one

2¢ Factory for Dating Events … static “Factory” methods keep Event details local

What’s wrong with 2¢ Factory? Doesn’t hide construction details adequately Should pass an enum argument rather than defining multiple methods: Event.makeEvent(SEEMOVIE) Violates the Open/Closed principle “make” methods shouldn’t be static Name prefix should be “create”, not “make” C: No delegation to an abstract class. No CLASS. And geez, the methods are STATIC.

Simple Factory (idiom) How to make Events not necessarily part of Event

Comparison – increasingly abstract class MyDatingClass { … Event event = new Event(2, “OrderFlowers”); // magic constants // concrete Event class (majorly violates OCP) Event event = Event.makeOrderFlowers(); // abstract class object (passed into constructor) Event event = factory.createEvent(FLOWERS);

Class Diagram for Simple Factory

What’s wrong with Simple Factory? create method has to know about all the concrete classes it new’s Client class composes concrete class Factory is a concrete class A: sure, but unavoidable B: Yes: SPF should implement SPF Interface, PS should compose it C: No: The factory itself has to be concrete so it can be instantiated and its methods called

Simple Factory is Good, but not OCP SimpleEventFactory should implement EventFactory interface Clients of SEF should then compose EventFactory: class DateClass { private EventFactory factory; public DateClass(EventFactory factory) { this.factory = factory; } … Someone can implement a new class with EventFactory interface, and DateClass can use it with no modification Means that when EventFactory is extended (new subclass), DateClass is extended with new EventFactory options. That’s open for extension!

Factory Method Style: Date as Factory ____Date____ createEvent() ____Youth____ createEvent() ____Senior___ createEvent() … Subclasses of Date are the factories! In this example, no parallel Date/Event class hierarchy Event hierarchy is “flat” – class hierarchy is implicit in data of Event What changes is time to first “flowers” event, what the flowers are, etc.

Factory Method Style: Separate Factory Separate Factory like SEF but in hierarchy with abstract super Super centralizes Event selector Subs implement making

Factory Motivation for Pizza Example: Franchising

Factory Method Class Diagram “composes” them all because their constructors are referenced

Object Relationships