If the code is correct, is it an excellent one? ---- Simple factory Model

Source: Internet
Author: User

The two short poems can summarize my learning mood at the moment! The first chapter of this book, the big talk design model, is not long, but I have gained a deep understanding of my own code writing capabilities. It turns out that the code is not written, I still have a lot of requirements for the code ------- in the first chapter of the simple factory model, I will use a small example to tell me some knowledge in the Code step by step, and these are what I lack. For example, in the example below, the additional code for writing a simple computer program is as follows: [csharp] <span style = "font-family: KaiTi_GB2312; font-size: 18px; "> classProgram {static void Main (string [] args) {Console. write ("Enter the number A:"); // the name is not standard string A = Console. readLine (); Console. write ("Enter the operator number (+,-, *,/)"); string B = Console. readLine (); Console. write ("Enter the number C:"); string C = Console. readLine (); string D = ""; if (B = "+") // The program is cumbersome. This means that every condition must be judged before execution can be performed, D = Convert. toS Tring (Convert. toDouble (A) + Convert. toDouble (C); if (B = "-") D = Convert. toString (Convert. toDouble (A)-Convert. toDouble (C); if (B = "*") D = Convert. toString (Convert. toDouble (A) * Convert. toDouble (C); if (B = "/") D = Convert. toString (Convert. toDouble (A)/Convert. toDouble (C); // If the divisor is 0, an error is reported. writeLine ("Result:" + D) ;}</span> Note: If I write it, I will write it like this, however, there are many non-standard areas, such as non-standard naming, tedious procedures, and so on! This is a problem for beginners! The code is well written, but it does not take into account its scalability flexibility and reusability. Such code is really terrible! Now I understand the meaning of coupling. A good program requires low coupling so that it is easy to maintain and expand! The first chapter is written in a simple way. At last, I understood what the simple factory model is and what the benefits it brings! Next I will record them one by one! Simple factory mode simple factory mode, also known as static factory method mode, belongs to the class creation mode, and its essence is a factory class according to the input parameters, dynamically determines the product type instance to be created. That is, to produce the object you want to create. The simple factory mode does not actually belong to 23 GOF models, but it can be used as a guide to the factory method mode. my personal understanding: I think this is like we want to customize a dress in our life, so we went to the store to describe our needs, then the store handed over our requirements to the factory and made the Clothes we needed. The store here is equivalent to our Clothes class. Our demand is the method class. Merchants create such a demand method, after being handed over to the factory, the factory can inherit the method classes provided by the merchants. Maybe you will think that there is so much trouble with a piece of clothing, and the merchants and factories. If there are too many clothes, There will be various problems in the middle, such as the size of clothes, color, and so on. In this case, the simple factory model comes out. We can easily handle problems and perform our jobs. This is in line with the low coupling principle. Some concepts in the simple factory model factory -------- the core of the role simple factory model, which is responsible for implementing the internal logic of creating all instances. The factory class can be directly called by the outside world to create the required product objects. Abstract product ---------- role parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances. The specific product ------- role is the creation target of the simple factory mode. All created objects are instances of a specific class that acts as the role. Taking advantage of the simple factory model, the factory class is the key to the entire model. it contains the necessary logical judgment to determine the specific class object to be created based on the information given by the outside world. by using the factory class, the outside world can get rid of the embarrassing situation of directly creating specific product objects, and only need to be responsible for "consuming" objects. Without having to worry about how these objects are created and organized, they clearly define their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture. Disadvantages: Because the factory class integrates the creation logic of all instances, it violates the High Cohesion responsibility allocation principle and integrates all creation logic into a factory class; the class it can create can only be considered in advance, if you need to add a new class, you need to change the factory class. When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion; these shortcomings have been overcome in the factory method model. The scope factory class is used to create fewer objects. The customer only knows the parameters passed in the factory class and does not care about how to create objects (logic; because simple factories are easy to violate the High Cohesion responsibility allocation principle, they are generally used only in simple cases. A dedicated class is defined to create instances of other classes. The created instance usually has a common parent class. [csharp] <span style = "font-family: KaiTi_GB2312; font-size: 18px;"> static void main (string [] args) // simple call of {Operation; operator = OperationFactory in the main function. createOperate ("+"); forward. numberA = 1; Numeric. numberB = 2; double result = random. getResult (); Console. writeLine (response. getResult () ;}} public classOperation // obtain the number A in the main class. B and result {private double _ numberA = 0; private double _ numberB = 0; public double NumberA {get {return _ numberA;} set {_ numberA = value ;}} public double NumberB {get {return _ numberB;} set {_ numberB = value ;}} public virtual double GetResult () {double result = 0; return result ;}} classOperationAdd: operation // addition {public override double GetResult () {double result = 0; result = NumberA + NumberB; return result ;}} classOperationSub: operation // subtraction {public override double GetResult () {double result = 0; result = NumberA-NumberB; return result ;}} classOperationMul: operation // multiplication {public override double GetResult () {double result = 0; result = NumberA * NumberB; return result ;}} class OperationDiv: operation // division inherits the main Operation class {public override double GetResult () {double result = 0; if (NumberB = 0) // write a primary class, that is, the number and method of the operation. Then, when writing each method, inherit and re-write these methods. thrownew Exception ("the divisor cannot be 0 "); result = NumberA/NumberB; return result;} public classOperationFactory // creates a factory. The user has entered a symbol, the factory will determine how to calculate {public static OperationcreateOperate (string operate) {Operation condition = null; switch (operate) {case "+": condition = new OperationAdd (); break; www.2cto.com case "-": condition = new OperationSub (); break; case "*": condition = new OperationMul (); break; case "/": break = new OperationDiv (); break;} return break ;}}</span>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.