Simple factory design mode []. Factory Design Mode
This baby is just a cainiao who writes and writes Study Notes to help you learn new things. If there are any mistakes or deficiencies, I hope you can correct me.
1. Let's give an example to illustrate. For example, the addition, subtraction, multiplication, division operation [Step-by-Step Optimization]
I. [first writing method]
Class Program {static void Main (string [] args) {// The first method is not professional. // enter the Console. write ("enter A number:"); // receives string A = Console. readLine (); Console. write ("Enter the operator, +,-, *,/"); string B = Console. readLine (); Console. write ("enter the second number"); string C = Console. readLine (); Console. write ("result ="); string D = ""; if (B = "+") {// string to double, tostring () cannot be forgotten () D = (Convert. toDouble (A) + Convert. toDouble (C )). toString ();} if (B = "-") {D = (Convert. toDouble (A) + Convert. toDouble (C )). toString ();} if (B = "*") {D = (Convert. toDouble (A) + Convert. toDouble (C ). toString ();} if (B = "/") {D = (Convert. toDouble (A) + Convert. toDouble (C ). toString ();} Console. writeLine ("Result:" + D );}}
II. The above writing method is not professional or beautiful, so optimize it and look at [the second writing method]
Class Program {static void Main (string [] args) {// This is the second method. try {Console. writeLine ("enter a number"); string sInum1 = Console. readLine (); Console. writeLine ("Enter the operator, +,-, *,/"); string operator = Console. readLine (); Console. writeLine ("enter the second number"); string sInum2 = Console. readLine (); string sResult = ""; switch (Convert) {case "+": sResult = (Convert. toDouble (sInum1) + Convert. toDouble (sInum2 )). toString (); break; case "-": sResult = (Convert. toDouble (sInum1) + Convert. toDouble (sInum2 )). toString (); break; case "*": sResult = (Convert. toDouble (sInum1) + Convert. toDouble (sInum2 )). toString (); break; case "/": if (sInum2 = "") {sResult = "the divisor cannot be 0";} else {sResult = (Convert. toDouble (sInum1) + Convert. toDouble (sInum2 ). toString ();} break;} Console. writeLine ("Result:" + sResult);} catch (Exception ex) {// Console. writeLine (ex); Console. writeLine ("Tips: the website is being repaired. Please contact the Administrator ");}}}
Iii. Are there also many deficiencies in the above writing?
The business logic layer is highly coupled with the client, with a strong correlation: 1. not conducive to use and expansion; 2. insecure;
Then we can optimize it. Let's see [the third Writing Method] [call it after creating an operating class]
We separate the business logic layer from the client. We just need to know how to use it to buy a car.
Class Program {static void Main (string [] args) {try {Console. writeLine ("Enter the first number:"); string sInum1 = Console. readLine (); Console. writeLine ("Enter the operator, +,-, *,/"); string sOper = Console. readLine (); Console. writeLine ("enter the second number:"); string sInum2 = Console. readLine (); string sResult = ""; sResult = (Operating. getResult (Convert. toDouble (sInum1), Convert. toDouble (sInum2), sOper )). toString (); Console. writeLine ("Result:" + sResult);} catch (Exception ex) {Console. writeLine ("Tips: the website is being repaired. Please contact the Administrator ");}}}
We separate the business logic layer from the client. The following code is equivalent to the core and bottom layer of the car.
class Operating { public static double GetResult(double iNum1, double iNum2, string sOper) { double Result = 0d; switch (sOper) { case "+": Result = iNum1 + iNum2; break; case"-": Result = iNum1 - iNum2; break; case"*": Result = iNum1 * iNum2; break; case"/": Result = iNum1 / iNum2; break; } return Result;} }
Iv. What are the shortcomings of the above statements?
Or high coupling. If an open square root is added, the whole program will be re-running;
For example, if the banking system is currently in process or the security factor is high, if +,-, *,/are developed by people with different permissions, they will be seen by each other;
[Then let's try the fourth method, simple factory design mode]
Operating is used as a parent class to write some common things. It only defines a method name and does not perform specific implementation operations.
class Operating { public double dNum1{get;set;} public double dNum2{get;set;} public virtual double GetResult() { double Result = 0; return Result; } }
Define a virtual method. The subclass only needs to override [+ operations].
Class OperAdd: Operating {// override public override double GetResult () {double Result = 0; Result = dNum1 + dNum2; return Result ;}}
class OperReduce:Operating { public override double GetResult() { double Result = 0; Result = dNum1 - dNum2; return Result; } }
class OperRide:Operating { public override double GetResult() { double Result = 0; Result = dNum1 * dNum2; return Result; } }
Class operatet: Operating {public override double GetResult () {double Result = 0; if (dNum2 = 0) {Console. writeLine ("the divisor cannot be 0");} else {Result = dNum1/dNum2;} return Result ;}}
Define a factory class OperateFactory. cs
Class OperateFactory {// create an OperateFactory class, which is used to receive "+,-, *,/" public static Operating createinstances (string sOper) {Operating instances = null; switch (sOper) {case "+": condition = new OperAdd (); // if it is +, we will create a + object, call GetResult () to process break; case "-": condition = new OperReduce (); // if it is-, we create a-object break; case "*": condition = new OperRide (); break; case "/": condition = new operatet (); break;} return condition ;}}
Let's look at the customer's code when calling:
Class Program {static void Main (string [] args) {Console. writeLine ("Enter the first number:"); string sNum1 = Console. readLine (); Console. writeLine ("Enter the operator, +,-, *,/"); string sOper = Console. readLine (); Console. writeLine ("enter the second number:"); string sNum2 = Console. readLine (); string sResult = ""; Operating temperature; temperature = OperateFactory. createinstances (sOper); instances. dNum1 = Convert. toDouble (sNum1); random. dNum2 = Convert. toDouble (sNum2); sResult = random. getResult (). toString (); Console. writeLine ("result equals:" + sResult );}}
Development results:
2. instance description:
Simple factory design pattern: In object-oriented programming, the most common design pattern for programmers is the simple factory pattern. The simple factory mode returns an instance of one of several classes based on the data provided to it. Normally, the returned class has a public parent class and a public method, but each method has different functions and is initialized based on different data.
Advantage: the factory class contains the necessary judgment logic to determine when to create a product class instance. The client can exempt the responsibility of directly creating product objects, but only "consume" the product. The simple factory model achieves division of responsibility through this approach.
Low coupling, high code reusability, and high scalability;
Disadvantage: when the product has a complex multi-layer hierarchical structure, the factory class only has its own, and should not change, is the disadvantage of the model. Because the factory class integrates the creation logic of all products, the entire system will be affected once it fails to work normally. At the same time, it is difficult to expand the system. Once a new product is added, the factory logic has to be modified, which may cause the factory logic to be too complex.
3. Simple factory role and structure:
Update it here for the time being. Go to bed. After all, you will be able to continue learning after all.