After the first example of the design pattern, I once again realized that building a software is not only for function implementation, but also for "serving the people "!
You do not need to use object-oriented methods to write data. To implement a simple "+-*/" Calculator, you only need to obtain the numbers and operators entered by the user, then the result of "A operation B" is displayed. The following code is used:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { static void Main(string[] args) {
// Enter the number and operator Console for calculation. writeLine ("Enter the number A"); double A = Convert. toDouble (Console. readLine (); Console. writeLine ("Enter the operator"); string operate = Console. readLine (); Console. writeLine ("Enter the number B"); double B = Convert. toDouble (Console. readLine ());
// Store the result in variable D. double D = 0;
// Use the if Condition Statement to determine the input symbol and perform the corresponding operation and output if (operate = "+") {D = A + B; Console. writeLine ("A + B =" + D);} if (operate = "-") {D = A-B; Console. writeLine ("A-B =" + D);} if (operate = "*") {D = A * B; Console. writeLine ("A * B =" + D);} if (operate = "/") {D = A/B; Console. writeLine ("A/B =" + D );}}}}
What if the user's needs have changed? For example, if you want to add an operation with the root number, then add an if statement, which must be modified in the input item. When the divisor is zero, an error will occur and the program will die, the generation of these changes is a very troublesome thing for modifying programs. Small programs are fine, but large programs are especially prone to errors and are not easy to modify, it is very likely to modify one place, but it causes a problem in another place. The whole program block has a combination with the program block, which is not conducive to maintenance and expansion. Therefore, with the idea of object-oriented.
The whole program should be abstracted into some classes, and each implementation of some functions, such as a display class for display, an operation class for calculation, and an operator class, etc, when a program needs to expand and add a function, we only need to add a new class. It has nothing to do with anything else, so it is very easy to maintain and expand. The modified code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace factory pattern _ calculator 2 {class Program {static void Main (string [] args) {try // if an error occurs, prompt {
// Enter the Console. writeLine ("Enter the number A"); double _ numA = Convert. toDouble (Console. readLine (); Console. writeLine ("Enter the operator (+,-, *,/)"); string _ operate = Convert. toString (Console. readLine (); Console. writeLine ("Enter the number B"); double _ numB = Convert. toDouble (Console. readLine (); double _ result; // The variable Operation execution that saves the result; Operation = operationFactory. createOperate ("+"); forward. numberA = _ numA; // assign values to class attributes. numberB = _ num B; operationFactory. createOperate (_ operate); _ result = success. getResult (); Console. writeLine (_ result); // output result} catch {Console. writeLine ("incorrect input! ") ;}} Public class Operation // Operation parent class {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 ;}} public class operationAdd: Operation // addition Operation of the inheritance Operation parent class {public override double GetResult () {double result = 0; result = numberA + numberB; return result ;}} public class operationSub: Operation // The subtraction Operation of the inherited Operation parent class {public override double GetResult () {double result = 0; result = numberA-numberB; return result ;}} public class operationMul: Operation // Multiplication Operation of the inherited Operation parent class {public override double GetResult () {double result = 0; result = numberA * numberB; return result;} public class operationDiv: Operation // inherit the division Operation of the parent class {public override double GetResult () {double result; result = numberA/numberB; return result;} public class operationFactory // class for processing operators {public static Operation createOperate (string operate) {Operation operator = null; switch (operate) {case "+": Signature = new operationAdd (); // instantiate the addition operation break; case "-": Signature = new operationSub (); break; case "*": break = new operationMul (); break; case "/": break = new operationDiv (); break;} return break ;}}}
The program becomes layered and several classes are abstracted. In this way, you can directly add classes if you need to add more functions later. Other classes do not have any influence!