In the previous article about the simple factory model, see link: http://www.cnblogs.com/zhuxiansheng/p/7873161.html
The Simple factory model decouples the client and implementation dependencies, but if there is another extension implementation, we have to modify the selection branch in the factory to add case and then add a new implementation, which violates the open-close principle. How to solve this problem, the factory model gives a very good answer.
Liar design mode-Class diagram
The example code in the liar design pattern is as follows:
/** * */ Public Interface ifactory { public Leifeng Createleifeng ();}
/***/Publicclass Leifeng {publicvoid Sweep () { System.out.println ("sweeping"); } Public void Buyrice () { System.out.println ("Buy rice"); } Public void Wash () { System.out.println ("wash the dishes");} }
/***/Publicclassextends leifeng{}
/***/Publicclassextends leifeng{}
/***/Publicclassimplements ifactory{ @Override public Leifeng Createleifeng () { returnnew Undergraduate ( ); }}
/***/Publicclassimplements ifactory{ @Override public Leifeng Createleifeng () { returnnew Volunteer ();} }
/***/Publicclass Test {publicstatic void Main (string[] args) { new volunteerfactory (); = Volunteerfactory.createleifeng (); Leifeng.sweep (); Leifeng.buyrice (); Leifeng.wash (); }}
If we add a different role, we just need to add the corresponding factory and role domain can be implemented without modifying the original code to add a new implementation, the factory conforms to the open-close principle, but for the client we still have to modify which specific factory to create the code.
Recall the simple Factory mode, if we expand the N-side of M, we only need to implement the computing factory interface, and then add the specific calculation class, the implementation of the extension, which is the author of the Simple Factory and factory model understanding, I hope to help learn the factory model of children's shoes!
Java Factory mode (big talk design mode)