Introduction to Software Engineering · The Factory Method Design Pattern Implementation -...

17
Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of Computer Science Technische Universität Darmstadt

Transcript of Introduction to Software Engineering · The Factory Method Design Pattern Implementation -...

Page 1: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

Introduction to Software Engineering (2+1 SWS)Winter Term 2009 / 2010 Dr. Michael EichbergVertretungsprofessur Software EngineeringDepartment of Computer ScienceTechnische Universität Darmstadt

Page 2: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

Dr. Michael EichbergFachgebiet Software EngineeringDepartment of Computer ScienceTechnische Universität Darmstadt

The Factory MethodDesign PatternFor details see Gamma et al. in “Design Patterns”

Page 3: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternExample / Motivation

3

Let’s assume we want to develop a framework for applications that can present multiple documents to the user (MDI style).

We want to support a wide variety of applications:▶ Text editors ▶ Word processors▶ Vector drawing applications▶ Document Viewers▶ ...

Our framework should - in particular - be able to manage the documents.

Page 4: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternExample / Motivation - Common functionality for handling documents

4

TextMate Nisus Writer Pro

Page 5: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternExample / Motivation - Common functionality for handling documents

5

TextMateNisus Writer Pro

(In the following, we focus on the implementation of “New”.)

Page 6: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternExample / Motivation - A Possible Implementation of the Framework

6

public abstract class Document { public abstract void open(); public abstract void close();}

public abstract class Application { private List<Document> docs = new ArrayList<Document>(); public void newDocument() { Document doc = createDocument(); // the framework manages the documents docs.add(doc); doc.open(); } ... public abstract Document createDocument(); // factory method}

Page 7: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternExample / Motivation - Implementation of an Application Using the Framework

7

public class TextDocument extends Document { … // implementation of the abstract methods}

public class MyApplication extends Application {

public Document createDocument() { return new TextDocument(); }

}

Page 8: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternExample / Motivation - Class Diagram of an Application Using the Framework

8

open()close()save()

Document

TextDocument

createDocument()newDocument()

Application

createDocument()

MyApplication

docs

Page 9: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternIntent

9

Define an interface for creating an object, but let subclasses decide which class to instantiate. (Factory Method lets a class defer instantiation to subclasses.)

Page 10: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternStructure

10

Product

ConcreteProduct

factoryMethod()anOperation()

Creator

factoryMethod() ConcreteCreator

«method»... factoryMethod()...

Page 11: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternParticipants

11

▶ Product… defines the interface of objects the factory method creates.

▶ ConcreteProduct… implements the Product interface.

▶ Creator… declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.

▶ ConcreteCreator… overrides the factory method to return an instance of a ConcreteProduct.

Page 12: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternConsequences (I)

12

▶ The framework’s code only deals with the Product interface; therefore it can work with any user-defined ConcreteProduct class.

▶!Provides a hook for subclassesThe hook can be used for providing an extended version of an object.

Page 13: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternConsequences (II)

13

▶ Connects parallel class hierarchiesClass Name

Collaborator ACollaborator B

Responsibility A

Responsibility B

Responsibility C

drag()...

Manipulator

LineManipulator

createManipulator()

Figure

TextManipulator

Client

createManipulator()

Text

createManipulator()

Line

Page 14: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternImplementation

14

Two major variants:▶ Creator is abstract▶ Creator is concrete and provides a reasonable default

implementation

Page 15: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternImplementation - Parameterized factory methods

15

(E.g. imagine an document previewer which can handle very different types of documents.)

General form:public abstract class Creator { public abstract Product createProduct(ProductId pid);}

Applied to our example:public abstract class Application { public abstract Document createDocument(Type e);}public class MyApplication extends Application { public Document createDocument(Type e){ switch(e) { case Type.JPEG : return new JPEGDocument(); case Type.PDF : return new PDFDocument(); } }}

Page 16: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|The GoF Design Patterns

The Factory Method Design PatternImplementation - Parameterized factory methods

16

public abstract class Application {

private Class<? extends Document> clazz;

public Application(Class<? extends Document> clazz){ this.clazz = clazz; }

public abstract Document createDocument(){ return clazz.newInstance(); }}

It is possible to use Java reflection in a type safe way.

Page 17: Introduction to Software Engineering · The Factory Method Design Pattern Implementation - Parameterized factory methods 15 (E.g. imagine an document previewer which can handle very

|Placeholder

The Factory Method Design PatternRelated Patterns

• Factory Methods are usually called within Template Methods.•Abstract Factory is often implemented with factory

methods.

17