Modern C ++ design policies and policy classes

Source: Internet
Author: User

Policy

The so-called policy is actually an interface of a class or class template. It consists of inner type definition, member functions, and member variables.

It is similarStrategy.

Here we will first introduce strategy:

Structure
Intention Define a series of algorithms, encapsulate them one by one, and make them replaceable. This mode allows algorithms to change independently of customers who use it.
Applicability
  • Many related classes only have different behaviors. "Policy" provides a method to configure a class with one of multiple actions.
  • Different variants of an algorithm are required. For example, you may define algorithms that reflect different space/time trade-offs. When these variants are implemented as class layers of an algorithm [h o 8 7], the policy mode can be used.
  • Algorithms use data that customers should not know. Policy modes can be used to avoid exposing complex algorithm-related data structures.
  • A class defines multiple behaviors, and these behaviors appear in the form of multiple conditional statements in the operations of this class. Move related condition branches into their respective s t r a t e g y classes to replace these condition statements.

 

In the following example, define a policy to generate an object: creatorpolicy is a class template with a type of T. It provides a create interface function, which does not accept parameters, returns a pointer to T. Every time create is called, a pointer is returned.

The following are three specific entities.

Template <class T>
Struct opnewcreator
{
Static T * Create ()
{
Return New T;
}
};

Template <class T>
Struct malloccreator
{
Static T * Create ()
{
Void * Buf = STD: malloc (sizeof (t ));
If (! Buf) return 0;
Return new (BUF) T;
}
};

Template <class T>
Struct prototypecreator
{
Prototypecreator (T * pobj = 0): pprototype _ (pobj ){}
T * Create ()
{
Return pprototype _? Pprototype _-> clone (): 0;
}
T * getprototype () {return pprototype _;}
Void setprototype (T * pobj) {pprototype _ = pobj ;}
PRIVATE:
T * pprototype _;
};

These three specific entities are calledPolicy classesIt may be a bit inappropriate because they also have templates.

 

The difference between the policies interface and the common class interface is that it is loose and has no strict restrictions as the class interface. For example, the create function does not have to be virtual or the pointer t is not returned, you only need to define create.

The first three instances all define create () and carry the necessary return types, so they all comply with the Creator policy.

 

Now we design a class that uses creatorpolicy. It uses the first instance in an inherited or composite way.

Template <class creationpolicy>
Class widgetmanager: Public creationpolicy
{...};

 

If this class uses one or more policies, we call it host or host classes. widgetmanager uses the host class of the policies. host is responsible for combining the structures and actions provided by elasticsearch into a more complex structure and behavior.

 

When the client externalizes the widgetmanager template, it must pass in the expected policy
Typedef widgetmanager <opnewcreator <widget> mywidgetmgr;

When the mywidgetmgr object needs to generate a widget entity, it calls the CREATE () provided by its policy child entity opnewcreator <widget>. Selecting the create policy is the freedom of the widgetmanage user. With this design, the widgetmanager user can automatically assemble the functions he/she wants.

 

Modern C ++ design policies and policy classes

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.