Two design modes (1) ==& gt; & gt; "simple factory", two design modes Factory

Source: Internet
Author: User

Two design models (1) ==>> "simple factory", two design models Factory

Let's take a calculator as an example to introduce the application of the simple factory:

Effect:

Here we use inheritance, virtual methods, simple factory design patterns to complete

First, in addition to setting up the form, we should prepare some of our classes:

1. Calculate the parent class Calculation

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace calculator _ simple factory _ {// parent class (virtual) Calculation class public abstract class Calculation {// The first operand public double Numleft {get; set ;} // second operand public double NumRight {get; set ;}// Operator public string Operator {get; set ;}// virtual method public abstract double Cal ();}}

2. Then, add, subtract, multiply, and divide sub-classes.

01. Addition class Add

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace calculator _ simple factory _ {// Add public class Add: Calculation {public override double Cal () {double result = Numleft + NumRight; return result ;}}}

02. subtraction Sub

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace calculator _ simple factory _ {// subtract public class Sub: Calculation {public override double Cal () {double result = Numleft-NumRight; return result ;}}}

03. Multiplication Mul

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace calculator _ simple factory _ {// subtraction class public class Mul: Calculation {public override double Cal () {double result = Numleft * NumRight; return result ;}}}

04. Division Div

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace calculator _ simple factory _ {// division class public class Div: Calculation {public override double Cal () {// division, the divisor cannot be 0 if (NumRight = 0) {throw new Exception ("the divisor cannot be 0");} double result = Numleft/NumRight; return result ;}}}

3. Finally, we prepare the "simple factory" class.

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace calculator _ simple Factory _ {// work class public class Factory {public static Calculation Cal (string ope) {// initialize the parent class Object Calculation calculation = null; // instantiate switch (ope) {case "+": calculation = new Add (); break; case "-": calculation = new Sub (); break Based on the sent operator; case "*": calculation = new Mul (); break; case "/": calculation = new Div (); break ;} // return the instantiated object return calculation ;}}}

4. After everything is ready, let's see how to use it in the form!

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace calculator _ simple factory _ {public partial class FrmMain: Form {public FrmMain () {InitializeComponent ();} private void btnCal_Click (object sender, EventArgs e) {// outer try: determines whether the input operand is correct. try {// obtain the value to be calculated double num1 = Convert. toDouble (txtLift. text); double num2 = Convert. toDouble (txtRight. text); string ope = cmbstring. text; // transmits the obtained operator to the "simple Factory" Calculation cal = Factory. cal (ope); double result = 0; // assign a value (forgot) cal. numleft = num1; cal. numRight = num2; // The internal try function: determines whether the divisor is 0 when division {// knows the requirement and starts the corresponding work result = cal. cal ();} catch (Exception ex) {MessageBox. show (ex. message);} // display the result label1.Text = result. toString ();} catch (Exception ex) {// If the input is incorrect, the system will prompt "the input string format is incorrect" MessageBox. show (ex. message );}}}}

 

Let's summarize some notes of our "simple factory" class:

1. The method is static and does not need to be instantiated during call.

2. the return value of the method is of the parent class type.

3. The method has parameters.

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.