3.9 KiB
obj |
---|
concept |
Factory Method Pattern
Description:
The Factory Method Pattern is a creational design pattern that defines an interface for creating objects but allows subclasses to alter the type of objects that will be created. It provides a way to delegate the responsibility of instantiating objects to subclasses, promoting flexibility and extensibility in your code.
How it's Used:
- Creator Interface or Abstract Class: Define an abstract class or interface, often referred to as the "creator," that declares a method for creating objects. This method is the factory method.
- Concrete Creators: Create concrete subclasses of the creator class, each of which implements the factory method. These subclasses decide which class of objects to create and return.
- Product: Define an abstract product class or interface that represents the type of object created by the factory method.
- Concrete Products: Create concrete classes that implement the product interface. Each concrete product represents a specific variant or type of object.
- Client Code: In your application, use the creator class and its factory method to create objects. The client code typically works with the creator interface or abstract class rather than concrete classes.
Example:
Imagine you are developing a document processing application that can handle different types of documents, such as TextDocument
and SpreadsheetDocument
. Here's how the Factory Method Pattern can be applied:
- Creator Interface or Abstract Class: Define an abstract class called
DocumentCreator
with a factory method namedcreateDocument
.
public abstract class DocumentCreator {
public abstract Document createDocument();
}
- Concrete Creators: Create concrete subclasses of
DocumentCreator
, such asTextDocumentCreator
andSpreadsheetDocumentCreator
. Each subclass implements thecreateDocument
method to return a specific type of document
public class TextDocumentCreator extends DocumentCreator {
@Override
public Document createDocument() {
return new TextDocument();
}
}
public class SpreadsheetDocumentCreator extends DocumentCreator {
@Override
public Document createDocument() {
return new SpreadsheetDocument();
}
}
- Product: Define an abstract
Document
class or interface that represents the common operations for documents.
public abstract class Document {
public abstract void open();
public abstract void save();
}
- Concrete Products: Create concrete classes like
TextDocument
andSpreadsheetDocument
that implement theDocument
interface with specific implementations.
public class TextDocument extends Document {
@Override
public void open() {
System.out.println("Opening Text Document");
}
@Override
public void save() {
System.out.println("Saving Text Document");
}
}
public class SpreadsheetDocument extends Document {
@Override
public void open() {
System.out.println("Opening Spreadsheet Document");
}
@Override
public void save() {
System.out.println("Saving Spreadsheet Document");
}
}
- Client Code: In your application, use the creator classes and their factory methods to create documents without knowing the concrete document classes:
DocumentCreator textDocumentCreator = new TextDocumentCreator();
Document textDocument = textDocumentCreator.createDocument();
textDocument.open();
textDocument.save();
DocumentCreator spreadsheetDocumentCreator = new SpreadsheetDocumentCreator();
Document spreadsheetDocument = spreadsheetDocumentCreator.createDocument();
spreadsheetDocument.open();
spreadsheetDocument.save();
The Factory Method Pattern allows you to extend your application with new document types by creating additional concrete creators and products without modifying existing client code, promoting code reuse and maintainability.