"Design Mode" Factory method
1. Create objects from the corresponding factory
2. Compared to a simple factory, code design complexity increases, calls become complex, but maintenance costs are reduced, and each time a new class is added, the corresponding factory is added without the need to modify the original code. Simple Factory each time you add a new class, you need to modify the original method of creating the object.
3. More in line with the opening and shutting principle
4. Code
PublicInterfaceifactory {calbase createcal (); }PublicClassaddfactory:ifactory {PublicCalbase createcal () {ReturnNewCaladd (); } }PublicClasssubfactory:ifactory {Publiccalbase createcal () { return new calsub ();}} public class mulfactory:ifactory { public calbase createcal () { return new Calmul (); } public class divfactory:ifactory { public calbase createcal () { return new Ca Ldiv (); } }
Design Mode Factory method