码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net 设计模式 之 “简单工厂”模式

时间:2017-11-08 19:54:21      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:cas   一个   返回   ogr   thread   思想   rgs   ted   prot   

  • 主要思想:
     public static Operation CreateFactory(string ope)
        {
            //实例化空父类,让父类指向子类
            Operation op = null;
            switch (ope)
            {
                case "+":
                    op = new OperationAdd();//父类指向OperationAdd这个子类,并调用子类中的加法
                    break;
                case "-":
                    op = new OperationSub();//父类指向OperationSub这个子类,并调用子类中的减法
                    break;
            }
            return op;
        }

 

  • 相关源码:
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace FactoryModel
 8 {
 9     //====================使用C#,利用简单工厂模式,实现简单的计算器功能====================
10     //考察时知识点:面向对象三大特性——继承、封装、多态
11     /// <summary>
12     /// 1.定义父类,同时也是一个封装
13     /// </summary>
14     class Operation
15     {
16         //2.因为要让子类能够对父类进行访问,故应要将参数定义为受保护的变量类型
17         protected int numberA;
18         protected int numberB;
19         //定义属性(必写)
20         public int NumberA
21         {
22             get { return numberA; }
23             set { numberA = value; }
24         }
25         public int NumberB
26         {
27             get { return numberB; }
28             set { numberB = value; }
29         }
30         //3.封装虚方法,以供子类进行重写
31         public virtual int getResule()
32         {
33             int result = 0;
34             return result;
35         }
36     }
37     /// <summary>
38     /// 4.定义子类,继承父类,并对父类进行重写(加法)
39     /// </summary>
40     class OperationAdd : Operation
41     {
42         public override int getResule()
43         {
44             return numberA + numberB;
45         }
46     }
47     //5.定义子类,继承父类,并对父类进行重写(减法)
48     class OperationSub : Operation
49     {
50         public override int getResule()
51         {
52             return numberA - numberB;
53         }
54     }
55     //6.创建简单工厂模式
56     class Factory
57     {
58         /// <summary>
59         /// 封装返回值类型为上面“父类型”——Operation类型的方法
60         /// </summary>
61         /// <param name="ope">ope是指运算的类型,如+、-、*、/</param>
62         /// <returns></returns>
63         public static Operation CreateFactory(string ope)
64         {
65             //实例化空父类,让父类指向子类
66             Operation op = null;
67             switch (ope)
68             {
69                 case "+":
70                     op = new OperationAdd();//父类指向OperationAdd这个子类,并调用子类中的加法
71                     break;
72                 case "-":
73                     op = new OperationSub();//父类指向OperationSub这个子类,并调用子类中的减法
74                     break;
75             }
76             return op;
77         }
78     }
79     //7.主函数中进行调用
80     class Program
81     {
82         static void Main(string[] args)
83         {
84             //要用哪种运算,只需将参数传入工厂中的方法中,工厂将会自动调用相关的方法,
85             //即(父类指向相应的子类,从而调用相应的方法),进行相应的运算
86             Operation op = Factory.CreateFactory("+");
87             op.NumberA = 10;
88             op.NumberB = 30;
89             //调用父类中的方法来获取结果
90             int result = op.getResule();
91             Console.WriteLine(result);
92             Console.ReadKey();
93         }
94         //如果在后续的编程中仍需要有其他的运算,则只需要在子类中加上相应的子类,
95         //并在工厂中加上相应的case情况即可,这也就是简单工厂的“利”所在
96     }
97 }

 

Asp.Net 设计模式 之 “简单工厂”模式

标签:cas   一个   返回   ogr   thread   思想   rgs   ted   prot   

原文地址:http://www.cnblogs.com/pang951189/p/7805073.html

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