工厂模式
个人理解就是一个工厂类控制所有的实体类,所有的实体类都归这个工厂类所管理,在使用这些类的地方,不直接使用这些类,而是中间多一步骤调用工厂类函数,可能会觉得中间夹杂一层工厂类这不是多此一举嘛,可如果根据工厂类去具体实例化哪一个实体类的话会使代码显得有节凑感,也就是是代码层层调用,代码看起来整齐,这样也方便后来对代码的维护,比如对实体类的添加,客户端需要调用新的实体类的情况。
例如:
一个超类CashSuper,由其他的子类所继承
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Two
{
abstract class CashSuper
{
public abstract double acceptCash(double money);
}
}
父类中的acceptCash函数,在子类中得到重载,子类具体怎么实现根据每一个实体子类的功能来决定。
子类一 CashNormal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Two
{
class CashNormal:CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
}
CashNormal实体类对输入的变量不做处理就返回。
子类二 CashReturn
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Two
{
class CashReturn:CashSuper
{
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
public CashReturn(string moneyCondition, string moneyReturn)<span style="font-family: Arial, Helvetica, sans-serif;">//默认构造函数,在工厂类中实例化这个类实体的话需要传过来两个参数</span>
{
this.moneyCondition =double.Parse(moneyCondition);
this.moneyReturn = double.Parse(moneyReturn);
}
public override double acceptCash(double money)
{
double result=money;
if (money > moneyCondition)
{
result = money - Math.Floor(money / moneyCondition) * moneyReturn;<span style="font-family: Arial, Helvetica, sans-serif;">//大于返利金额的就把当前金额减去返利金额,满300间100,满600减200.。。。</span>
}
return result;
}
}
}
CashReturn继承父类,根据工厂类中实例化该类实体时候,在构造函数中传递的参数来具体处理输入的金额,最终在重写的aceptCash函数中返回处理后的金额。
子类三CashRebeat
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Two
{
class CashRebeat:CashSuper
{
private double moneyRebeat = 1d;
public CashRebeat(string moneyRebeat)
{
this.moneyRebeat =double.Parse(moneyRebeat);
}
public override double acceptCash(double money)
{
return money * moneyRebeat;
}
}
}
CashRebeat子类继承父类,实例化该类实体的时候,默认构造函数传递参数,在重载的acceptCash函数中作最终的金额处理并返回。
工厂类 CashFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Two
{
class CashFactory
{
public static CashSuper createCashAccept(int typeindex)
{
CashSuper cs = null;
switch (typeindex)
{
case 0:
cs = new CashNormal();
break;
case 2:
cs = new CashReturn("300","100");//默认构造函数,传递参数给实体
break;
case 1:
cs = new CashRebeat("0.8");
break;
}
return cs;
}
}
}
根据在客户端或网页中调用工厂类中的createCashAccept函数来具体实例化哪一个类
在客户端中
<pre name="code" class="csharp"> private void button1_Click(object sender, EventArgs e)
{
CashSuper csuper = CashFactory.createCashAccept(comboBox1.SelectedIndex);//根据所选择的具体实例化哪一个类,这样csuper就实例化了
double totalprice = 0d;
total = csuper.acceptCash(Convert.ToDouble(textBox1.Text)*Convert.ToDouble(textBox2.Text));//具体是哪一个类的话会在构造函数中对相应的处理变量赋值
//这样在acceptCash函数中只需要把原始金额传递进去就可以做对应的处理了
totalprice = total + totalprice;
string info = "单价:" + textBox1.Text + "数量:" + textBox2.Text + " "
+ comboBox1.SelectedItem.ToString() + " 合计:" + total.ToString();
listBox1.Items.Add(info);
label5.Text = totalprice.ToString();
}
好了,先在这Mark一下,活还没干完呢,干活中。。。。