NET design pattern instances Simple Factory mode (Factory pattern)

Source: Internet
Author: User

Introduction of Simple Factory model (Bref Introduction)

The advantage of simple Factory pattern is that the factory class contains the necessary logical judgments, dynamically instantiating related classes based on the client's selection criteria, and for the client, to remove the reliance on the specific product

Ii. Problem Solving (what to Solve)

When a client instantiates an object, it does not need to be concerned about which subclass of the object is instantiated.

Three, simple factory model Analysis 1, simple factory model structure

I Product Interface: Abstract Product class

concreteproduct class : Specific implementation of product classes

Simple Factory Simple Factory class

Four Example Analysis (Example) 1, scene

A simple calculator with simple add and subtract operations. Structure as shown

simplefactory: Simple Factory class.

Operation: Abstract Operation class

addoperation: addition operation

suboperation: subtraction operation

2. Code

1, abstract Operation class operation, and its specific implementation class Addoperation, suboperation

<summary>

Abstract Operation class

</summary>

Public abstract class operation

{

public int Numbera;

public int numberb;

public abstract int GetResult ();

}

<summary>

Addition operation

</summary>

public class Addoperation:operation

{

public override int GetResult ()

{

Return (This.numbera + this.numberb);

}

}

<summary>

Subtraction operation

</summary>

public class Suboperation:operation

{

public override int GetResult ()

{

return (THIS.NUMBERA-THIS.NUMBERB);

}

}

2, Simple factory class Simplefactory

<summary>

Simple Factory class

</summary>

public class Simplefactory

{

public static Operation CreateOperation (String operation)

{

Operation o = null;

Switch (operation)

{

Case "+":

o = new Addoperation ();

Break

Case "-":

o = new Suboperation ();

Break

}

return o;

}

}

3 , client-side code

static void Main (string[] args)

{

Operation Operation1 = simplefactory.createoperation ("+");

Operation1.numbera = 10;

Operation1.numberb = 20;

Console.WriteLine ("{0}+{1}={2}", Operation1.numbera, Operation1.numberb, Operation1. GetResult ());

Operation Operation2 = Simplefactory.createoperation ("-");

Operation2.numbera = 10;

Operation2.numberb = 20;

Console.WriteLine ("{0}-{1}={2}", Operation2.numbera, Operation2.numberb, Operation2. GetResult ());

Console.read ();

}

3. Example running result

V. Summary (Summary)

Simple Factory mode is a relatively simple design mode, this paper describes the concept of this mode and its design structure diagram, and finally a calculator example is described.

Reprinted from: http://www.cnblogs.com/ywqu/archive/2010/01/06/1640026.html

NET design pattern instances Simple Factory mode (Factory pattern)

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.