The difference between a policy model and a simple factory model _ Factory mode

Source: Internet
Author: User

Directly on the code:

Simple Factory mode:

------------------------------1 Abstract Classes--------------------------

Abstract class Absclass
{
        //abstract method: The algorithm that provides some columns operates public
        abstract void Acceptcash (string org);

------------------------------2 Implementation Classes--------------------------

Inherit from abstract class class
A:absclass
{
//Concrete method: Provide some column algorithm operations public
override double Acceptcash (string org)
{
Console.writerline ("Class A Method");
}


Inherit from abstract class class
B:absclass
{
//Concrete method: Provide some column algorithm operations public
override double Acceptcash (string org)
{
Console.writerline ("Class B Method");
}
}


------------------------------Simple Factory class--------------------------

Cash charge factory
class cashfactory
{
//return corresponding object according to condition public
static Absclass createcashaccept (StringType)
{
Absclass cs = null;
Switch (type)
{case
"A":
cs = new Al ();
break;
Case "B":
cs = new B ();
break;
Case "...":
........ break;
return CS;
}


Client invocation:

/Use simple Factory mode according to the Drop-down selection box, generate the corresponding object
Absclass csuper = cashfactory.createcashaccept ("A");//You can get a specific implementation class by passing in the type, like: You go to the airport, you tell your secretary directly, we drive to the airport, this time your secretary drove me, You can take the bus to the airport.

Absclass csuper= cashfactory.createcashaccept ("B"); Or a secretary to call a cab, let's take a cab.



Policy mode

The previous class did not change, just turned the cashfactory into a Casecontext policy class

Policy context
class Cashcontext
{
//Declare a cash charge parent object
private Absclass cs;
Set policy behavior, parameters for specific cash charges subclasses (normal, discounted or rebate) public
Cashcontext (absclass csuper)
{
This.cs = csuper;
}
Get cash Promotion results (using polymorphic mechanisms, different policy behaviors result in different results) public
double GetResult (double)
{return
Cs.acceptcash ( money);
}


Client invocation:

Absclass cc = NULL;
Switch (cbxType.SelectedItem.ToString ())
{case
"a":
cc = new Cashcontext (new A);/The same goes to the airport, at this time: drive to C19/>break;
Case "B":
cc = new Cashcontext (new B);/or take a taxi to break
;
Case "...": .....
break;


Summary:

The policy pattern and the simple factory pattern look very similar, and are implemented by polymorphism to select different subclasses.

From the perspective of using these two patterns, we will find that in the simple factory model we only need to pass the appropriate conditions to get an object (tell others what I want to do, others to do), and then through this object to implement the operation of the algorithm. The policy pattern, when used, must first create a class object that you want to use (do it yourself), and then pass the object in the most parameter, calling a different algorithm through the object. In the simple factory model, we can select a class to instantiate the object through the condition, and the policy mode will select the work of the corresponding object to the user of the mode, and it does not do the selection work itself.

Combined with the above code is not difficult to see, in fact, the difference between the two is very subtle, factory is a direct creation of specific objects and the object to perform the corresponding action, and the context of this operation to the context class, did not create specific objects, the implementation of the further encapsulation of code, Client code does not need to know the specific implementation process.

Of course, the most used is the combination of the two.


Summary: Simple factory model: Just make a command, to be implemented by others, the policy model: not only to give orders, but also to do it himself.

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.