Design Mode 1-- simple factory pattern

Simple factory pattern, one sentence is the reference mass used for the embodiment of the process object instance.

The benefits of simple plant: decoupling, implement polymorphism.

A brief description; the factory model is the key to achieving the original function of class division, abstracted into subclasses (if a simple function of the entire project, more harm than good), a mass participation instantiated through a unified approach, so that the benefits , wherein the time when a demand is changed, the function does not affect the other modules, while in subclass can be pulled out of the construction method for processing, the initialization data and other operations.

 

class EasyFactory{

    public function create(param){
         switch(param){
             case 'add':
              return new ProductAdd();
             //....
        }
    }  
}

class ProductAdd{...}

Usage scenarios: can be used with single-mode embodiment, to reduce the number of instances, the object can also customize the pool, the pool or the objects created in the object acquired

Guess you like

Origin www.cnblogs.com/callmelx/p/12609631.html