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

Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Making the Concrete Abstract

Factory Method, Abstract

Factory, and More

1

Page 2: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Open/Closed Principle

Yes, you can have it both ways

2

Brain surgery is not necessary when putting on a hat

Page 3: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

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

3

Page 4: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

OCP in Strategy

Also, a constructor that initially sets tires field.

Note that KiaSoul delegates to Tire

Open to infinity of tires without modification4

<<interface>>

_____Tire_____int size()

bool perf()

String

season()

_MichelinPilot_

_DunlopSP_

……

_____KiaSoul_____

Tire tires_________int tireSize()

bool tirePerf()

String tireSeason()

Page 5: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

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 modification5

composes

abstract type

<− inherits abs. type

Page 6: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

OCP for Decorator

Open to addition of new drinks and condiments without modification to support all combinations

6

Composition of

abstract types

Decorator

delegates to

abstract

superclass

Inheritance from

abstract types

Page 7: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Does this diagram indicate OCP?

A. Yes (and why?)

B. No (why not?)

7

Page 8: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

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.

8

Page 9: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

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 will9

Page 10: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Factory is the answer

Actually a collection of patterns

Simply factory (idiom)

Factory Method

Abstract Factory (not today)

10

Concrete

class;

Mystery

how to

make one

Page 11: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

2¢ Factory for Dating Events

11

static “Factory”

methods keep

Event details

local

Page 12: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

What’s wrong with 2¢ Factory?

A. Doesn’t hide construction details adequately

B. Should pass an enum argument rather than

defining multiple methods:

Event.makeEvent(SEEMOVIE)

C. Violates the Open/Closed principle

D. “make” methods shouldn’t be static

E. Name prefix should be “create”, not “make”

12

Page 13: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Simple Factory (idiom)

13

How to make

Events not

necessarily part

of Event

Page 14: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

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);

14

Page 15: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Class Diagram for Simple Factory

15

Page 16: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

What’s wrong with Simple Factory?

A. create method has to know about all the

concrete classes it new’s

B. Client class composes concrete class

C. Factory is a concrete class

16

Page 17: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

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 EventFactoryinterface, 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!

17

Page 18: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Factory Method Style: Date as Factory

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. 18

____Date____

createEvent()

____Youth____

createEvent()

____Senior___

createEvent()…

Page 19: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Factory Method Style: Separate Factory

Separate Factory like SEF but in

hierarchy with abstract super

Super centralizes Event selector

Subs implement making19

Page 20: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Factory Motivation for Pizza Example:

Franchising

20

Page 21: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Factory Method Class Diagram

21

“composes” them all

because their

constructors are

referenced

Page 22: Factory Method, Abstract Factory, and Morecseweb.ucsd.edu/classes/fa16/cse110-a/applications/... · Factory Pattern: Motivation Correctly making objects is complex Especially making

Object Relationships

22