Abstract Factory pattern

The abstract factory pattern is to create other factory classes around a gigafactory class. Around the factory class, create the entity class. It adds a gigafactory class to the traditional factory model.

What is the Abstract Factory pattern?

The abstract factory pattern is to create other factory classes around a gigafactory class. Around the factory class, create the entity class.

It adds a gigafactory class to the traditional factory model.

Its advantages and disadvantages are similar to those of factory mode. There are no more advantages and disadvantages here. Let’s talk about implementation directly.

How to implement the Abstract Factory pattern?

To make the goal clearer, implement the following schematic:

Prepare the entity class

As before, we implement several entity classes: Cat and Dog as a group, Male and Female as a group.

Class Dog {run() {console.log(" Dog "); }} class Cat {run() {console.log(" Cat "); }} class Male {run() {console.log(" Male "); }} class Female {run() {console.log(" Female "); }}Copy the code

Prepare factory class

Let’s say that Cat and Dog belong to the Animal factory; Male and Female are products of the Person factory. So you need to implement two factory classes: Animal and Person.

Since the factory class also has a gigafactory on it, the factory class should provide the method interface of the production entity for the convenience of the factory class.

To better constrain the implementation of the factory class, first implement an abstract factory class:

Class AbstractFactory {getPerson() {throw new Error(" subclass please implement interface "); } getAnimal() {throw new Error(" subclass please implement interface "); }}Copy the code

Next, Animal and Dog implement an AbstractFactory class:

class PersonFactory extends AbstractFactory { getPerson(person) { person = person.toLocaleLowerCase(); switch (person) { case "male": return new Male(); case "female": return new Female(); default: break; } } getAnimal() { return null; } } class AnimalFactory extends AbstractFactory { getPerson() { return null; } getAnimal(animal) { animal = animal.toLocaleLowerCase(); switch (animal) { case "cat": return new Cat(); case "dog": return new Dog(); default: break; }}}Copy the code

Implementing the Gigafactory

Gigafactory implementation is not difficult, as follows:

class Factory { constructor(choice) { choice = choice.toLocaleLowerCase(); switch (choice) { case "person": return new PersonFactory(); case "animal": return new AnimalFactory(); default: break; }}}Copy the code

Analysis of the summary

advantages

  1. When multiple objects in a product family are designed to work together, it ensures that clients always use only objects in the same product family.
  2. Easy to exchange product families, because the concrete factory class only needs to appear once in an application during initialization, making it very easy to change the concrete factory of an application to use different product configurations by changing the concrete factory.
  3. It separates the concrete instance creation process from the client side, which manipulates the instance through their abstract interface, and the concrete class name of the product is separated from the implementation of the concrete factory and does not appear in the client code

disadvantages

  1. Product family extension is very difficult, and to add a product to a family requires both code in the abstract Creator and code in the concrete one.
  2. If you want to add a Project table Project, then you need to add three classes, IProject, MySQLProject, OracleProject, also need to change the IFactory, MySQLFactory, OracleFactory to complete the implementation. This is bad. Programming is an art, and this is obviously a very ugly practice.

How to improve?

Abstract factories can be improved with simple factories. Remove IFactory, MySQLFactory, OracleFactory three factory classes, with a DataAcces class, with a simple factory pattern to implement. Specific implementation code omission, interested partners can achieve their own.

Usage scenarios

  1. QQ for skin, a set of changes together.
  2. Generate programs for different operating systems.

The characteristics of

  • Product families are difficult to scale
  • Product grades are easy to expand.