Strategy Mode and delegate Delegation

Source: Internet
Author: User

StrategyThe mode isAlgorithm. Even for a computing behavior, if its diversity exists, we also need to abstract it and define it in the form of interfaces to achieve the purpose of easy scalability. Because the object-oriented polymorphism is fully utilized, the specific implementation of this line is determined at runtime. Taking tax calculation as an example, it is assumed that the tax policies are divided into personal income tax and corporate income tax. Abstract The tax policy as an interface based on the policy ModeItaxstrategy:

Public Interface Itaxstrategy
{
Double Calculate(Double Income);
}

Various tax policies implement this class:

Public Class Peronaltaxstrategy : Itaxstrategy
{
Public Double Calculate ( Double Income )
{
// Implementation;
}
}
Public Class Enterprisetaxstrategy : Itaxstrategy
{
Public Double Calculate ( Double Income )
{
// Implementation;
}
}

If there is a public class at this time, tax-related operations are provided, including the method for calculating income tax:

Public Class Taxop
{
Private Itaxstrategy Strategy ;
Public Taxop ( Itaxstrategy Strategy )
{
This . Strategy = Strategy ;
}
Public Double Gettax ( Double Income )
{
Return Strategy . Calculate ( Income );
}
}

Client call:

Public Class App
{
Public Static Void Main ( String [] ARGs )
{
Taxop OP = New Taxop ( New Personaltaxstrategy ());
Console . Writeline ( "The personal tax is: {0 }" , OP . Gettax ( 1000 ));
}
}

This is a typical object-oriented design concept. However, we can also use some simple algorithm behaviors.DelegateAlthough it is more similar to process-oriented design, its scalability is also flexible. If the logic of the algorithm is not complex and the implementation of the algorithm is in a certain state to be determinedStrategyMode is more convenient.

 

We also use the above example to modify the original abstract interface to delegate:

Public Delegate Double Calculatetax(Double Income);

 

The individual income tax and enterprise income tax are changed:

Public Class Tax
{
Public Static Double Calculatepersonaltax ( Double Income )
{
// Implementation;
}
Public Static Double Calculateenterprisetax ( Double Income )
{
// Implementation;
}
}

The public category of taxation is modified as follows:

Public Class Taxop
{
Private Calculatetax Caldel ;
Public Taxop ( Calculatetax Caldel )
{
This . Caldel = Caldel ;
}
Public Double Gettax ( Double Income )
{
Return Caldel ( Income );
}
}

Client call:

Public Class App
{
Public Static Void Main ( String [] ARGs )
{
Taxop OP = New Taxop (New Calculatetax ( Tax . Calculatepersonaltax ));
Console . Writeline ( "The personal tax is: {0 }" , OP . Gettax (1000 ));
}
}

From the two implementation schemes,CodeThey are similar, but their design ideas are quite different. It is the difference between object-oriented and process-oriented. The former encapsulates behavior as an object, while the latter directly operates on the method and utilizesDelegateDelegate to implement expansion. In my opinion, I still prefer the first solution, but the latter also provides at least one idea. In particular, we can also think of delegation as a special abstraction, because it is essentially a function pointer, which represents a cluster of functions, in this way, behaviors with the same features are abstracted in universal sense. Maybe, this will promote the understanding of delegation.

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.