C # Big talk design pattern code

Source: Internet
Author: User

Cashier procedure 1: Implementation of the simple factory Model

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
/// <Summary>
/// Simple factory mode implementation
/// </Summary>
Public partial class module_study_designpatter: system. Web. UI. Page
{
/// <Summary>
/// Cash receipt
/// </Summary>
Abstract class cashsuper
{
Public abstract double acceptcash (double money );
}
/// <Summary>
/// Normal charges
/// </Summary>
Public class normalcash: cashsuper
{
Public override double acceptcash (double money)
{
Throw money;
}
}
/// <Summary>
/// Discount
/// </Summary>
Public class rebatecash: cashsuper
{
Double _ moneyrebate = 1D;
Public double moneyrebate
{
Set {_ moneyrebate = value ;}
Get {return _ moneyrebate ;}

}
Public rebatecash (double moneyrebate)
{
Moneyrebate = moneyrebate;
}
Public override double acceptcash (double money)
{
Return moneyrebate * money;
}
}
/// <Summary>
/// Pay-as-you-go
/// </Summary>
Public class returncash: cashsuper
{
Double _ moneycondition = 0.0d;
Double _ moneyreturn = 0.0d;
Public returncash (double moneycondition, double moneyreturn)
{
_ Moneycondition = moneycondition;
_ Moneyreturn = moneyreturn;


}
Public override double acceptcash (double money)
{
Double result;
If (money> _ moneycondition)
{
Result = money-math. Floor (money/_ moneycondition) * _ moneyreturn;
}
}
}
/// <Summary>
/// Paid Factory
/// </Summary>
Class cashfactory
{
Public static cashsuper createcash (string cashtype)
{
Cashsuper cs = NULL;
Switch (cashtype)
{
Case "normal charges ":
Cs = new normalcash ();
Break;
Case "free 00 at 300 ":
Cs = new returncash (300366d, 100.d );
Break;
Case "":
Cs = new rebatecash (0.8d );
Break;
}
Return Cs;

}

}
Protected void page_load (Object sender, eventargs E)
{

}
}

Cashier Procedure 2: Rule Mode

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
/// <Summary>
/// Implementation Method of policy Mode
/// </Summary>
Public partial class module_study_strategypatter: system. Web. UI. Page
{
/// <Summary>
/// Billing policy
/// </Summary>
Class cashcontext
{
/// <Summary>
/// Define the cash receipt class
/// </Summary>
Cashsuper Cs;
/// <Summary>
/// Set the policy action. The parameter is the specific cash charge type.
/// </Summary>
/// <Param name = "csuper"> </param>
Public void setbehavior (cashsuper csuper)
{
This. cs = csuper;
}
/// <Summary>
/// Obtain the cash promotion Calculation Result
/// </Summary>
/// <Param name = "money"> </param>
/// <Returns> </returns>
Public double getresult (double money)
{
Return CS. acceptcash (money );
}

}
/// <Summary>
/// Cash receipt
/// </Summary>
Abstract class cashsuper
{
Public abstract double acceptcash (double money );
}
/// <Summary>
/// Normal charges
/// </Summary>
Public class normalcash: cashsuper
{
Public override double acceptcash (double money)
{
Throw money;
}
}
/// <Summary>
/// Discount
/// </Summary>
Public class rebatecash: cashsuper
{
Double _ moneyrebate = 1D;
Public double moneyrebate
{
Set {_ moneyrebate = value ;}
Get {return _ moneyrebate ;}

}
Public rebatecash (double moneyrebate)
{
Moneyrebate = moneyrebate;
}
Public override double acceptcash (double money)
{
Return moneyrebate * money;
}
}
/// <Summary>
/// Pay-as-you-go
/// </Summary>
Public class returncash: cashsuper
{
Double _ moneycondition = 0.0d;
Double _ moneyreturn = 0.0d;
Public returncash (double moneycondition, double moneyreturn)
{
_ Moneycondition = moneycondition;
_ Moneyreturn = moneyreturn;


}
Public override double acceptcash (double money)
{
Double result;
If (money> _ moneycondition)
{
Result = money-math. Floor (money/_ moneycondition) * _ moneyreturn;
}
}
}
/// <Summary>
/// Paid Factory
/// </Summary>
Class cashfactory
{
Public static cashsuper createcash (string cashtype)
{
Cashsuper cs = NULL;
Switch (cashtype)
{
Case "normal charges ":
Cs = new normalcash ();
Break;
Case "free 00 at 300 ":
Cs = new returncash (300366d, 100.d );
Break;
Case "":
Cs = new rebatecash (0.8d );
Break;
}
Return Cs;

}

}
Protected void page_load (Object sender, eventargs E)
{

}
}

Comment: In this way, the change point is handed back to the client, so that the client must know the policy, so further improvement is required ..

 

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.