Go from △ to OO and enter the policy Mode

Source: Internet
Author: User

In reality, many devices need to calculate other values based on some values, such as the area of the known radius of the garden, the perimeter of the known triangle on the three sides, and so on. There are also some technical equipment that will use this method, in the final analysis, a formula is used to calculate the corresponding attribute values. Now we take triangles as an example to learn how to solve these problems.

Basic Requirement: Calculate the triangle perimeter based on the three sides A, B, and C.

Basic Abstraction: a triangle-like triangle, which has a method for calculating the perimeter getcirbyabc ();

Basic implementation:

Public class Triangle
{
Public double GetCirByABC (double a, double B, double c)
{
Return a + B + c;
}

}

Basic disadvantages: According to the OO abstraction, the details should not be depended on. Obviously, A, B, C, and return values all locate the corresponding attributes, violating the principle that abstraction does not depend on details, therefore, a triangle object should be returned directly. One of its advantages is that the attribute is used to obtain the corresponding value for any value. Benefit 2 will be mentioned later.

After improvement:

Public class triangle
{
Public double A {Get; set ;}
Public Double B {Get; set ;}
Public double c {Get; set ;}
Public double CIR {Get; set ;}

Public triangle GetModel (Triangle TG)
{
TG. Cir = Tg. A + TG. B + TG. C;
Return TG;
}

}

 

Expansion requirement: Calculate the length of the C side based on the and B sides and the CIR perimeter. It seems that you can simply add a method to solve this problem.

Public triangle GetModel (Triangle TG)
{
TG. c = Tg. cir-TG. A-TG. B;
Return TG;
}

 

It can be observed that the benefits of directly returning objects 2. What remains unchanged is abstraction, but the algorithm has changed, and thus the policy mode emerged.

Strategy: it defines a series of algorithms, encapsulates each algorithm, and can be replaced with each other. The birth of new algorithms will not damage the class structure, it is just an extension.

Policy abstract class definition:

Abstract class Strategy
{
Public abstract Triangle GetModel (Triangle tg );
}

 

The two specific policy Classes define cirbyabcstrategy and cbycirabstrategy:

// Calculate C Based on the circumference and AB side
Class CByCirABStrategy: Strategy
{
Public override Triangle GetModel (Triangle tg)
{
Tg. C = tg. Cir-tg. A-tg. B;
Return tg;
}
}

 

 

// Calculate the perimeter Based on ABC
Class CirByABCStrategy: Strategy
{
Public override Triangle GetModel (Triangle tg)
{
TG. Cir = Tg. A + TG. B + TG. C;
Return TG;
}
}

 

 

Context class (save a reference to a policy class) Definition:

 

Class Context
{
Private strategy Strategy;
Public context (strategy Strategy)
{
This. Strategy = strategy;
}
Public triangle contextinface (Triangle TG)
{
Return strategy. GetModel (TG );
}
}

 

 

Client perimeter call example:

 

Triangle triangle = new Triangle ();
Triangle. A = 3;
Triangle. B = 4;
Triangle. C = 5;
Context context = new Context (new CirByABCStrategy (); // The specific policy class is called here. If it is obtained based on AB and Cir, it is new CByCirABStrategy ();
Triangle tg = context. ContextInface (triangle );
Console. WriteLine ("rule mode perimeter:" + tg. Cir );

 

 

Disadvantages of the Policy mode: The implementation is complicated. If there are n kinds of devices, there are multiple algorithms for each device. There may be three or four algorithms for some devices, the number of classes will be large. No perfect design, only suitable design.

PS: attached Structure

 

 

 

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.