设计模式学习之一 —— 简单工产模式

简单工产模式,主要是返回具有相同方法的类的实例。

可用于:不同派生的子类,和共享接口的类。

思考中,同类的不同重载能不能用简单工厂来实现呢?

编了个小程序去实现简单工产模式。顺便复习了一下exception和继承的用法

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  DesignPattern1
{
    
class SimpleFactory
    
{
        
/// <summary>
        
/// 这个小程序是取自彩票程序的一个功能;
        
/// 在数字型彩票分析中,经常要将各位的数字,分成不同的类型,例如,大中小,奇偶,被三除余012等;
        
/// 本程序只实现了其中一个功能,将输入的数字,分类为被三除余012这三类,并用ABC代表。
        
/// 
        
/// 
        
/// </summary>
        
/// <param name="args"></param>

        static void Main(string[] args)
        
{
            
//Exception的运用
            try
            
{
                
//
                
//读入数据
                Console.Write("请输入数字A:");
                
string strNumberA = Console.ReadLine();
                Console.WriteLine(
"你输入的是" + strNumberA);
                
string strType = "";

                
//
                
//创建类Operation的一个实例,并将输入数字填入
                Operation oper = new Operation();
                oper.NumberA 
= Convert.ToInt32(strNumberA);
                
//通过Factory模式,选择对应的类型。
                oper = OperationFactory.createOperate(oper.NumberA);
                
//返回类型并输出。
                strType = oper.GetType();
                Console.WriteLine(
"你输入的数字属于类型" + strType);
                Console.ReadLine();
            }


            
catch (Exception ex)
            
{
                Console.WriteLine(
"你的输入有错:" + ex.Message);
                Console.ReadLine();
            }

        }


        
public class Operation
        
{
            
private int numberA = 0;
            
public int NumberA
            
{
                
get return numberA; }
                
set { numberA = value; }
            }

            
public virtual string GetType()
            
{
                
string numType = "";
                
return numType;
            }

        }

        
class OperationTypeA : Operation
        
{
            
public override string GetType()
            
{
                
string numType = "";
                numType 
= "A";
                
return numType;
            }

        }

        
class OperationTypeB : Operation
        
{
            
public override string GetType()
            
{
                
string numType = "";
                numType 
= "B";
                
return numType;
            }

        }

        
class OperationTypeC : Operation
        
{
            
public override string GetType()
            
{
                
string numType = "";
                numType 
= "C";
                
return numType;
            }

        }

        
public class OperationFactory
        
{
            
public static Operation createOperate(int number)
            
{
                
int operate = number % 3;
                Operation oper 
= null;
                
switch (operate)
                
{
                    
case 0:
                        oper 
= new OperationTypeA();
                        
break;
                    
case 1:
                        oper 
= new OperationTypeB();
                        
break;
                    
case 2:
                        oper 
= new OperationTypeC();
                        
break;
                }

                
return oper;
            }


        }


    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值