What is the C # simple factory model?

Source: Internet
Author: User
Title:Do a shopping mall cashier's small program, may appear the situation includes: normal charges, 90 percent discount, 70 percent discount, full 300 minus 50 and other different changes at any time of the preferential activities.

The interface is as follows:

Analysis:

First of all, we write a parent class Cashsuper for collecting money. This parent class is used to include other kinds of charges: normal charges, 70 percent discount, 80 percent discount, 90 percent discount, 300 minus 50, full 400 minus 70, 500 minus 100, etc., although the discount is different, but the type is similar, full reduction of the same, so we can be normal charges, Discount and concession are divided into three different classes: Cashnormal, Cashrebate, Cashreturn.

and Cashsuper This parent class is used for inheritance, so we set to abstract to be overridden, and then the parent class contains three subclasses will be called together to a parameter: that is the actual need to receive the price of the goods, So our parameters only need to pass in the common parameters: Acceptmoney.

1 abstract class CashSuper2     {3 public         abstract double Acceptcash (double Acceptmoney); 4     }

Then there is the normal charge: Cashnormal

First he obtains the parameter from the parent class is actually needs to receive the commodity the price, he oneself matter normal charge does not have any preferential, therefore directly returns the value which passes in from the parent class.

1     class Cashnormal:cashsuper2     {3 public         override double Acceptcash (double Acceptmoney) 4         {5             return acceptmoney;6         }7     }

Discount: cashrebate

He is similar to the normal charge, inherited from the parent class Cashsuper, will get the parameters from the parent class, get the actual need to receive the price of the goods, but he needs to achieve is the product discount, so he needs to define a discount parameters, so that other people call him when the discount parameters passed in, He can get back to the user by giving a discount on the original price.

1     class Cashrebate:cashsuper 2     {3         ///This is the attribute of Cashrebate 4         private double monrebate = 1;  5 6         / /Call Cashrebate when you need to transfer the benefits from the outside 7 public         cashrebate (string moneyrebate) 8         {9             this.monrebate = Double. Parse (moneyrebate);         }11 public         override double Acceptcash (double Acceptmoney)             Acceptmoney * monrebate;15         }16     }

Full reduced offer: Cashreturn

This is similar to the discount offer, except that he has two parameters: a full-cut horizontal line, and a reduction in the number of buttons. It is therefore possible to define two parameters for such a class.

1     class Cashreturn:cashsuper 2     {3         ///This is the attribute of Cashreturn 4         private Double cashlevel = 0;         5 Private Dou ble moneyreturn = 0; 6  7         //External call function so it must be public 8 public         Cashreturn (String level,string monreturn) 9         {Ten this             . Cashlevel = Double. Parse (level);             Moneyreturn = Double. Parse (Monreturn);         }13 public         override double Acceptcash (double Acceptmoney)             result = Acceptmoney;17             if (Acceptmoney >= cashlevel),             {                 result = Acceptmoney-math.floor ( Acceptmoney/cashlevel) * moneyreturn;20             }21             return result;22         }23     }

Now get a few concessions, but we need to determine when we need to call what kind of preferential situation, which we through the user's choice, the user will choose the preferential way to transfer over, we then determine which preferential way to call, this is the use of simple Factory mode, all the preferential way to encapsulate, in further calls.

 1 class Cashfactory 2 {3//cashsuper is now similar to double, the return value is cashsuper 4 public static Cashsuper creat                 Ecashaccept (String type) 5 {6 Cashsuper cs = NULL; 7 8 switch (type) 9 {10 Case "normal charge": one cs = new Cashnormal (); break;13 CA Se "300 minus": cs = new Cashreturn ("+"), break;16 case "500 minus": + cs = new Cashreturn ("$", "+"); break;19 case " Full 400 minus ": cs = new Cashreturn (" "", "" "), Break;22 case" full 90 0 minus ": 80 percent CS = new Cashreturn (" "" "," "" "), and break;25 case" discount                     ": + cs = new Cashrebate (" 0.8 "), break;28 case" 90 percent discount ": 29 CS = new CaShrebate ("0.9"), break;31 case "70 percent discount": cs = new Cashrebate ("0. 7 "); break;34}35 return cs;36}37}

Finally, the above function is called on the user interface.

1         private void Ok_button_click (object sender, EventArgs e) 2         {3             /** the external side needs to know two functions 4              * 1. Cashfactory.createcashaccept, this is in order to determine which of the 5 * 2.csuper.acceptcash are available for each purchase              , this is for the benefit of each offer 6              */7             Cashsuper csuper = cashfactory.createcashaccept (typecomboBox.SelectedItem.ToString ()); 8              9             Totalprices = Csuper.acceptcash (double. Parse (unitprice_textbox.text) * Double. Parse (Amount_textbox.text)),             total + = totalprices;11             listBox1.Items.Add ("Unit Price:" + Unitprice_ TextBox.Text.ToString () + "Quantity:" + amount. ToString () + "  " + typecomboBox.SelectedItem.ToString () + "total:" + totalprices.tostring ())             Resultlabel.text = total. ToString ();         
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.