Docsity

The Factory Method and Abstract Factory Design Patterns in Software Engineering, Study notes of Design

Partial preview of the text

Download The Factory Method and Abstract Factory Design Patterns in Software Engineering and more Study notes Design in PDF only on Docsity!

Dr. Michael Eichberg Software Engineering Department of Computer Science Technische Universität Darmstadt

Introduction to Software Engineering

The Factory Method

Design Pattern

For details see Gamma et al. in “Design Patterns”

The GoF Design Patterns | Example / Motivation 2 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.

The GoF Design Patterns | Example / Motivation - Common functionality for handling documents 4 Nisus Writer Pro TextMate

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

The GoF Design Patterns | Intent 5 Define an interface for creating an object, but let subclasses decide which class to instantiate. (Factory Method lets a class defer instantiation to subclasses.)

The GoF Design Patterns | Example / 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(); } }

The GoF Design Patterns | Example / Motivation - Class Diagram of an Application Using the Framework 8 open() close() save() Document TextDocument createDocument() newDocument() Application createDocument() MyApplication docs

The GoF Design Patterns | Participants 10

  • (^) 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.

The GoF Design Patterns | Consequences (I) 11

  • (^) The framework’s code only deals with the Product interface; therefore it can work with any user-defined ConcreteProduct class.
  • (^) Provides a hook for subclasses The hook can be used for providing an extended version of an object.

The GoF Design Patterns | Implementation 13 Two major variants:

  • (^) Creator is abstract
  • (^) Creator is concrete and provides a reasonable default

implementation

The GoF Design Patterns | Implementation - Parameterized factory methods 14

(E.g. imagine a 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(); } } }

Placeholder | Related Patterns

  • (^) Factory Methods are usually called within Template Methods
  • (^) Abstract Factory is often implemented with factory methods 16

Dr. Michael Eichberg Software Engineering Department of Computer Science Technische Universität Darmstadt

Introduction to Software Engineering

The Abstract Factory

Design Pattern

For details see Gamma et al. in “Design Patterns”

The GoF Design Patterns | The Abstract Factory Method Design Pattern Motivation / Example Scenario 19 Our goal is to support different databases. Requirements: ▶The application should support several databases (We want to be able to change the database at startup time.) ▶We want to support further databases (We want to make the implementation unaware of the specific database(s).)

Excursion | Supporting Variety A result set enables the iteration over the result of an SQL query. 20 How to provide an interface to all of these different kinds of ResultSets? MySQLResultSet DB2ResultSet FirebirdResultSet MsSQLResultSet OracleResultSet