Design Mode-simple factory Mode
Click to view: there are no simple factory models in the 23 design models of the design model series. However, we all have simple factory models, his true purpose is to gradually understand the subsequent factory model, factory method, abstract factory, that is, to understand the term "Factory. Simple factory mode simple factory mode is to enter the factory class for decision-making by providing data, instantiate the class, and perform program operations in the instantiated class, then return the public base class that may be instantiated, and obtain the expected result from the base class. Example: seeing a doctor is like processing components in a factory. How can this problem be solved? For example, you go to the male department. However, there are many classrooms in the hospital. After you enter the hospital, you will face a nurse and check if you are a male girl or what is the situation, then take you to your department. Analysis: This oncoming nurse is the decision maker who chooses the department first, because he knows some of your secrets from your description as a factory method. However, each object belongs to a different object and waits for you to see a doctor. The basic category of each department is a hospital. After you finish reading the disease, the hospital will give you a diagnosis and treatment statement. Basic class code: copy the code // public class Room in the hospital {// male nurse, the content that the gynecology ignores protected string boyRoomSay, grilRoomSay; // return the public string getBoyRoom () {return boyRoomSay;} // return the public string getGrilRoom () {return grilRoomSay;} copied the code subclass code: according to their own characteristics, different nurses will give you a reasonable statement to copy the code // male public class boyRoom: Room {public boyRoom (string sex) {if (sex = "male") {boyRoomSay = "Welcome to male classroom"; grilRoomSay = "Haha, you cannot come to gynecology. ";} Else {boyRoomSay =" Are you a male or female! "; GrilRoomSay =" Are you a male or female! ";}}// Gynecology public class grilRoom: Room {public grilRoom (string sex) {if (sex =" female ") {grilRoomSay =" Welcome to female "; boyRoomSay = "well, you cannot come to the male division. ";} Else {boyRoomSay =" Are you a male or female! "; GrilRoomSay =" Are you a male or female! ";}}} Copy the code Factory: copy the code to the nurse who decides where to go // build the public class RoomFactory {public RoomFactory () {}// decision maker in the factory, give me a parameter. I will tell you the public Room getRoom (string sex) {if (sex = "male") {return new boyRoom (sex );} else {return new grilRoom (sex) ;}} copy the code to run: copy the code public static void Main (string [] args) {Console. writeLine ("excuse me, your gender is:"); // obtain the gender you entered string str = Console. readLine (); RoomFactory fac = new RoomFactory (); Room rom = fac. getRoom (str); Console. writeLine ("male nurse:" + rom. getBoyRoom (); Console. writeLine ("gynecological NURSE:" + rom. getGrilRoom (); Console. readLine ();}