I: The purpose of software design is to achieve software maintenance, scalability, reusability, flexibility, and other functions. The textbook demonstrates the benefits of object-oriented programming through the movable print technique.
2. Example: enter two numbers and operators to calculate the result.
Separate console input and operator operations to separate the business layer and interface logic to reduce coupling between them. Encapsulate all the parts that may change.
Operation operations
<span style="font-size:14px;"> public class Operation { public static double GetResult(double,numberA,double numberB,string operate) double result = 0d switch(operate) { case "+": resule = numberA+numberB; break; case "-": resule = numberA-numberB; break; case "*": resule = numberA*numberB; break; case "/": resule = numberA/numberB; break; } return result; }</span>Client code
<Span style = "font-size: 14px;"> static void main (string () ARGs) {try {console. write ("Enter the number A:"); string strnumbera = console. readline (); console. write ("select the operator (+-*/):"); string stroperate = console. readline (); console. write ("Enter the number B:"); string strnumberb = console. readline (); string strresult = "" strresult = convert. tostring (operation. getresult (convert. todouble (strnumbera, convert. todouble (strnumberb), stroperate);) console. writeline ("Result:" + strresult); console. readline ();} catch (isolation ation ex) {console. writeline ("your input is incorrect:" + ex. message) ;}}</span>The above Code encapsulates the operation class. However, if you add an SQRT operation, add a branch to the switch in the operation class, however, the other four operations do not need to be modified, but the code is involved in compilation. During the modification, it is easy to make no problematic code errors, exposing other important code.
How can I improve code flexibility to facilitate code modification and expansion? This method uses Object-oriented Inheritance and polymorphism, operator subclass inheritance operation class, subclass rewriting operation method of parent class, and instantiate operation subclass through factory. The following describes a simple factory model.
III:Simple factory Model
1. Define a class to create instances of other classes. The created instance usually has a common parent class or interface. The simple factory mode is also called the static factory method mode. It belongs to the class creation mode and usually returns instances of different classes according to a condition (parameter.
2 content:
Creator)
Is the core of the simple factory model. It is responsible for creating all the instances of specific product classes. The factory class can be directly called by the outside world to create the required product objects.
Abstract Product role)
Is the parent class of all specific product roles, which describes the common public interfaces of all instances.
Product role (Concrete Product)
Inherited from abstract product roles. Generally, there are multiple roles, which are the goal of creating a simple factory model. The factory class returns a specific product of this role.
3uml Diagram
The code after the reconstruction is as follows:
4. (1) operation class
<span style="font-size:14px;">Public class Operation{ private double _numberA =0; private double _numberB =0; public double NumberA { get { return _numberA;} set( _numerA = value;) } public double NumberB { get { return _numberB;} set( _numerB = value;) } public victual double GerResult() { double result = 0; return result; }}</span>
4. (2) subclass inheritance operation class
<span style="font-size:14px;">Class OperationAdd :Operation{ public override double GetResult() { double result = 0 ; result = _numberA + numberB; return result; }}Class OperationSub:Operation{ double result = 0; result = numberA - numberB; return result;}Class OperationMul : Operation{ double result = 0; result = numberA * numberB; return result;}Class OperationDiv:Operation{ double result = 0; result = numberA /numberB; return result;}</span>
In the encapsulation example, operation can be called by two numbers and operator numbers. the getresult method requires the corresponding calculation method of the corresponding budget operator, but each calculation class has its own calculation method, but how to input + -.. to obtain the corresponding operation method.
4. (3) Factory instantiation operator subclass
<Span style = "font-size: 14px;"> the public class operationfactory {public static operation createoperation (string operate) method returns an operation class {operation operator = NULL through the input parameter; switch (operate) {Case "+": condition = new operationadd (); break; Case "-": condition = new operationsub (); break; case "*" operator = new operationmul (); break; Case "/" operator = new operationdiv (); break;} return operator }}</span>The console will instantiate the corresponding object by entering the corresponding operator, and return the result of the calculator through polymorphism.
4. (4) Console
<span style="font-size:14px;">Operation oper;Oper = OperationFactory.createOperation("+");Oper.NumberA=1;Oper.numberB=2;Double result = oper.GerResult();</span>The final code can be modified only by modifying the operationadd class to the content of the + operation class. To add other operation classes, you only need to add one operation class.
Add the corresponding instantiated object in the switch.
Iv. Summary: The simple factory mode determines the specific class object to be created based on the information given by the outside world. With this feature, the outside world is isolated from specific classes, with low coupling. Clearly differentiate their respective responsibilities and powers, which is conducive to the optimization of the entire software architecture.