From the very beginning I set a goal for myself, to update at least 2 blogs each week to keep track of the problems I encountered during the last week or to come up with new ideas, and to keep a record of what I have mastered in order to avoid forgetting them for a long time. Secondly, although my blog post is not very good but may have a certain impact on a small number of readers, and I believe that with my original blog writing more and more, the level will be more and more high, the depth will be more and more deep (haha, I also envy those famous bloggers, not only professional knowledge is great, but also great writing). Two articles a week I found that I wanted to record something, like this week, I also have a log system, binary data cache (actually called low point of the database is more appropriate), QQwry.dat data interpretation, zip compressed file incremental generation of these aspects are not written, In the later time I will slowly update to my blog, OK no, this time I introduce another mode.
In fact, this model is also more commonly used, should be introduced first, but because I contact the singleton mode is more, so the first introduction of a singleton mode. Simple Factory mode, first look at the definition of it: from the design pattern of the type, the Simple Factory mode is a creation mode, also known as the Static Factory method (Factory) mode, but does not belong to one of the 23 gof design patterns. A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.
What are the benefits of a simple factory model in a project? He has two benefits:
1. The first is to use the simple factory model to instantiate different classes based on different parameters instead of the new method to instantiate each different class, so that the user is better managed.
2. Secondly, if the class to be instantiated is used in more than one file, when we modify the class name, we only need to modify the factory class, without having to modify every file that instantiates the class (a little bit of a feeling of chicken, which is rarely seen).
Look at one of the most classic and best understood examples of a simple factory pattern, operator operations:
The factory class of the simple Factory mode is generally using static methods, by accepting different parameters to return different object instances, the code is written dead, so the code cannot be extended without modification, violating the OCP (for extended development, the change-off principle).
<?PHP/** * Simple Factory mode--Classic Operator Example * @author Yan ([email protected])*// * SimpleIndustrial ******************** Factory ******************** class*/classoperation{/** * @var int $numa * Two numbers to manipulate*/ protected $numa; protected $numb; Public function__construct ($a,$b){ $this->numa =$a; $this->numb =$b; } //static methods that generate different object instances by accepting different parameters Public Static functionCreate$operation,$a,$b){ Switch($operation) { Case' + ':return NewOperationadd ($a,$b); Break; Case‘-‘:return NewOperationminus ($a,$b); Break; default:#code ... Break; } }}/************************************************//*addition*/classOperationaddextendsoperation{ Public functiondoing () {return $this->numa +$this-numb; }}/*Subtraction*/classOperationminusextendsoperation{ Public functiondoing () {return $this->numa-$this-numb; }}$test= operation::create (' + ', 2,56);Echo $test->doing ();
Before I was only exposed to the simple factory model in the project, in order to write this article I specifically checked the data, found that there are three types of Factory mode: Simple Factory mode (also known as static Factory mode), Factory mode, abstract Factory mode, after seeing the Factory mode, it seems that his usefulness is not very large, but in line with the OCP principle, When there is a new product that conforms to the abstract product interface and the abstract factory interface, we only need to extend a specific product and factory, without having to modify the original code to summarize the advantages and disadvantages of the factory model:
Pros: The first is the OCP principle, the scalability is improved, and the maintainability is improved, as long as you can find your own factory role when modifying a specific factory role, without worrying about affecting the implementation of other factory roles.
Cons: A lot of code, each product to a product class and a factory class. This drawback can be achieved with the combination of a simple factory model and a factory model, combining the factory classes of similar product classes into one.
<?PHP/** * Simple Factory mode--Classic Operator Example * @author Yan ([email protected])*//*********** Square ********** method of ********* factory*///Vehicle Interface (abstract product role)Interfacevehicle{ Public functionruning ();}//Vehicle Factory Interface (abstract factory role)Interfacevehiclefactory{ Public Static functionget ();}/*Specific Product Roles*/classCarImplementsvehicle{ Public functionruning () {Echo"My speed is 120km/h \ r"; }}classBicycleImplementsvehicle{ Public functionruning () {Echo"My speed is 30km/h \ r"; }}/*Specific Factory roles*/classCarfactoryImplementsvehiclefactory{ Public Static functionget () {return Newcar (); }}classBicyclefactoryImplementsvehiclefactory{ Public Static functionget () {return Newbicycle (); }}$test= Bicyclefactory::get ();$test->runing ();
Factory class themselves did not in the specific project used, said always feel a little strange feeling, if there is anything wrong, inappropriate place, but also hope that you master predecessors pointed out.
Send me~
PHP design mode (ii)