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!

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use