Pan-type programming and design new thinking

zhaozj2021-02-08  209

Pan-type programming and design new thinking

New thinking

Combining design modes, generic programming, and object-oriented programming (Object-Oriented Programming) form new thinking. Among them, the design model is a refined outstanding design method, and it is a reasonable and reusable solution for many situations; generic programming is a model of paradigm, focusing on type abstraction , Form a fine set in terms of functional requirements, and utilize these requirements to implement algorithms, the same algorithm can be used in a wide range of types, so-called generics, it is possible to operate in a variety of data types; The polymorphism and template (Templates) such as object program are combined to obtain a high level of generic components. The generic components advance the design module in advance, allowing users to specify types and behaviors to form reasonable design, mainly feature flexible, universal, and easy to use.

Policies and Policy classes are an important class design technology that is used to define a class or class template interface that consists of the following or all of the following: internal type definitions, member functions, and member variables. The POLICY-based class consists of many small classes (called policies), each such small classes are only responsible for some aspects of behavior or structures. The Policies mechanism consists of templates and multiple inherits, which can be mixed with each other, thereby forming a diversity of design, but can customize behavior through the PLICY class, but also can be customized.

The following is a simple example of the use of generalized thinking and object-oriented thinking in some design modes.

Singletons design pattern generalization

Singleton mode is a guaranteed one object (Class) only one entity and provides a global access point. Singleton is an improved global variable that is only available in the program, which focuses on generating and managing a separate object, and does not allow another such object.

Let's take a look at the basic technique of general C implementation, the following is the source code:

// Singleton.h file

Class Singleton

{

PUBLIC:

Static Singleton & Instance ()

{

IF (! pinstance _) {

if (destroyed _) {// Is the reference to fail?

ONDEADRECERENCE ();

}

Else {

Create (); // Create an instance when the first time

}

}

Return * Pinstance_;

}

Private:

Singleton (); // Prohibits default configuration

Singleton (Const Singleton &); // Prohibits Copy Construction

Singleton & Operator = (const singleton); // Prohibition of assignment operation

Static void create () // Send an instance reference to create

{

STATIC Singleton theinstance;

Pinstance_ = & theinstance;

}

Static void ONDEADREFERENCE ()

{

Throw std :: runtime_ERROR ("instance is unfair to destroy");

}

Virtual ~ Singleton ()

{

PINSTANCE- = 0;

DESTROYED_ = true;}

Static Singleton * Pinstance_;

Static bool destroyed_;

}

// Singleton.cpp initialization in static member variables

Singleton * Singleton :: Pinstance_ = 0;

Bool Singleton :: destroyed_ = false;

As shown above, only one public member instance () in the Singleton mode implementation is used to create a single instance when used for the first time. When the second use, the static variable will have been set, and the instance will not be created again. The default constructor, copy constructor, and assignment operator are also placed in Private, and the location is not allowed to use them. In addition, in order to avoid instantiation after instance unexpected destruction, the static Boolean variable destroy_ is added to determine whether it is wrong to achieve stability.

It can be seen from the above general implementation that the Singleton mode implementation is mainly in creating aspects and LifeTime, which can create Singleton through various methods. Creation inevitably creates and destroys objects, inevitably open these two corresponding functions, will be created as an independent policy to separate, so you can create polymorphism objects, so the generalization Singleton does not have Creator objects, it Placed in the CreationPolicy Template. The life period refers to the C rules, and then created first destroyed, and the procedure life period destroys the Singleton object at a time.

Here is a simple generalization of Singleton mode (not considering thread factors)

Template

<

Class T,

Template Calss CreationPolicy = CREATEUSINGNEW,

Template class lifetimepolicy = Defaultlifetime,

>

Classs Singletonholder

{

PUBLIC:

Static T & Instance ()

{

IF (! pinstance_)

{

IF (Destroyed_)

{

LifetimePolicy :: ONDEADRECERENCE ();

DESTROYED_ = FALSE;

}

Pinstance_ = CREATIONPOLICY :: Create ();

LifetimePolicy :: SchedultCall (& DestorySingleton);

}

Return * Pinstance_;

}

Private:

Static void destroysinleton ()

{

Assert (! destroyed_);

CreationPlicy :: DESTROY (Pinstance_);

PINSTANCE_ = 0;

DESTROYED_ = True;

}

SingletonHolder ();

SingletonHolder (Const SingletonHolder &);

SingletonHolder & Operator = (const singletonholder);

Static T * pinstance_;

Static bool destroyed_;

}

Instance () is the only PUBLIC function open by SingletonHold, which creates a housing in CreationPolicy, LifetimePolicy. Among them, the template parameter type T, receive the class name, and both of the class of Singleton needs. The class template within the template parameter default parameter createusingNew refers to the object by the New operator and the default constructor, and the defaultlifetime is used to manage the life period through the C rules. There are two member functions in LifetimePolicy , and the ScheduleDestrution () function accepts a function pointer, pointing to the actual execution function of the destructure operation, as described above DestorySingleton destructor; ONDEADREFERENCE () function is the same as the same name in the general C above, is Responsible for discovering the failure instance to throw an abnormality. CreationPlicy The Create () and Destroy () two functions are used to create and destroy specific objects. The following is the use of the above generalized Singleton mode:

1, apply one

Class a {};

Typedef Singletonhold SINGLEA;

2, apply two

Class a {};

Class Derived: Public a {};

Template struct mycreator: public createusingnew

{

Static t * create ()

{

Return new deerid;

}

Static Void Destroy (T * Pinstance)

{

Delete Pinstance;

}

}

Typedef Singletonhold Singlea;

As can be seen from the example, SingletonHold is implemented based on a PLICY design, which decomposes the Singleton object into several policies, CreationPolicy and LifetimePolicy in the template parameter class, equivalent to two policies package. Using them can assist fabrications to customize the Singleton object, but also reserved the space for adjustment and extension. From this, generic components are a pluggable design template that combines templates and patterns that create new methods for creating expanded design in C , providing a simple transition from design to code, Help us write clear, flexible, highly reused code.

references

C Primer (Third Edition) --- Pan Aimin et al

Effective C (Second Edition) --- Houjie

More Effective C --- Houjie

Exceptional C --- Zhuo Xiao Tao translated

More EXCEPTIONAL C --- Translation of Chun

Deep exploration C object model --- Houjie

Pan-programming and STL --- Houjie

C STL programmer development guide --- Pengmump, etc.

Design mode: can be used for object-oriented software - Li Yingjun and other translations

C design new thinking - Hou Jie and other translations

Contact information:

Email: jingzhou_xu@163.com

Future Studio (Future Studio)

转载请注明原文地址:https://www.9cbs.com/read-2169.html

New Post(0)