The factory pattern
define
The factory pattern defines an interface for creating objects and lets subclasses decide which class to instantiate.
Usage scenarios
- The factory pattern can be used anywhere complex objects need to be generated.
- Create different instances under different conditions.
Code implementation
The following describes how to use the factory mode to implement data storage services.
- Abstract product classes define operation methods
public abstract class DataHandler {
public abstract void add(String id, String name);
public abstract void delete(String id);
public abstract void update(String id, String name);
public abstract String query(String id);
}
Copy the code
- Three different storage methods (file, XML, database) code implementation.
public class FileHandler extends DataHandler{
@Override
public void add(String id, String name) {}@Override
public void delete(String id) {}@Override
public void update(String id, String name) {}@Override
public String query(String id) {
return "FileHandler"; }}Copy the code
public class XmlHandler extends DataHandler{
@Override
public void add(String id, String name) {}@Override
public void delete(String id) {}@Override
public void update(String id, String name) {}@Override
public String query(String id) {
return "XmlHandler"; }}Copy the code
public class DBHandler extends DataHandler {
@Override
public void add(String id, String name) {}@Override
public void delete(String id) {}@Override
public void update(String id, String name) {}@Override
public String query(String id) {
return "DBHandler"; }}Copy the code
- The factory class
public class DataFactory {
public static <T extends DataHandler> T getDataHandler(Class<T> tClass) {
DataHandler handler = null;
try {
handler = (DataHandler) Class.forName(tClass.getName()).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return(T) handler; }}Copy the code
- Use of factory patterns
public class DataHandlerTest {
@Test
public void testFactory(a) {
FileHandler fileHandler = DataFactory.getDataHandler(FileHandler.class);
System.out.println("FileHandler query result = " + fileHandler.query("Test"));
XmlHandler xmlHandler = DataFactory.getDataHandler(XmlHandler.class);
System.out.println("XmlHandler query result = " + xmlHandler.query("Test"));
DBHandler dbHandler = DataFactory.getDataHandler(DBHandler.class);
System.out.println("FileHandler query result = " + dbHandler.query("Test"));
}
}
FileHandler query result = FileHandler
XmlHandler query result = XmlHandler
FileHandler query result = DBHandler
Copy the code
Abstract Factory pattern
define
The abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their concrete classes. The creation of the object is ultimately done by the concrete factory.
Usage scenarios
- It is important for all types of factory patterns that a system should not depend on the details of how product class instances are created, combined, and expressed.
- There is more than one product family in the system and only one of them is used at a time.
- Products belonging to the same product family will be used together, and this constraint must be reflected in the design of the system.
- The system provides a library of product classes, all of which appear in the same interface, so that the client is implementation-independent.
The difference between abstract factory and factory pattern
In an abstract factory, multiple types of products can be created per factory, whereas in a factory pattern, only one type of product can be created.
Code implementation
- Abstract Factory class: Generic interfaces that define concrete factories.
- Concrete factory classes: Create instances of different concrete product classes by using the methods of different target products.
- Abstract product class: Defines the public interface for abstract product classes.
- Abstract product class: Inherits the abstract product class and defines the common interface of the concrete product class.
- Concrete product class: Inherits the abstract product class and defines methods for creating corresponding concrete product instances.
Existing ready-to-wear companies need to generate jackets and T-shirts in fashionable and classical styles.
- Firstly, define the abstract clothing category Clothes and the public interface show() of the connotation clothing.
public interface clothes {
void show(a);
}
Copy the code
- Define abstract product classes for coats and T-shirts, respectively
public abstract class CoatClothes implements clothes {}
public abstract class TShirtClothes implements clothes{}Copy the code
- Define specific styles and types of garment categories
public class StylishCoat extends CoatClothes {
@Override
public void show(a) {
System.out.println("Create a fashionable coat."); }}public class ClassicCoat extends CoatClothes {
@Override
public void show(a) {
System.out.println("Make a classic coat."); }}public class StylishTShirt extends TShirtClothes {
@Override
public void show(a) {
System.out.println("Generate a fashionable T-shirt."); }}public class ClassicTShirt extends TShirtClothes {
@Override
public void show(a) {
System.out.println("Generating a classic T-shirt."); }}Copy the code
- Define specific factory classes for coats and T-shirts to produce different styles of coats and T-shirts
public class CoatFactory extends AbstractFactory {
@Override
publicclothesstylish(a) {
return new StylishCoat();
}
@Override
publicclothesclassic(a) {
return newClassicCoat(); }}public class TShirtFactory extends AbstractFactory {
@Override
publicclothesstylish(a) {
return new StylishTShirt();
}
@Override
publicclothesclassic(a) {
return newClassicTShirt(); }}Copy the code
- Start up factories to produce ready-made garments
public class AbstractFactoryTest {
@Test
public void clothes(a) {
CoatFactory coatFactory = new CoatFactory();
coatFactory.stylish().show();
coatFactory.classic().show();
TShirtFactory tShirtFactory = newTShirtFactory(); tShirtFactory.stylish().show(); tShirtFactory.classic().show(); }} Generates fashionable coats generates classical coats generates fashionable T-shirts generates classical T-shirtsCopy the code