简单介绍之工厂模式

工厂模式

个人理解就是一个工厂类控制所有的实体类,所有的实体类都归这个工厂类所管理,在使用这些类的地方,不直接使用这些类,而是中间多一步骤调用工厂类函数,可能会觉得中间夹杂一层工厂类这不是多此一举嘛,可如果根据工厂类去具体实例化哪一个实体类的话会使代码显得有节凑感,也就是是代码层层调用,代码看起来整齐,这样也方便后来对代码的维护,比如对实体类的添加,客户端需要调用新的实体类的情况。

例如:

一个超类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一下,活还没干完呢,干活中。。。。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九十分115

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值