Feel the great wisdom in the process of design evolution, experience the process of music and anger memorable scenes in life. --"Dahua design pattern"
Computer programming can be written as a novel effect, the book is the biggest bright spot, but also I admire the place. After getting the book, put it down, with 23 tomatoes will be full read through the book, in the process of reading, sometimes the eyebrow meditation, not its solution, and sometimes pat legs rapid, big call Miao Zai. Although a lot of places still do not understand, but the mind has been in the process with the problem in the world wandering.
The first chapter of the book throws a question directly at the reader: the code is flawless. People who usually have the basics of programming give a negative answer, but the understanding of the problem is clearly not deep enough, so the following example will make you unforgettable.
If you go to the software company for an interview, the interviewer gives you a topic: please use C + +, Java, C # or vb.net, such as any one object-oriented language to implement a calculator console program, require input two numbers and operation symbols, output results.
Perhaps when you see this topic, the heart will be secretly pleased: so easy. Then you envelope, giving a plan that you think is satisfactory:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks; Namespace Amateur Calculator {class Program {static void Main (string[] args) {Console.Write ("Please enter
Number A: ");
String A = Console.ReadLine ();
Console.Write ("Please enter the operational notation (+ 、-、 *,/):");
String B = Console.ReadLine ();
Console.Write ("Please enter the number B:");
String C = Console.ReadLine ();
String D = "";
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 (B = = "/") D = Convert.ToString (convert.todouble (A)/convert.todouble (C));
Console.WriteLine ("The result is:" + D); }
}
}
Let's not say that the solution characters given above do not conform to object-oriented thinking, from the code itself to see the existence of the naming is not standardized, if the redundancy, the code a large number of repetitions, as well as the exception to deal with a series of problems, the biggest problem is that the object-oriented, and so on, the interview results can be imagined.
So what does a professional person give a solution like, look at the following code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace ConsoleApplication5 {class Program {static void Main (string[] args) {
Operation Oper=null;
Double x, y;
String o;
Console.WriteLine ("Please enter operator:");
o = convert.tostring (Console.ReadLine ());
Oper = Operationfactory.createoperate (o);
Console.WriteLine ("Please enter the shipping Count 1:"); X =convert.
ToDouble (Console.ReadLine ());
Console.WriteLine ("Please enter the shipping Count 2:"); Y =convert.
ToDouble (Console.ReadLine ()); Oper.
Numbera = x; Oper.
Numberb = y; Double result = oper.
GetResult ();
Console.WriteLine ("The result of the operation is: {0}", results);
Console.read ();
}//operation the Operation class public class Operation {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;
}//Subtraction classes class Operationadd:operation {public override double GetResult () {
Double result = 0;
result = Numbera + Numberb;
return result; } class Operationsub:operation {public override double GetResult () {Double R
Esult = 0;
result = Numbera-numberb;
return result; } class Operationmul:operation {public override double GetResult () {Double R
Esult = 0;
result = Numbera * NUMBERB; return reSult; } class Operationdiv:operation {public override double GetResult () {Double R
Esult = 0; if (Numberb = 0) throw new Exception ("divisor cannot be 0.")
");
result = Numbera/numberb;
return result; }///simple Operation factory class public class Operationfactory {public static Operation Createoperate (string operate
) {Operation oper = null;
Switch (operate) {case "+": Oper = new Operationadd ();
Break
Case "-": Oper = new operationsub ();
Break
Case "*": oper = new Operationmul ();
Break
Case "/": Oper = new Operationdiv ();
Break
return oper; }
}
}
In comparison, you will find that three characteristics of object-oriented encapsulation, inheritance, polymorphism in the latter embodiment of the most vividly, and its code maintainability, reusability, scalability and flexibility, etc. are very high, the biggest bright spot is to increase the simple operation of the factory class, this is a design pattern embodiment, That is, a separate class to complete the process of instantiating objects, in layman's terms is the product design (parameters) to the factory, the factory is responsible for the production of products, this is the so-called factory model.