What is the C# simple factory pattern?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What does WeChat Do Not Disturb mode mean? Nowadays, with the popularity of smartphones and the rapid development of mobile Internet, social media platforms have become an indispensable part of people's daily lives. WeChat is one of the most popular social media platforms in China, and almost everyone has a WeChat account. We can communicate with friends, family, and colleagues in real time through WeChat, share moments in our lives, and understand each other’s current situation. However, in this era, we are also inevitably faced with the problems of information overload and privacy leakage, especially for those who need to focus or

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:
