"Big Talk Design Mode" Simple Factory Mode — An Example of Shopping Mall Promotion System

Store cashier software

For a cash register interface like the following, if discounts, full reductions, and other activities are added, first use a simple factory to realize it. Insert picture description here
Abstract class of cash charges:

abstract class CashSuper
{
    
    
	public abstract double acceptCash(double money);
}

Normal charge sub-categories:

class CashNormal:CashSuper()
{
    
    
	public override double acceptCash(double money)
	{
    
    
		return money;
	}
}

Discount fee sub-categories:

class CashRebate:CashSuper
{
    
    
	private double moneyRebate = id;
	public CashRebate(string moneyRebate)
	{
    
    
		this.moneyRebate = double.Parse(moneyRebate);
	}
	public override double acceptCash(double money)
	{
    
    
		return money * moneyRebate;
	}
}

Rebate fee sub-categories:

class Cashreturn:cashSuper
{
    
    
	private double moneyCondition = 0.0d;
	private double moneyReturn = 0.0d;
	public CashReturn(string moneyCondition,string moneyReturn)
	{
    
    
		this.moneyCondition = double.Parse(moneyCondition);
		this.moneyReturn = double.Parse(moneyReturn);
	}
	public override double acceptCash(double money)
	{
    
    
		double return = money;
		if(money >= moneyCondition)
			result = money - Math.Floor(money / moneyCondition) * moneyReturn;
		
		return result;
	}
}

Cash toll factory:

class CashFactory
{
    
    
	public static CashSuper createCashAccept(string type)
	{
    
    
		CashSuper cs = null;
		swith(type)
		{
    
    
			case "正常收费":
				cs = new CashNormal();
				break;
			case "满300返100":
				CashReturn cr1 = new CashReturn("300","100");
				cs = cr1;
				break;
			case "打8折":
				CashRebate cr2 = new CashReturn("0.8");
				cs = cr1;
				break;
		}
		return cs;
	}
}

Client main program (main part):

double total = 0.0d;
private void btn0k_Click(object sender,EventArgs e)
{
    
    
	CashSuper csuper = CashFoctory.createCashAccept(cbxType,SelectedItem.ToString());
	double totalPrices = 0d;
	totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice,Text) 
	* Convert.ToDouble(txtNum.Text));
	total = total + totalPrices;
	lbxList.Item.Add("单价:" + textPrice.Text + "数量:" + txtNum.Text + " "
	+ cbxType.SelectedItem + "合计:" + totalPrice,ToStirng());
	lblResult.Text = total.ToString();
}
Object-oriented programming does not mean that the more classes, the better. The division of classes is for encapsulation, but the basis of classification is abstraction. Classes are abstract collections of objects with the same properties and functions.

But is there any limitation to this way of writing? If you add another promotion method, for example, if you need 100 points and 10 points, what if you want to receive prizes later? Every time I maintain and expand the charging method, I have to change this factory, so that the code needs to be recompiled and deployed... So next I will continue to improve the strategy-based writing mode.

Guess you like

Origin blog.csdn.net/A_Tu_daddy/article/details/103360875