[PDF] [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 



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 - 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 PatternIntent5Define 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 PatternsThe Factory Method Design PatternExample / Motivation - A Possible Implementation of the Framework6public 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 documentsdocs.add(doc);doc.open();}...public abstract Document createDocument(); // factory method}

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

|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 PatternParticipants10•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 PatternsThe Factory Method Design PatternConsequences (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.

Class Name

Collaborator A

Collaborator B

Responsibility A

Responsibility B

Responsibility C

drag()

ManipulatorLineManipulatorcreateManipulator() FigureTextManipulatorClientcreateManipulator() TextcreateManipulator() Line

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

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

|The GoF Design PatternsThe Factory Method Design PatternImplementation - Parameterized factory methods15public abstract class Application {private Class clazz;public Application(Class clazz){this.clazz = clazz;}public abstract Document createDocument(){return clazz.newInstance();}}It 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 Patterns18How to create families of related classes that implement a (set of) common interface(s)?

(We want to make the implementation unaware of the specific database(s).)

|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

|ExcursionA result set enables the iteration over the result of an SQL query. Supporting Variety by Providing a Common Interface21A 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 PatternIssues23•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 used

|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

... creates products by calling the ConcreteFactory; uses the AbstractProduct interface (Clients need to know to use the factory rather than a constructor.) createStatement(), createBlob() and prepareStatement() Statement s = connection.createStatement(); Solution createStatement() createBlob() create...() "interface»

Connection

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

MySQLConnection

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

DB2Connection

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

FirebirdConnection

connection = Dri verManager.getConnection("org.postgresql.Driver") or connection = |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

(Instead of using the standard constructor.) |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 GoF Design Patterns33The Abstract Factory Method Design PatternRelated Patterns•A concrete factory is often a singleton

quotesdbs_dbs9.pdfusesText_15