Home  >  Article  >  Backend Development  >  What is the C# simple factory pattern?

What is the C# simple factory pattern?

零下一度
零下一度Original
2017-06-23 16:18:402269browse

Title: Make a small program for shopping mall cashiers. Possible situations include: normal charges, 10% off discount, 30% off discount, 50% off for purchases over 300, etc. Different promotions are subject to change at any time.

The interface is as follows:

Analysis:

First, we write a parent class CashSuper for collecting money. This parent class is used to include various other charging methods: normal charging, 30% discount, 20% discount, 10% discount, 50% off for purchases over 300, 70% off for purchases over 400, 100% off for purchases over 500, etc., among which discounts are provided Although different, the types are similar, and the full discount discounts are the same. Therefore, we can divide normal charges, discount offers and full discount offers into three different categories: CashNormal, CashRebate, and CashReturn.

The parent class CashSuper is used for inheritance, so we set it to abstract for rewriting. Secondly, the three subclasses contained in this parent class will all call one parameter together: that is the actual The price of the goods needs to be received, so our parameters only need to pass in the common parameter: acceptMoney.

1 abstract class CashSuper2     {3         public abstract double acceptCash(double acceptMoney);4     }

Then there is the normal charging: CashNormal

First of all, the parameters he obtains from the parent class are the prices of the goods that actually need to be received. He charges normally without any Discount, so just return the value passed in from the parent class.

1     class CashNormal : CashSuper2     {3         public override double acceptCash(double acceptMoney)4         {5             return acceptMoney;6         }7     }

Discount: CashRebate

It is similar to normal charging. It inherits from the parent class CashSuper, and will get the parameters from the parent class to get the actual payment required. The price of the product, but what he needs to achieve is to discount the product, so he needs to define a discount parameter himself, so that when others call him, the discount parameter is passed in, and he can give feedback to the user by discounting the original price. .

 1     class CashRebate : CashSuper 2     { 3         //这就是cashrebate的属性了 4         private double monRebate = 1; 5  6         //调用CashRebate的时候需要从外面将优惠程度传进来 7         public CashRebate(string moneyRebate) 8         { 9             this.monRebate = double.Parse(moneyRebate);10         }11 12         public override double acceptCash(double acceptMoney)13         {14             return acceptMoney * monRebate;15         }16     }

Full discount discount: CashReturn

This is similar to the discount offer, except that it has two parameters: the horizontal line of full discount, and the amount of discount. Therefore, just define two parameters for this class.

 1     class CashReturn : CashSuper 2     { 3         //这就是cashreturn的属性了 4         private double CashLevel = 0; 5         private double MoneyReturn = 0; 6  7         //对外调用函数所以必须是public 8         public CashReturn(string level,string MonReturn) 9         {10             this.CashLevel = double.Parse(level);11             this.MoneyReturn = double.Parse(MonReturn);12         }13 14         public override double acceptCash(double acceptMoney)15         {16             double result = acceptMoney;17             if (acceptMoney >= CashLevel)18             {19                 result = acceptMoney - Math.Floor(acceptMoney / CashLevel) * MoneyReturn;20             }21             return result;22         }23     }

Now we have several discounts, but we need to judge when to call which discount. For this, we pass the user's choice and the user will transmit the selected discount. Come over, let's determine which discount method to call. This is to use the simple factory mode to encapsulate all the discount methods before calling them further.

 1     class CashFactory 2     { 3         //CashSuper现在就类似double之类,返回值就是CashSuper 4         public static CashSuper createCashAccept(string type) 5         { 6             CashSuper cs = null; 7  8             switch (type) 9             {10                 case "正常收费":11                     cs = new CashNormal();12                     break;13                 case "满300减50":14                     cs = new CashReturn("300", "50");15                     break;16                 case "满500减100":17                     cs = new CashReturn("500", "100");18                     break;19                 case "满400减70":20                     cs = new CashReturn("400", "70");21                     break;22                 case "满900减200":23                     cs = new CashReturn("900", "200");24                     break;25                 case "八折优惠":26                     cs = new CashRebate("0.8");27                     break;28                 case "九折优惠":29                     cs = new CashRebate("0.9");30                     break;31                 case "七折优惠":32                     cs = new CashRebate("0.7");33                     break;34             }35             return cs;36         }37     }

Finally, just call the above function in the user interface.

 1         private void ok_button_Click(object sender, EventArgs e) 2         { 3             /**对外这边需要了解两个函数 4              * 1.CashFactory.createCashAccept,这个是为了确定每一次的购物时调用的哪一种优惠方式 5              * 2.csuper.acceptCash,这个是为了获得每种优惠方式获得的优惠结果 6              */ 7             CashSuper csuper = CashFactory.createCashAccept(typecomboBox.SelectedItem.ToString()); 8              9             totalPrices = csuper.acceptCash(double.Parse(unitPrice_textBox.Text) * double.Parse(amount_textBox.Text));10             total += totalPrices;11             listBox1.Items.Add("单价:" + unitPrice_textBox.Text.ToString() + " 数量:" + amount.ToString() + "  " + typecomboBox.SelectedItem.ToString() + " 合计:" + totalPrices.ToString());12             resultLabel.Text = total.ToString();13         }

The above is the detailed content of What is the C# simple factory pattern?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn