Design Model --- simple factory and Singleton, design model --- Factory

Source: Internet
Author: User

Design Model --- simple factory and Singleton, design model --- Factory

1. Concept of design pattern:

Design pattern: Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people, classified and catalogued.

2. Advantages of the design model:

To make code reusable, make it easier for others to understand, and ensure code reliability.

--------- Simple factory design model

The so-called simple factory design model certainly has a factory class, which has a method.

1. Notes for this method:

01. It must be static.

02. The returned value is of the parent class type.

03. PASS Parameters (different parameters are given through different questions)

Eg:

Public static Pizza getdo (string type ){}

2. Use switch to select a structure to overwrite the Child class of the parent class

Creates a parent class object and assigns a value of null.

Pizza pizza = null;

Switch (input parameter)

{

Case 1:

Pizza = new ChessePizza ();

Break;

Case 2:

Pizza = new pgPizza ();

Break;

}

Return pizza;

--------------------- 01 simple factory (production of Pisa case )---------------

First, create a parent class pizza class: this class is abstract, that is, this class is abstract.

This class has an abstract method used to create a pizza.

The parent class has a number of sub-classes at the bottom: CheesePizza (cheese pizza) PgPizza (bacon pizza class)

Subclass must override the abstract method of the parent class.

// Pisa class public abstract class Pizza {public abstract string Show ();} // bacon Pisa public class PgPizza: Pizza {public override string Show () {return "the production of bacon Pisa is complete! ";}}// Cheese Pisa public class CheesePizza: Pizza {public override string Show () {return" cheese Pisa finished! ";}}// Factory class public class PizzaFactory {public static Pizza dopizza (string type) {Pizza ps = null; switch (type) {case" bacon Pisa ": ps = new PgPizza (); break; case "cheese Pisa": ps = new CheesePizza (); break;} return ps ;}}

Call in form

Private void btndo_Click (object sender, EventArgs e) {if (rbBacon. Checked = true ){
// Call the production method in the factory. The returned values are received by the parent class and imported to the created Pizza type Pizza ps = PizzaFactory. dopizza (rbBacon. text); string work = ps. show (); MessageBox. show (work);} else {Pizza ps = PizzaFactory. dopizza (rbCheese. text); string work = ps. show (); MessageBox. show (work );}}

 

------------------- 02 simple factory (production calculator )---------------

 

// Calculation class: parent class public abstract class Calculate {public double num1 {get; set;} // The first operand public double num2 {get; set ;} // second operand // calculation result of the abstract method public abstract double Cal ();} // addition calculation public class Add: Calculate {public override double Cal () {return this. num1 + this. num2 ;}// subtraction calculation public class Reduce: Calculate {public override double Cal () {return this. num1-this. num2 ;}// multiplication calculation public class Ride: Calculate {public override double Cal () {return this. num1 * this. num2 ;}// Division calculation public class Div: Calculate {public override double Cal () {double result = 0.0; if (this. num2 = 0) {throw new Exception ("the divisor cannot be 0");} else {result = this. num1/this. num2;} return result ;}}// factory public class OpearFactory {public static Calculate Calresult (string opear) {Calculate cal = null; switch (opear) {case "+ ": cal = new Add (); break; case "-": cal = new Reduce (); break; case "*": cal = new Ride (); break; case "/": cal = new Div (); break;} return cal ;}}

Call in the From Form

When you click the computing button in the window, call the static method of the factory class and use the operator selected in the drop-down box as the method parameter to obtain a type of Return Value of the parent class, then you can customize a parent class type to accept it!

Pass the two operation parameters of the window TextBox to the two parameters of the custom parent class, and then the passed operator enters the factory class to determine which subclass of the New class, then the parent class calls the subclass calculation method to get a result!

Private void btnok_Click (object sender, EventArgs e) {// obtain the first operand double num1 = Convert. toInt32 (txtnum1.Text); // obtain the second operand, double num2 = Convert. toInt32 (txtnum2.Text); // returns the string opear = cboOpear. text; // The type of the input opear return value is the parent class type Calculate cal = OpearFactory. calresult (opear); cal. num1 = num1; cal. num2 = num2; double result = 0.0; try {// call the Method result = cal for the subclass. cal ();} catch (Exception ex) {MessageBox. show (ex. message);} lblresults. text = result. toString ();}

----------------------- Singleton design model ---------------
Concept of Singleton mode:

The Singleton mode ensures that the specified class has only one instance at any time during the entire application lifecycle,

It also provides a global access point for the customer program to obtain the instance. Simply put, this class can only be instantiated once,

In the future, it will be taken out of its instance.

At the end of the S1 stage, a KTV karaoke system is created. Each time you click a window to play the video, a new window is created, this involves the singleton design model:

01: create a form class FrmKTVplay.

02: Change the public initialization component method of the system to private

Private FrmKTVplay ()
{
InitializeComponent ();
}

03: Define a static custom variable of the same type as the window

Effect: private static FrmKTVplay;

04: another method for defining a public static Window Type

Effect: public static FrmPlay Show ()
{
If (play = null)
{
Play = new FrmKTVplay ();
}
Return play;
}

 

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.