Simple factory Mode c #,

Source: Internet
Author: User

Simple factory Mode c #,

Simple factory Mode c # simple example

Namespace simplefactory

{
Public partial class Form1: Form
{
Private void button#click (object sender, EventArgs e) // user level
{
Operation operation = OperationFactory. createOperate (this. cbbOperation. SelectedIndex); // connect to the simple factory to obtain the algorithm
Operation. NumberA = Convert.ToDouble(this.txt NumberOne. Text); // assign values to the algorithms obtained from a simple factory.
Operation. NumberB = Convert.ToDouble(this.txt NumberTwo. Text );

This.txt Result. Text = operation. getResult (). ToString (); // obtain the algorithm Result

}

}
Class OperationFactory // simple factory level
{
Public static Operation createOperate (int operate) // two types of factory division addition and subtraction
{
Operation operator = null;
Switch (operate)
{
Case 0:
{
Operator = new OperationAdd (); // Addition
Break;
}
Case 1:
{
Operator = new OperationSub (); // Subtraction
Break;
}
}
Return response;
}
}
Public abstract class Operation // The line abstraction for addition and subtraction algorithms to facilitate uniform assignment of NumberA and NumberB and calculation result getResult ()
{
Public double NumberA {get; set ;}
Public double NumberB {get; set ;}
Public abstract double getResult ();
}
Class OperationAdd: Operation // Addition Algorithm inherits the abstract algorithm
{
Public override double getResult ()
{
Return NumberA + NumberB;
}
}
Class OperationSub: Operation // The subtraction algorithm inherits the abstract algorithm.
{
Public override double getResult ()
{
Return NumberA-NumberB;


}
}
}

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.