Youyuan interface-limit the number of your youyuan classes

Source: Internet
Author: User

Youyuan interface-Limit the number of your friends

 

Write c ++ProgramYou may have encountered the following situation:
There is a set of policies in a class, and the implementation of these policies needs to be accessedAAnd these Members do not want to be accessed by other classes.
Generally, these members are expected to be protected or private, and these policies are treated as this class.A. For example:

 

Code
Class Strategy1;
Class Strategy2;
Class Strategy3;

Class A
{
Public :
Friend Class Strategy1;
Friend Class Strategy2;
Friend Class Strategy3;
Private :
Void _ Foo ();
Int _ Bar;
};

Now, if you want to add a new policyStrategy4To maintain this subtle relationship, you needStrategy4Add as ClassA.
As policies increase, this process continues to expand.AIn the end, you may not like what you seeCode. In addition, each time you add a policy, you must make the dependencyACode re-compilation, there must be something wrong here.

I think of a solution that makes your code look messy.
First, since this set of policies appears in a similar situationAThey may have similarities. For example, they may need to accessA.
So if you access these members through a proxy class, this set of policies does not have to beAAs long as the proxy isAYou can.
This proxy is called the youyuan interface.

Code
Class Ifrienda_strategy;

Class A
{
Public :
Friend Class Ifrienda_strategy;
Private :
Void _ Foo ();
Int _ Foo2 ( Int P) Const ;
Int _ Bar;
};

ClassIfrienda_strategy
{
Protected:
Typedef ifrienda_strategy friend;//Unified Access methods for the youyuan Interface

// Presumably, the implementation of this set of functions does not have to be given.
Static   Void _ Foo ( & RA );
Static   Int _ Foo2 ( Const A & Ra, Int P );
Static   Int Get_bar ( Const A & RA );
Static   Void Set_bar ( Const A & Ra, Int Value );
};

Class Strategy1: Protected Ifrienda_strategy
{
Public :
Void X ( & Ra, Int P)
{
// So you can access
Friend: _ Foo (RA );
Int F = Friend: _ foo2 (RA, P );
Friend: set_bar (RA, friend: get_bar (RA) + F );
}
};

 

In this way, the relationship between friends and friends is completed and the dependency is reduced.
The disadvantage is that the Code is not defensive, and unauthorized classes can easily access it.AThe ability to protect members. You only need to inherit from the youyuan interface.
Here we can further see that if the classAThere are multiple sets of different policies, which need to be accessedADifferent subsets of members, then they can be independent to their friends interface, irrelevant.

Kaikai

PS. I found it was reprinted, without links, and it was also classified into the Java section. Here we will make some changes in the content: Mark the Language C ++

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.