define

Define an interface for creating objects and let subclasses decide which class to instantiate so that instantiation of a class can be deferred until subclass implementation.

nature

Select the implementation.

The focus of a simple factory is selection. The implementation is already done, and the goal is to select the appropriate implementation for the client, so that the client and the implementation are decoupled, so that the implementation changes without changing the client.

His role

  • Product (Product)

    Product abstract classes define product features, that is, the functional interfaces required by customers.

  • Creator

    Factory abstract classes that define the production process,

  • ConcreteProducts

    Decide on a specific product, because it implements the features of the product, and there may be more than one.

  • ConcreteCreator

    Responsible for generating concrete products, as it implements the product generation process.

The sample code

Public abstract class Product {public abstract void use(); } /** * Generate an abstract class for the product, Public abstract class Factory {public final Product create(String owner){Product Product = createProduct(owner); resisterProduct(product); return product; } protected abstract Product createProduct(String owner); protected abstract void resisterProduct(Product product); } /** * public class extends Product{private String owner; IDCard(String owner) {system.out.println (" make "+ owner +" ID card "); this.owner = owner; } @override public void use() {system.out.println (" + owner + "); } public String getOwner() { return owner; }} /** * The factory where the product is generated, */ Public class IDCardFactory extends Factory{private List<String> owners = new ArrayList(); @Override protected Product createProduct(String owner) { return new IDCard(owner); } public List<String> getOwners() { return owners; } @Override protected void resisterProduct(Product product) { owners.add(((IDCard)product).getOwner()); Public class RunFactory {public static void main(String args[]){Factory Factory = new IDCardFactory();  Product idCard1 = factory. Create (" x "); Product idCard2 = factory. Create (" x "); Product idCard3 = factory. Create (" idCard3 "); idCard1.use(); idCard2.use(); idCard3.use(); }}Copy the code

The results

Making the ID card of Xiaoming 1 and making the ID card of Xiaoming 2 and making the ID card of Xiaoming 3 and making the ID card of Xiaoming 1 and making the ID card of xiaoming 2Copy the code

function

To create a concrete class instance.

advantages

  • Encapsulate, encapsulate the implementation process of an object, so that external calls can be interface oriented programming.
  • Decoupling, because it encapsulates the realization process of the object, so that the external can get the reference of the object only through the interface call, thus realizing the decoupling of the creation and use of the object.

disadvantages

  • This increases the complexity of external object acquisition, because the parameters needed to create the object need to be provided by the external, so that the external needs to understand the meaning of each parameter, increasing the difficulty of using.

When to use

  • To fully encapsulate and isolate the concrete implementation so that the external can only get objects through the interface, you can choose the simple factory pattern, which lets the client get objects through the interface provided by the factory, regardless of the concrete implementation of the object.
  • If you want to centralize the management and control of external object creation, you can choose a simple factory. A simple factory pattern can create many, unrelated objects.