第一章:简单工厂模式
面试题目:
请用C++ 、Java 、C# 或者VB.NET 任意一种面向对象语言实现一个计算器控制台程序,要求输入两个数和运算符号,得到结果。
菜鸟实现方法一:
1 using System; 2 3 namespace TwoDemo 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 Console.WriteLine("请输入数字A!"); 10 string A = Console.ReadLine(); 11 Console.WriteLine("请选择运算符号(+ - * /)!"); 12 string B = Console.ReadLine(); 13 Console.WriteLine("请输入数字B!"); 14 string C = Console.ReadLine(); 15 string D = ""; 16 if (B =="+") 17 { 18 D = Convert.ToString( Convert.ToDouble(A) + Convert.ToDouble(C)); 19 } 20 if (B == "+") 21 { 22 D = Convert.ToString(Convert.ToDouble(A) + Convert.ToDouble(C)); 23 } 24 if (B == "-") 25 { 26 D = Convert.ToString(Convert.ToDouble(A) - Convert.ToDouble(C)); 27 } 28 if (B == "*") 29 { 30 D = Convert.ToString(Convert.ToDouble(A) * Convert.ToDouble(C)); 31 } 32 if (B == "/") 33 { 34 D = Convert.ToString(Convert.ToDouble(A) / Convert.ToDouble(C)); 35 } 36 37 Console.WriteLine(D); 38 Console.ReadLine(); 39 } 40 } 41 }
仔细观察会发现初学者很多的毛病:
1、变量的命名不规范。
2、上面代码的判断分支写法,意味着每个条件都要做判断,等于计算机做了三次无用功。
3、如果是除法的时候,客户输入了0怎么办,如果输入的是字符而不是数字,该怎么办?
菜鸟实现方法二:代码规范
1 using System; 2 3 namespace ThreeDemo 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 try 10 { 11 Console.WriteLine("请输入数字A!"); 12 string strNumberA = Console.ReadLine(); 13 Console.WriteLine("请选择运算符号(+ - * /)!"); 14 string strOperate = Console.ReadLine(); 15 Console.WriteLine("请输入数字B!"); 16 string strNumberB = Console.ReadLine(); 17 string strResult = ""; 18 19 switch (strOperate) 20 { 21 case"+": 22 strResult = Convert.ToString(Convert.ToDouble(strNumberA) + Convert.ToDouble(strNumberB)); 23 break; 24 case "-": 25 strResult = Convert.ToString(Convert.ToDouble(strNumberA) - Convert.ToDouble(strNumberB)); 26 break; 27 case "*": 28 strResult = Convert.ToString(Convert.ToDouble(strNumberA) * Convert.ToDouble(strNumberB)); 29 break; 30 case "/": 31 if (strNumberB!="0") 32 { 33 strResult = Convert.ToString(Convert.ToDouble(strNumberA) - Convert.ToDouble(strNumberB)); 34 35 } 36 else 37 { 38 strResult = "除数不能为0"; 39 } 40 break; 41 default: 42 break; 43 } 44 45 Console.WriteLine("结果是:"+ strResult); 46 Console.ReadLine(); 47 } 48 catch (Exception ex) 49 { 50 Console.WriteLine("您的输入有错:"+ex.Message); 51 } 52 } 53 } 54 }
菜鸟实现方法三:业务的封装,准确的说就是让业务和界面逻辑分开,让它们之间的耦合度下降,只有分离开,才可以达到容易维护或扩展。
Operation运算类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace FourDemo 6 { 7 public class Operation 8 { 9 public static string GetResult(string numberA, string numberB, string strOperate) 10 { 11 string strResult = string.Empty; 12 switch (strOperate) 13 { 14 case "+": 15 strResult = Convert.ToString(Convert.ToDouble(numberA) + Convert.ToDouble(numberB)); 16 break; 17 case "-": 18 strResult = Convert.ToString(Convert.ToDouble(numberA) - Convert.ToDouble(numberB)); 19 break; 20 case "*": 21 strResult = Convert.ToString(Convert.ToDouble(numberA) * Convert.ToDouble(numberB)); 22 break; 23 case "/": 24 if (numberB != "0") 25 { 26 strResult = Convert.ToString(Convert.ToDouble(numberA) - Convert.ToDouble(numberB)); 27 28 } 29 else 30 { 31 strResult = "除数不能为0"; 32 } 33 break; 34 default: 35 break; 36 } 37 return strResult; 38 } 39 } 40 }
客户端代码
1 using System; 2 3 namespace FourDemo 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 Console.WriteLine("请输入数字A!"); 10 string strNumberA = Console.ReadLine(); 11 Console.WriteLine("请选择运算符号(+ - * /)!"); 12 string strOperate = Console.ReadLine(); 13 Console.WriteLine("请输入数字B!"); 14 string strNumberB = Console.ReadLine(); 15 string strResult = ""; 16 strResult = Operation.GetResult(strNumberA, strNumberB, strOperate); 17 Console.WriteLine("结果是:"+strResult); 18 Console.ReadLine(); 19 } 20 } 21 }
老鸟实现方法: 紧耦合 VS 松耦合
Operation运算类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace OneDemo 6 { 7 public class Operation 8 { 9 public double NumberA { get; set; } 10 public double NumberB { get; set; } 11 public virtual double GetResult() 12 { 13 double result = 0; 14 return result; 15 } 16 } 17 }
加法类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace OneDemo 6 { 7 public class OperationAdd:Operation 8 { 9 public override double GetResult() 10 { 11 double result = 0; 12 result = NumberA + NumberB; 13 return result; 14 } 15 } 16 }
减法类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace OneDemo 6 { 7 public class OperationSub:Operation 8 { 9 public override double GetResult() 10 { 11 double result = 0; 12 result = NumberA - NumberB; 13 return result; 14 } 15 } 16 }
乘法类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace OneDemo 6 { 7 /// <summary> 8 /// 乘法类,继承运算类 9 /// </summary> 10 public class OperationMul:Operation 11 { 12 public override double GetResult() 13 { 14 double result = 0; 15 result = NumberA * NumberB; 16 return result; 17 } 18 } 19 }
除法类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace OneDemo 6 { 7 /// <summary> 8 /// 除法类,继承运算类 9 /// </summary> 10 public class OperationDiv:Operation 11 { 12 public override double GetResult() 13 { 14 double result = 0; 15 if (NumberB==0) 16 { 17 throw new Exception("除数不能为0"); 18 } 19 result = NumberA / NumberB; 20 return result; 21 } 22 } 23 }
简单工程模式:OperationFactory 类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace OneDemo 6 { 7 public class OperationFactory 8 { 9 public static Operation createOperate(string opearte) 10 { 11 Operation oper = null; 12 switch (opearte) 13 { 14 case "+": 15 oper = new OperationAdd(); 16 break; 17 case "-": 18 oper = new OperationSub(); 19 break; 20 case "*": 21 oper = new OperationMul(); 22 break; 23 case "/": 24 oper = new OperationMul(); 25 break; 26 } 27 return oper; 28 } 29 } 30 }
客户端调用
1 using System; 2 3 namespace OneDemo 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 Operation operation = null; 10 operation = OperationFactory.createOperate("+"); 11 operation.NumberA = 12; 12 operation.NumberB = 13; 13 Console.WriteLine(operation.GetResult()); 14 Console.ReadLine(); 15 } 16 } 17 }

浙公网安备 33010602011771号