[PDF] [PDF] The Factory Method Design Pattern - GitHub Pages

The Factory Method Design Pattern Example / Motivation - A Possible Implementation of the Framework public abstract class Document { public abstract void 



Previous PDF Next PDF





[PDF] Abstract Factory Pattern

The Abstract Factory Pattern provides an interface for creating families Abstract Factory Method Motivation (1) Abstract Factory Method: Structural Example 



[PDF] Abstract Factory Pattern

This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes



[PDF] Creational Design Patterns Abstract Factory Review Abstract Factory

Creational Patterns - CSC407 Tutorial Notes 1 Factory Method defines an interface for creating objects, but Abstract Factory Example 1 (Continued )



[PDF] Factory Patterns: Factory Method and Abstract Factory

All OO languages have an idiom for object creation In Java this idiom is the new operator Creational patterns allow us to write methods that create new objects 



[PDF] 33 Le pattern Abstract Factory - Unithequecom

Le but du pattern Abstract Factory est la création d'objets regroupés en familles sans devoir connaître les Design Patterns en Java L'objet Catalogue peut 



[PDF] Factory Pattern - University of Colorado Boulder

need to look at two design patterns: Factory Method and Abstract Factory 6 To demonstrate the factory method pattern, the pizza store example evolves



[PDF] The Factory Method Design Pattern - GitHub Pages

The Factory Method Design Pattern Example / Motivation - A Possible Implementation of the Framework public abstract class Document { public abstract void 



[PDF] The Factory Method Design Pattern - GitHub Pages

The Factory Method Design Pattern Example / Motivation - A Possible Implementation of the Framework 6 public abstract class Document { public abstract void 



ABSTRACT FACTORY AND SINGLETON DESIGN PATTERNS TO

Design Patterns, Abstract Factory, Singleton, Decorator, Reusability, Web Application 1 INTRODUCTION patterns in the web application For Example if the



[PDF] Design Patterns

Bridge Builder Chain of Responsibility Adapter Abstract Factory Objet Template Method Interpreter Adapter Factory Method Classe Portée Comportement

[PDF] abstract factory design pattern in java with realtime example

[PDF] abstract factory design pattern in spring boot

[PDF] abstract factory design pattern javatpoint

[PDF] abstract factory design pattern python

[PDF] abstract factory design pattern real time example

[PDF] abstract factory design pattern vs factory pattern

[PDF] abstract factory vs factory method

[PDF] abstract for android app project

[PDF] abstract for android application project

[PDF] abstract for course management system

[PDF] abstract for project

[PDF] abstract function

[PDF] abstract ieee format example

[PDF] abstract imrad format example

[PDF] abstract in project documentation

|The GoF Design PatternsThe Factory Method Design PatternExample / Motivation•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.2

|The GoF Design PatternsThe Factory Method Design PatternExample / Motivation - Common functionality for handling documents3TextMateNisus Writer Pro

|The GoF Design PatternsThe Factory Method Design PatternExample / Motivation - Common functionality for handling documents4TextMateNisus Writer Pro

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

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

|The GoF Design PatternsThe Factory Method Design PatternExample / Motivation - A Possible Implementation of the Frameworkpublic abstract class Document { public abstract void open(); public abstract void close(); } public abstract class Application { private List docs = new ArrayList(); public void newDocument() { Document doc = createDocument(); // the framework manages the documents docs.add(doc); doc.open(); } ... public abstract Document createDocument(); // factory method }6

|The Factory Method Design PatternExample / Motivation - Implementation of an Application Using the Frameworkpublic class TextDocument extends Document { ... // implementation of the abstract methods } public class MyApplication extends Application { public Document createDocument() { return new TextDocument(); } }The GoF Design Patterns7

|The GoF Design PatternsThe Factory Method Design PatternExample / Motivation - Class Diagram of an Application Using the Framework8

open() close() save()

DocumentTextDocument

createDocument() newDocument()

ApplicationcreateDocument() MyApplicationdocs

|The GoF Design PatternsThe Factory Method Design PatternStructure9ProductConcreteProduct factoryMethod() anOperation()

CreatorfactoryMethod() ConcreteCreator

"method» ... factoryMethod()... |The GoF Design PatternsThe Factory Method Design PatternParticipants•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.10

|The GoF Design PatternsThe Factory Method Design PatternConsequences (I)•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.11

|The GoF Design PatternsThe Factory Method Design PatternConsequences (II)•Connects parallel class hierarchies12

Class Name

Collaborator A

Collaborator B

Responsibility A

Responsibility B

Responsibility C

drag()

ManipulatorLineManipulatorcreateManipulator() FigureTextManipulatorClientcreateManipulator() TextcreateManipulator() Line

|The GoF Design PatternsThe Factory Method Design PatternImplementationTwo major variants: •Creator is abstract •Creator is concrete and provides a reasonable default implementation13

|The GoF Design PatternsThe Factory Method Design PatternImplementation - Parameterized factory methods(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(); } } }14

|The GoF Design PatternsThe Factory Method Design PatternImplementation - Parameterized factory methodspublic abstract class Application { private Class clazz; public Application(Class clazz){ this.clazz = clazz; } public abstract Document createDocument(){ return clazz.newInstance(); } }15It is possible to use Java reflection in a type safe way.

|PlaceholderThe Factory Method Design PatternRelated Patterns•Factory Methods are usually called within Template Methods •Abstract Factory is often implemented with factory methods16

|The GoF Design PatternsHow to create families of related classes that implement a (set of) common interface(s)?18

|The GoF Design PatternsThe Abstract Factory Method Design PatternMotivation / Example ScenarioOur 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).)19

|ExcursionSupporting VarietyA result set enables the iteration over the result of an SQL query. 20How to provide an interface to all of these different kinds of ResultSets?MySQLResultSetDB2ResultSetFirebirdResultSetMsSQLResultSetOracleResultSet

|ExcursionSupporting Variety by Providing a Common InterfaceA result set enables the iteration over the result of an SQL query.21A common interface is introduced to abstract from the concrete classes.

first() next() close() "interface» java.sql.ResultSet first() next() close()

MySQLResultSet

first() next() close()

DB2ResultSet

first() next() close()

FirebirdResultSet

|The GoF Design PatternsThe Abstract Factory Method Design PatternMotivation / Example Scenario•To complete the abstraction of the database, one also needs to create class hierarchies for: CallableStatements, PreparedStatements, Blobs, ... •The code interacting with the database can now deal with ResultSets and SQL statements without referring to the concrete classes, e.g., Firebird-ResultSet •However, we still have to know the concrete implementation subclass at creation time!22

|The GoF Design PatternsThe Abstract Factory Method Design PatternIssues•How can we avoid to know about the concrete product types at creation time?

We want to avoid to write:

PreparedStatement = new FBPreparedStatement(); •Hard-coding product types as above makes it impossible to select a different database •Even offline changes are difficult as it is easy to miss one constructor and end up with FireBird's FBPreparedStatement while a DB2 database is used23

|The GoF Design PatternsIssues - How can we avoid to know about the concrete product types at creation time?Swapping Code •Swap in and out different files when compiling for a different database •Does neither require subclassing nor a special creation logic Trade-offs •Application code is completely unaware of different databases •Needs configuration management of source files •Does not allow different databases to be chosen at startup, e.g., if more than one is supported •Does not allow multiple databases to be used at runtime24Solution// DB2 Version java.sql.ResultSet// MySQL Version java.sql.ResultSet// MaxDB Version java.sql.ResultSet

|The GoF Design PatternsThe Abstract Factory Method Design PatternStructure25 createProdA() createProdB() "interface»

AbstractFactory

createProdA() createProdB()

ConcreteFactory

"interface»

AbstractProductA

ProductA1ProductA2

"interface»

AbstractProductB

ProductB1ProductB2

createProdA() createProdB()

ConcreteFactoryClient

|The GoF Design PatternsThe Abstract Factory Method Design PatternParticipants•AbstractFactory ... provides an interface for creating products of a family •ConcreteFactory ... implements the operations to create concrete products •AbstractProduct ... declares the interface for concrete products •ConcreteProduct

... provides an implementation for the product created by the corresponding ConcreteFactory •Client

... creates products by calling the ConcreteFactory; uses the AbstractProduct interface26

|The GoF Design PatternsThe Abstract Factory Method Design PatternConsequences•Abstracts away from concrete products

(Clients can be ignorant about concrete products they are using, even at creation time.) •Exchanging product families is easy

(Changing one line can completely swap the behavior of a whole product family.) •Ensures consistency among products

(As family selection is concentrated to one line, one may not accidentally mix product types.) •Supporting new kinds of products is difficult

(Adding new products involves changing the abstract factory and all of its subclasses.) •Creation of objects is non-standard

(Clients need to know to use the factory rather than a constructor.)27

|The GoF Design PatternsThe Abstract Factory Method Design PatternIssues - How can we avoid to know about the concrete product types at creation time?Factory Class •Group creation functions into a special "factory" class responsible for creating the objects to interact with the database on request. •Has functions like...

createStatement(), createBlob() and prepareStatement()

as part of its interface •Different factory subclasses provide implementations for different databases.

Statement s = connection.createStatement(); 28Solution createStatement() createBlob() create...() "interface»

Connection

createStatement() createBlob() create...()

MySQLConnection

createStatement() createBlob() create...()

DB2Connection

createStatement() createBlob() create...()

FirebirdConnection

|The GoF Design PatternsThe Abstract Factory Method Design PatternProduct Creation•Creation of database objects is done by accessing the global variable connection of type Connection (the "factory")

Statement = connection.createStatement(); •To interact with a different database the connection is initialized differently:

connection = or connection =

DriverManager.getConnection("org.mysql.Driver") •We can make the initialization value for DriverManager.getConnection a parameter of the application29

|The GoF Design PatternsThe Abstract Factory Method Design PatternApplied30 createStatement() createBlob() "interface»

Connection

createStatement() createBlob()

DB2Connection

"interface» java.sql.Statement "interface» java.sql.Blob

MySQLBlobDB2BlobFirebirdBlob

|The GoF Design PatternsThe Abstract Factory Method Design PatternSummary•Application code can be ignorant about different databases •Only one line of code (or configuration parameter) must vary to support various databases •Allows different databases to be chosen at startup •Enforces creation of consistent product families

(Prevents FBBlob from being used with a DB2 database.) •Code must follow a new convention for creating products from a family

(Instead of using the standard constructor.)31 |The Abstract Factory Method Design Pattern - Applied32 getNewCashDrawer() : jpos.CashDrawer getNewCoinDispenser() : jpos.CoinDispenser "interface»

IJavaPOSDevicesFactory

getNewCashDrawer() : jpos.CashDrawer getNewCoinDispenser() : jpos.CoinDispenser

NCRJavaPOSDevicesFactory

getNewCashDrawer() : jpos.CashDrawer getNewCoinDispenser() : jpos.CoinDispenser

NCRJavaPOSDevicesFactory

"method» return new com.ibm.pos.jpos.CashDrawer; "method» return new com.ncr.posdevices.CashDrawer; isDrawerOpened() : boolean "interface» jpos.CashDrawer isDrawerOpened() : boolean com.ncr.posdevices.CashDrawer isDrawerOpened() : boolean com.ibm.pos.jpos.CashDrawer

Example from the POS Domain.

The Abstract Factory Method Design PatternRelated Patterns•A concrete factory is often a singleton

quotesdbs_dbs17.pdfusesText_23