首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

深入繼承:抽象類別與介面實戰案例(二)下——書店業務邏輯與工廠模式完整實作

     編輯:關於C語言

實作基底類別中的抽象方法並分派任務給衍生類別

#region 實現基類中的抽象方法以及將任務分派到下面的派生類去
/// <summary>
/// 實現基類的抽象方法,但考慮還需要下一層派生類完成
/// 所以選擇讓它呼叫其他可被派生類修改的方法
/// </summary>
public override double GetMoney() { return TGetMoney(); }
public override string Execute() { return TExecute(); }

/// <summary> 在此不處理,交由處理它的派生類完成 </summary>
public virtual double TGetMoney() { return 0; }
/// <summary> 在此不處理,交由處理它的派生類完成 </summary>
public virtual string TExecute() { return ""; }
#endregion
}
}

篇幅限制,不多贅述,相信真實案例代碼展示加上自行總結會比我解說更好。實作類別代碼如下:

實作買書邏輯

namespace EBook
{
/// <summary> 處理會員購買書,假設只買一本 </summary>
public class Buy : Sell
{
    public Buy(B_Type bt, string un, string bn, double p) : base(un, bn, p)
    { _btype = bt; _uname = un; _bookname = bn; _price = p; }

    public override double TGetMoney()
    {
        switch (_btype)
        {
            case B_Type.novel: _cash = _price * 0.5; break;
            case B_Type.life: _cash = _price * 0.8; break;
            default: _cash = _price; break;
        }
        return _cash;
    }
    public override string TExecute()
    {
        return "尊敬的會員:" + _uname + ",您購買《" + _bookname + "》,定價為:" + _price + ",折扣後為:" + GetMoney().ToString();
    }
}

/// <summary> 處理零售客戶購買,邏輯類似但折扣不同 </summary>
public class SBuy : Sell
{
    public SBuy(B_Type bt, string un, string bn, double p) : base(un, bn, p)
    { _btype = bt; _uname = un; _bookname = bn; _price = p; }

    public override double TGetMoney()
    {
        switch (_btype)
        {
            case B_Type.novel: _cash = _price * 0.6; break;
            case B_Type.life: _cash = _price * 0.9; break;
            default: _cash = _price; break;
        }
        return _cash;
    }
    public override string TExecute()
    {
        return "尊敬的顧客:" + _uname + ",您購買《" + _bookname + "》,定價為:" + _price + ",折扣後為:" + GetMoney().ToString();
    }
}
}

實作租書邏輯

namespace EBook
{
/// <summary> 租書:不需支付租金,但需支付押金 </summary>
public class Rent : Hire
{
    public Rent(string un, string bn, double p)
    { _uname = un; _bookname = bn; _price = p; }
    public override double TGetMoney() { return _cash; }
    public override string TExecute()
    {
        return "尊敬的顧客,您租借《" + _bookname + "》,本書定價為:" + _price + "元,請支付押金 " + _price + "元,實際支付" + TGetMoney().ToString() + "元";
    }
}

/// <summary> 會員還書:退還押金並支付租金(會員租金較低) </summary>
public class M_Back : Hire
{
    public M_Back(string un, string bn, int day, B_Type bt)
    { _uname = un; _bookname = bn; _day = day; _btype = bt; }
    public override double TGetMoney() { return _deposit - GetRent(); }
    private double GetRent()
    {
        switch (_btype)
        {
            case B_Type.novel: _cash = Convert.ToDouble(_day) * 0.1; break;
            case B_Type.life: _cash = Convert.ToDouble(_day) * 0.5; break;
            case B_Type.magazine: _cash = Convert.ToDouble(_day) * 0.1; break;
        }
        return _cash;
    }
    public override string TExecute()
    {
        return "尊敬的顧客,您租借《" + _bookname + "》,共計:" + _day + "天,已支付押金" + _deposit + ",實際產生租金" + GetRent() + "元,應找您" + TGetMoney() + "元";
    }
}

/// <summary> 普通顧客還書:租金標準較高 </summary>
public class S_Back : Hire
{
    public S_Back(string un, string bn, int day, B_Type bt)
    { _uname = un; _bookname = bn; _day = day; _btype = bt; }
    public override double TGetMoney() { return _deposit - GetRent(); }
    private double GetRent()
    {
        switch (_btype)
        {
            case B_Type.novel: _cash = Convert.ToDouble(_day) * 0.5; break;
            case B_Type.life: _cash = Convert.ToDouble(_day) * 1d; break;
            case B_Type.magazine: _cash = Convert.ToDouble(_day) * 0.3; break;
        }
        return _cash;
    }
    public override string TExecute()
    {
        return "尊敬的顧客,您租借《" + _bookname + "》,共計:" + _day + "天,已支付押金" + _deposit + ",實際產生租金" + GetRent() + "元,應找您" + TGetMoney() + "元";
    }
}
}

定義工廠類別,依會員類型回傳具體類別

using EBook;
/// <summary> 工廠類別:依會員類型回傳對應的具體類別 </summary>
public class Factory
{
    private U_Type _utype; private B_Type _btype; private S_Type _stype;
    private H_Type _htype; private string _bookname; private string _uname;
    private double _price; private int _day;

    public Factory(U_Type ut, B_Type bt, S_Type st, string bn, string un, double p)
    { _utype = ut; _btype = bt; _stype = st; _bookname = bn; _uname = un; _price = p; }

    public H_Type Htype { get { return _htype; } set { _htype = value; } }
    public int Day { get { return _day; } set { _day = value; } }

    /// <summary> 依交易類型判斷呼叫哪個方法 </summary>
    public Root HorS()
    { if (_stype == S_Type.hire) return GetHire(); else return GetBuy(); }

    /// <summary> 依會員類型回傳對應的已初始化類別 </summary>
    private Sell GetBuy()
    { if (_utype == U_Type.member) return new Buy(_btype, _uname, _bookname, _price); else return new SBuy(_btype, _uname, _bookname, _price); }

    /// <summary> 依租/還書與顧客類型初始化對應類別 </summary>
    private Hire GetHire()
    {
        if (_htype == H_Type.rent) return new Rent(_uname, _bookname, _price);
        else
        {
            if (_utype == U_Type.member) return new M_Back(_uname, _bookname, _day, _btype);
            else return new S_Back(_uname, _bookname, _day, _btype);
        }
    }
}

至此,書店程式邏輯設計完畢。具體調用代碼不再贅述,歡迎共同探討學習。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved