码迷,mamicode.com
首页 > Windows程序 > 详细

设计模式("大话设计模式"读书笔记 C#实现)

时间:2016-08-14 10:18:53      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

前言:毫无疑问 ,学习一些设计模式,对我们的编程水平的提高帮助很大。写这个博客的时候自己刚开始学习设计模式,难免有错,欢迎评论指正。

        我学设计模式的第一本书是“大话设计模式”。

1.为什么要学设计模式?

设计模式的存在就是为了抵御需求变更。学会了这些思想,开始一个项目的时候考虑的更多,当用户提出变更的时候项目改动更少。

2.怎么才能学会设计模式?

我不知道,不过轮子哥(vczh)文章中的一句话,我觉得对,就是:“设计模式就是因为情况复杂了所以才会出现的,所以我们只能通过复杂的程序来学习设计模式。你不管看别人的程序也好,自己写程序练习也好,那必须要复杂,复杂到你不用设计模式就做不下去,这才能起到学习设计模式的作用”。所以,我准备选择一些自己用到的设计模式,通过写代码的方式去熟悉它们。

一.简单工厂模式

工厂类

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 //Author:cuishiyu
 7 //2016.08.13
 8 namespace OperationByFactory.Class
 9 {
10     //运算工厂类
11     class OperationFactory
12     {
13         public static Operation createOperation(string operate)
14         {
15             Operation oper = null;
16             switch (operate)
17             {
18                 case "+":
19                     oper = new OperationAdd();
20                     break;
21                 case "-":
22                     oper = new OperationSub();
23                     break;
24                 case "*":
25                     oper = new OperationMul();
26                     break;
27                 case "/":
28                     oper = new OperationDiv();
29                     break;
30             }
31             return oper;
32         }
33     }
34 }
View Code

运算类

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 //Author:cuishiyu
 7 //2016.08.13
 8 namespace OperationByFactory.Class
 9 {
10     //运算类
11     class Operation
12     {
13         private double _numberA = 0;
14         private double _numberB = 0;
15 
16         public double NumberA
17         {
18             get
19             {
20                 return _numberA;
21             }
22 
23             set
24             {
25                 _numberA = value;
26             }
27         }
28 
29         public double NumberB
30         {
31             get
32             {
33                 return _numberB;
34             }
35 
36             set
37             {
38                 _numberB = value;
39             }
40         }
41 
42         public virtual double GetResult()
43         {
44             double result = 0;
45             return result;
46         }
47     }
48 }
View Code

加减乘除类

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 //Author:cuishiyu
 7 //2016.08.13
 8 namespace OperationByFactory.Class
 9 {
10     //加法类
11     class OperationAdd:Operation
12     {
13         public override double GetResult()
14         {
15             return NumberA + NumberB;
16         }
17     }
18 }
View Code
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 //Author:cuishiyu
 7 //2016.08.13
 8 namespace OperationByFactory.Class
 9 {
10     //减法类
11     class OperationSub:Operation
12     {
13         public override double GetResult()
14         {
15             return  NumberA - NumberB;
16         }
17     }
18 }
View Code
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 //Author:cuishiyu
 7 //2016.08.13
 8 namespace OperationByFactory.Class
 9 {
10     //乘法类
11     class OperationMul:Operation
12     {
13         public override double GetResult()
14         {
15             return NumberA * NumberB;
16         }
17     }
18 }
View Code
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 //Author:cuishiyu
 7 //2016.08.13
 8 namespace OperationByFactory.Class
 9 {
10     //除法类
11     class OperationDiv:Operation
12     {
13         public override double GetResult()
14         {
15             if(NumberB== 0)
16                 throw new Exception("除数不能为0");
17             return NumberA / NumberB;
18         }
19     }
20 }
View Code

 总结:简单工厂模式很简单,重要的是。这个设计模式是怎么一步步形成的、和这样做的好处有哪些。

1.可移植性号,无论是控制台程序,Windows程序,Web程序,都可以用这段代码。

2.扩展性好,更安全,以后增加平方,立方,开根号等运算的时候,增加一个相应的类,然后再Switch里增加分支就好了。同时也不用担心程序员修改原先写好的加减乘除类,使得原先的代码不会被有意或者无意的修改,所以更安全。

3.(1)编程尽可能避免重复的代码。(2)对业务进行封装,尽可能的让业务逻辑和页面逻辑分开,让它们的耦合度下降,这样更容易维护和扩展。

4.大家可以熟悉一下UML类图,画出来之后理解更直观。

设计模式("大话设计模式"读书笔记 C#实现)

标签:

原文地址:http://www.cnblogs.com/Cui-Shi-Yu/p/5756913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!