Design Mode: simple factory mode, design mode Factory

Source: Internet
Author: User

Design Mode: simple factory mode, design mode Factory

1. Compile a class chart

2. Open vs2010

3. Choose file (F)> New (N)> Project (P) from the menu. The following dialog box is displayed:

4. In the displayed dialog box, select "Visual C #"> "Windows" under the template option, and then select "console application ".

 

5. In the Name text (N) box, enter the project name "SimpleFactorySample", name the solution name "SimpleFactorySample", and set the Save path.

6. Take the default value for other items and click "OK" to generate the following solution.

7. Create a Chart class: an abstract Chart interface that acts as an abstract product class.

8. Create a HistogramChart class to indicate the bar chart class and act as a specific product class.

Using System;

 

Namespace SimpleFactorySample

{

Class HistogramChart: Chart

{

Public HistogramChart ()

{

Console. WriteLine ("Create a bar chart "!);

}

Public void Display ()

{

Console. WriteLine ("display bar chart! ");

}

}

}

9. Create a PieChart class to represent the pie chart class and act as a specific product class.

Using System;

Namespace SimpleFactorySample

{

Class PieChart: Chart

{

Public PieChart ()

{

Console. WriteLine ("create pie chart! ");

}

Public void Display ()

{

Console. WriteLine ("show pie chart! ");

}

}

}

10. Create a LineChart class to represent the line chart class and act as a specific product class.

Using System;

Namespace SimpleFactorySample

{

Class LineChart: Chart

{

Public LineChart ()

{

Console. WriteLine ("Create a line chart! ");

}

Public void Display ()

{

Console. WriteLine ("display line chart! ");

}

}

11. Create a chart factory class ChartFactory to act as a factory class

Using System;

Namespace SimpleFactorySample

{

Class ChartFactory

{

// Static factory Method

Public static Chart GetChart (string type)

{

Chart chart = null;

If (type. Equals ("histogram "))

{

Chart = new HistogramChart ();

Console. WriteLine ("initialize setting the bar chart! ");

}

Else if (type. Equals ("pie "))

{

Chart = new PieChart ();

Console. WriteLine ("initialize the pie chart! ");

}

Else if (type. Equals ("line "))

{

Chart = new LineChart ();

Console. WriteLine ("initialize and set the line chart! ");

}

Return chart;

}

}

}

12. Update the client test code.

Using System;

Using System. Configuration;

Namespace SimpleFactorySample

{

Class Program

{

Static void Main (string [] args)

{

/*

Product product;

Product = Factory. GetProduct ("A"); // create A product object through the Factory class

Product. MethodSame ();

Product. MethodDiff ();

*/

Chart chart;

// Read the configuration file

String chartStr = ConfigurationManager. etettings ["chartType"];

Chart = ChartFactory. GetChart (chartStr); // create a product using the static factory Method

Chart. Display ();

Console. Read ();

}

}

}

Create and implement the following classes:

Using System;

Namespace SimpleFactorySample

{

Abstract class Product

{

// Public business methods for all products

Public void MethodSame ()

{

// Implementation of common methods

}

// Declare the abstract Business Method

Public abstract void MethodDiff ();

}

 

Class ConcreteProductA: Product

{

// Business Implementation Method

Public override void MethodDiff ()

{

// Business method implementation

}

}

Class ConcreteProductB: Product

{

// Business Implementation Method

Public override void MethodDiff ()

{

// Business method implementation

}

}

Class Factory

{

// Static factory Method

Public static Product GetProduct (string arg)

{

Product product = null;

If (arg. Equals (""))

{

Product = new ConcreteProductA ();

// Initialize the product settings

}

Else if (arg. Equals ("B "))

{

Product = new ConcreteProductB ();

// Initialize the product settings

}

Return product;

}

}

}

Namespace SimpleFactorySample

{

/*

Abstract class Product

{

Public abstract void Process ();

}

Class ConcreteProductA: Product

{

Public override void Process ()

{

//......

}

}

Class ConcreteProductB: Product

{

Public override void Process ()

{

//......

}

}

Class Factory

{

Public static Product CreateProduct (char type)

{

Switch (type)

{

Case 'A ':

Return new ConcreteProductA (); break;

Case 'B ':

Return new ConcreteProductB (); break;

//......

}

}

}

*/

}

13. Compile and run the program and output the following results:

 

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.