Simple factory mode simplefactory
The simple factory mode is also called the static method mode (because the factory class defines a static method). A factory class determines which product class instance to create based on the input parameters.
Change the demandEncapsulationTo oneClassTo reduce the coupling between objects.
// Encapsulate the frequently changed requirements into the public class simplefactorycooker of the simple factory class simplefactorycooker {// defines the static method cook public static food cook (string type) {// define the object to be returned and assign the value to null food = NULL; // switch (type) {Case "tomato": Food = new tomatoes (); break; Case "potato": Food = new potatoes (); break ;}// return the target object return food ;}}
Application scenarios:
When the factory class is responsible for creating fewer objects, you can consider using the simple factory mode;
If you only know the parameters passed into the factory class, you can consider using the simple factory mode when you do not care about how to create the object logic.