Abstract Factory of Factory Pattern

Factory design pattern abstract factory

Demo site

https://github.com/rocky123shine/AbsFactory180417

concept

Define a uniform interface for creating objects, which is then implemented by subclasses. The factory pattern is divided into simple factory and abstract factory. This article will give a simple understanding and use of abstract factories.
An abstract factory provides an interface for clients to create product objects in multiple product families.

AbsFactory usage conditions

1. There are multiple product families in the system, and the system can only consume one family of products at a time
2. Products belonging to the same product family are used

AbsFactory implementation and use

AbsFactory implementation

  1. Two abstract classes, AbsFactory and AbsObject
  2. AbsFactory provides methods to create objects and return AbsObject
  3. AbsObject provides common methods for objects
  4. Concrete Object implementation, abstraction and implementation of specific object's functional modules
  5. concrete factory implementation
  6. Create concrete factories and object classes

AbsFactory uses

  AbsFactory factory = new BaiDuFactory();
        AbsMapView mapView = factory.createMapView();
        AbsMapLocation mapLocation = factory.createMapLocation();
        AbsMapNavigation mapNavigation = factory.createMapNavigation();
        mapNavigation.turnByTurn();
        mapLocation.location();

Factory practical application example

For example, in the development, we are developing a map module, among which there are Baidu maps, Gaode maps, Google maps, etc. for users to choose. In the process of selection, it is necessary to switch between different maps. If we use the factory pattern, then no matter if we add other maps in the future, we can easily implement it as long as we change the specific object, and the scalability and maintainability of the code is greatly improved. Not enough, this time the abstract factory is a good choice

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324513753&siteId=291194637