简单工产模式,主要是返回具有相同方法的类的实例。
可用于:不同派生的子类,和共享接口的类。
思考中,同类的不同重载能不能用简单工厂来实现呢?
编了个小程序去实现简单工产模式。顺便复习了一下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;
}
}
}
}
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;
}
}
}
}