zoukankan      html  css  js  c++  java
  • 常用设计模式(一)

    设计模式

    1、 目的

    为了代码复用,增强代码可维护性。

    2、 UMl类图(http://uml.riaoo.com/)

    统一建模语言,是客户需求和实际开发的桥梁。

    2.1类

    分为三层,第一层显示类的名称,如果是抽象类,则用斜体显示,第二层是类的特性,通常就是字段和属性,第三层是类的操作,通常是方法和行为,前面是+表示public,-表示private,#表示protected。

     

    2.2 接口

    顶部有<<interface>>

    第一行:接口名称

    第二行:接口方法

     

       2.3 继承

      

       3.3 关联关系

      

       3.4 聚合关系

       A对象可以包含B对象,但B对象不是A对象的一部分,

      

       3.5 组合关系

       A是B的一部分,部分和整体的关系。

      

       3.6 依赖关系

      

       3.7 实现

      

    3、 简单工厂模式

    Demo1

    Operation类:

     1 /// <summary>
     2     /// 运算类
     3     /// </summary>
     4     public class Operation
     5     {
     6         private double _numberA = 0;
     7 
     8         public double NumberA
     9         {
    10             get { return _numberA; }
    11             set { _numberA = value; }
    12         }
    13         private double _numberB = 0;
    14 
    15         public double NumberB
    16         {
    17             get { return _numberB; }
    18             set { _numberB = value; }
    19         }
    20         public virtual double GetRuselt()
    21         {
    22             double result = 0;
    23             return result;
    24         }
    25     }
    View Code

    OperationFactory类:

     1  /// <summary>
     2     /// 简单工厂类
     3     /// </summary>
     4     public class OperationFactory
     5     {
     6         public static Operation CreateOperate(string operate)
     7         {
     8             Operation oper=null;
     9             switch (operate)
    10             { 
    11                 case "+":
    12                     oper = new OperationAdd();
    13                     break;
    14                 case "-":
    15                     oper = new OperationSub();
    16                     break;
    17                 case "*":
    18                     oper = new OperationMul();
    19                     break;
    20                 case "/":
    21                     oper = new OperationDiv();
    22                     break;
    23             }
    24             return oper;
    25         }
    26     }
    View Code

    OperationAdd类:

     1 /// <summary>
     2     /// 加法类
     3     /// </summary>
     4     public class OperationAdd:Operation
     5     {
     6         public override double GetRuselt()
     7         {
     8             double result = 0;
     9             result = NumberA + NumberB;
    10             return result;
    11         }
    12     }
    View Code

    OperationDiv类

     1 /// <summary>
     2     /// 除法类
     3     /// </summary>
     4     public class OperationDiv : Operation
     5     {
     6         public override double GetRuselt()
     7         {
     8             double result = 0;
     9             if (NumberB == 0)
    10             {
    11                 throw new Exception("除数不能为0");
    12             }
    13             result = NumberA / NumberB;
    14             return result;
    15         }
    16     }
    View Code

    OperationMul类

     1  /// <summary>
     2     /// 乘法类
     3     /// </summary>
     4     public class OperationMul : Operation
     5     {
     6         public override double GetRuselt()
     7         {
     8             double result = 0;
     9             result = NumberA * NumberB;
    10             return result;
    11         }
    12     }
    View Code

    OperationSub类:

     1   /// <summary>
     2     /// 减肥类
     3     /// </summary>
     4     public class OperationSub : Operation
     5     {
     6         public override double GetRuselt()
     7         {
     8             double result = 0;
     9             result = NumberA - NumberB;
    10             return result;
    11         }
    12     }
    View Code

    调用:

     1  public partial class Form1 : Form
     2     {
     3         public string strOper;
     4         public double dNumber1;
     5         public double dNumber2;
     6         public string sLinkNumber;
     7         public bool b = false;
     8         public Form1()
     9         {
    10             InitializeComponent();
    11         }
    12 
    13         /// <summary>
    14         /// 运算符
    15         /// </summary>
    16         /// <param name="sender"></param>
    17         /// <param name="e"></param>
    18         private void button4_Click(object sender, EventArgs e)
    19         {
    20             textBox1.Text = "";
    21             sLinkNumber = "";
    22             Button btn = sender as Button;
    23             strOper = btn.Text;
    24             b = true;
    25         }
    26 
    27 
    28         /// <summary>
    29         /// 等于
    30         /// </summary>
    31         /// <param name="sender"></param>
    32         /// <param name="e"></param>
    33         private void button15_Click(object sender, EventArgs e)
    34         {
    35             if (b == false)
    36             {
    37                 return;
    38             }
    39             Operation oper;
    40             oper = OperationFactory.CreateOperate(strOper);
    41             oper.NumberA = dNumber1;
    42             oper.NumberB = dNumber2;
    43             textBox1.Text = oper.GetRuselt().ToString();
    44             sLinkNumber = textBox1.Text;
    45             b = false;
    46         }
    47         public void Clear()
    48         {
    49             b = false;
    50             textBox1.Text = "";
    51             dNumber1 = 0;
    52             dNumber2 = 0;
    53             sLinkNumber = "";
    54         }
    55         /// <summary>
    56         /// 数字
    57         /// </summary>
    58         /// <param name="sender"></param>
    59         /// <param name="e"></param>
    60         private void button1_Click(object sender, EventArgs e)
    61         {
    62             Button btn = sender as Button;
    63             sLinkNumber += btn.Text;
    64             if (b)
    65             {
    66                 dNumber2 = Convert.ToDouble(sLinkNumber);
    67             }
    68             else
    69             {
    70                 dNumber1 = Convert.ToDouble(sLinkNumber);
    71             }
    72             textBox1.Text = sLinkNumber;
    73         }
    74 
    75         private void button13_Click(object sender, EventArgs e)
    76         {
    77             Clear();
    78         }
    79     }
    View Code

    Demo2

       面向对象编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类。

    CashSuper类:

    1 /// <summary>
    2     /// 现金收取抽象类
    3     /// </summary>
    4     public abstract class CashSuper
    5     {
    6         public abstract double AcceptCach(double money);
    7     }
    View Code

    CashContext类:

     1  public class CashContext
     2     {
     3         private CashSuper cs = null;
     4         public CashContext(string type)
     5         {
     6             switch (type)
     7             {
     8                 case "正常收费":
     9                     CashNormal cs0 = new CashNormal();
    10                     cs = cs0;
    11                     break;
    12                 case "满300返100":
    13                     CashReturn cs1 = new CashReturn("300","100");
    14                     cs = cs1;
    15                     break;
    16                 case "打八折":
    17                     CashRebate cs2 = new CashRebate("0.8");
    18                     cs = cs2;
    19                     break;
    20             }
    21         }
    22         public double GetRusult(double money)
    23         {
    24             return cs.AcceptCach(money);
    25         }
    26 
    27     }
    View Code

    CashNormal类:

     1  /// <summary>
     2     /// 正常收费子类
     3     /// </summary>
     4     public class CashNormal:CashSuper
     5     {
     6         public override double AcceptCach(double money)
     7         {
     8             return money;
     9         }
    10     }
    View Code

    CashRebate类:

     1 /// <summary>
     2     /// 打折收费子类
     3     /// </summary>
     4     public class CashRebate:CashSuper
     5     {
     6         private double moneyRebate = 1d;
     7         public CashRebate(string moneyRebate)
     8         {
     9             this.moneyRebate = double.Parse(moneyRebate);
    10         }
    11         public override double AcceptCach(double money)
    12         {
    13             return money * moneyRebate;
    14         }
    15     }
    View Code

    CashReturn类:

     1 /// <summary>
     2     /// 返利收费子类
     3     /// </summary>
     4     public class CashReturn:CashSuper
     5     {
     6         private double moneyCondition = 0.0d;
     7         private double moneyReturn = 0.0d;
     8         public CashReturn(string moneyCondition, string moneyReturn)
     9         {
    10             this.moneyCondition = double.Parse(moneyCondition);
    11             this.moneyReturn = double.Parse(moneyReturn);
    12         }
    13         public override double AcceptCach(double money)
    14         {
    15             double result = money;
    16             if (money >= moneyCondition)
    17             {
    18                 result = money - Math.Floor(money / moneyCondition) * moneyReturn;
    19             }
    20             return money;
    21         }
    22     }
    View Code

    调用:

     1  public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6         }
     7         double total = 0.0d;
     8         private void button1_Click(object sender, EventArgs e)
     9         {
    10             CashContext cs = new CashContext(comboBox1.SelectedItem.ToString());
    11             double totalPrices = 0d;
    12             totalPrices = cs.GetRusult(Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox2.Text));
    13             total += totalPrices;
    14             listBox1.Items.Add("单价:" + textBox1.Text + "数量:" + textBox2.Text + " " + comboBox1.SelectedItem + "合计:" + totalPrices.ToString());
    15             label5.Text = total.ToString();
    16         }
    17     }
    View Code

    4、 单例模式

    保证一个类仅有一个实例,并提供一个访问它的全局访问点。严格地控制客户怎样访问以及合适访问实例。

    Demo3

    Singleton类:

     1 public class Singleton
     2     {
     3         private static Singleton instance;
     4         private Singleton() { }//构造方法私有,就堵死了外界利用new创建此类实例的可能
     5         public static Singleton GetInstance()
     6         {
     7             if (instance == null)
     8             {
     9                 instance = new Singleton();
    10             }
    11             return instance;
    12         }
    13     }
    View Code

    调用:

     1  class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Singleton s1 = Singleton.GetInstance();
     6             Singleton s2 = Singleton.GetInstance();
     7             if (s1 == s2)
     8             {
     9                 Console.WriteLine("两个对象是相同的对象");
    10             }
    11             Console.ReadKey();
    12         }
    13     }
    View Code

    5、 适配器模式

    适配器模式使得原本由于接口不兼容而不能在一起工作的那些类可以在一起工作。

    Demo4

    Player类:

     1 public abstract class Player
     2     {
     3        protected string name;
     4        public Player(string name)
     5        {
     6            this.name = name;
     7        }
     8        public abstract void Attack();//进攻方法
     9        public abstract void Defense();//防守方法
    10     }
    View Code

    Forwards类:

     1  /// <summary>
     2     /// 前锋类
     3     /// </summary>
     4     class Forwards:Player
     5     {
     6         public Forwards(string name)
     7             : base(name)
     8         { 
     9         
    10         }
    11         public override void Attack()
    12         {
    13             Console.WriteLine("前锋{0}进攻", name);
    14         }
    15         public override void Defense()
    16         {
    17             Console.WriteLine("前锋{0}防守", name);
    18         }
    19     }
    View Code

    Guards类:

     1 class Guards:Player
     2     {
     3         public Guards(string name)
     4             : base(name)
     5         { 
     6         
     7         }
     8         public override void Attack()
     9         {
    10             Console.WriteLine("后卫{0}进攻", name);
    11         }
    12         public override void Defense()
    13         {
    14             Console.WriteLine("后卫{0}防守", name);
    15         }
    16     }
    View Code

    Translator类:

     1   /// <summary>
     2     /// 翻译者
     3     /// </summary>
     4     class Translator:Player
     5     {
     6         private ForeignCenter fc = new ForeignCenter();
     7         public Translator(string name)
     8             : base(name)
     9         {
    10             fc.Name = name;
    11         }
    12         public override void Attack()
    13         {
    14             fc.进攻();
    15         }
    16         public override void Defense()
    17         {
    18             fc.防守();
    19         }
    20     }
    View Code

    ForeignCenter类:

    View Code

    调用:

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Player b = new Forwards("巴蒂尔");
     6             b.Attack();
     7             Player m = new Guards("麦克格雷迪");
     8             m.Attack();
     9 
    10             Player ym = new Translator("姚明");//翻译者告诉姚明,教练要求你进攻
    11             ym.Attack();
    12             Console.ReadKey();
    13         }
    14     }
    View Code

    6、 模版方法模式

    模板方法模式是通过把不变行为搬移到超类,去除子类中的重复代码来体现它的优势。就是提供了一个很好的代码复用平台。

    Demo5

    AbstractClass类:

     1  abstract class AbstractClass
     2     {
     3         public abstract void PrimitiveOperation1();
     4         public abstract void PrimitiveOperation2();
     5         /// <summary>
     6         /// 模板方法
     7         /// </summary>
     8         public void TemplateMethod()
     9         {
    10             PrimitiveOperation1();
    11             PrimitiveOperation2();
    12             Console.WriteLine("");
    13         }
    14     }
    View Code

    ConcreteClassA类:

     1 class ConcreteClassA:AbstractClass
     2     {
     3         public override void PrimitiveOperation1()
     4         {
     5             Console.WriteLine("具体类A方法1实现");
     6         }
     7         public override void PrimitiveOperation2()
     8         {
     9             Console.WriteLine("具体类A方法2实现");
    10         }
    11     }
    View Code

    ConcreteClassB类:

     1 class ConcreteClassB:AbstractClass
     2     {
     3         public override void PrimitiveOperation1()
     4         {
     5             Console.WriteLine("具体类B方法1实现");
     6         }
     7         public override void PrimitiveOperation2()
     8         {
     9             Console.WriteLine("具体类B方法2实现");
    10         }
    11     }
    View Code

    调用:

     1   class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             AbstractClass c;
     6             c = new ConcreteClassA();
     7             c.TemplateMethod();
     8             c = new ConcreteClassB();
     9             c.TemplateMethod();
    10             Console.ReadKey();
    11         }
    12     }
    View Code

    7、 外观模式

    为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

    Demo6

    Facade类:

     1 class Facade
     2     {
     3         SubSystemOne one;
     4         SubSystemTwo two;
     5         SubSystemThree three;
     6         SubSystemFour four;
     7         public Facade()
     8         {
     9             one = new SubSystemOne();
    10             two = new SubSystemTwo();
    11             three = new SubSystemThree();
    12             four = new SubSystemFour();
    13         }
    14         public void MethodA()
    15         {
    16             Console.WriteLine("
    方法组A()-----");
    17             one.MethodOne();
    18             two.MethodTwo();
    19             three.MethodThree();
    20             four.MethodFour();
    21         }
    22         public void MethodB()
    23         {
    24             Console.WriteLine("
    方法组B()-----");
    25             two.MethodTwo();
    26             three.MethodThree();
    27         }
    28     }
    View Code

    SubSystemOne类:

    1  class SubSystemOne
    2     {
    3         public void MethodOne()
    4         {
    5             Console.WriteLine("子系统方法一");
    6         }
    7     }
    View Code

    SubSystemTwo类:

    1  class SubSystemTwo
    2     {
    3         public void MethodTwo()
    4         {
    5             Console.WriteLine("子系统方法二");
    6         }
    7     }
    View Code

    SubSystemThree类:

    1 class SubSystemThree
    2     {
    3         public void MethodThree()
    4         {
    5             Console.WriteLine("子系统方法三");
    6         }
    7     }
    View Code

    SubSystemFour类:

    1 class SubSystemFour
    2     {
    3         public void MethodFour()
    4         {
    5             Console.WriteLine("子系统方法四");
    6         }
    7     }
    View Code

    调用:

     1  class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Facade f = new Facade();
     6             f.MethodA();
     7             f.MethodB();
     8             Console.ReadKey();
     9         }
    10     }
    View Code
  • 相关阅读:
    C#磁吸屏幕窗体类库
    准备
    我写的诗
    How to turn off a laptop keyboard
    How to tell which commit a tag points to in Git?
    Why should I care about lightweight vs. annotated tags?
    How to get rid of “would clobber existing tag”
    Facebook, Google and Twitter threaten to leave Hong Kong over privacy law changes
    The need for legislative reform on secrecy orders
    Can a foreign key be NULL and/or duplicate?
  • 原文地址:https://www.cnblogs.com/ruanmou001/p/3695772.html
Copyright © 2011-2022 走看看