[Software architecture] modular programming ideas and (C + +) practice

Source: Internet
Author: User

Repost

Content Introduction:

    • The idea of modularization
    • The composition of the module
    • Management of modules
    • Modular Practice
    • Defining the module structure
    • Declaring a Module object
    • Defining a Module List
    • Module List Object
The idea of modularization

1. The system is divided into many modules, the module internal focus on their own needs to achieve the core business.

2. The typical characteristics of modularity are: "Low coupling between modules" and "high aggregation within the module".

The composition of the module

1. Name. Global scope indicates the unique identity of the module.

2. Reliance. Other modules that are called modules that run references are dependent.

3. Interface. The service provided externally by the module is called an interface.

Management of modules

1. Scan Module List

2. Analysis Module dependencies

3. Rearrange the module sequence

4. Pre-initialization preparation

5. Initialization of the module

6. Module Anti-initialization

7. Final destruction of the module

Modular Practice
#include <vector><string><functional>usingnamespace  std;  #ifndef _unicodetypedef std::string  stdstring; #else typedef std::wstring stdstring; #endif
Defining the module structure
/** * Define initialization and inverse initialization function types*/typedef function<void() >selffunction; /** * Define module type*/structselfmodule{/** * Module ID*/stdstring _id; /** * Module dependencies, different dependencies between '; ' Split*/stdstring _depends; /** * Operation prior to initialization*/selffunction _preinit; /** * Initialization function*/selffunction _init; /** * Inverse initialization function*/selffunction _uninit;};
Declaring a Module object
/** * Declare the definition of the module*/classdefmodule{ Public:         /** Constructor * * @Param ID * Module ID * @Param depends * modulo Block Dependent * @Param PreInit * Operation prior to initialization * @Param init * Module initialization function * @Pa Ram UnInit * Module's inverse initialization function * @Param destructor * The destructor of the module*/Defmodule (stdstring ID, stdstring depends, Selffunction preinit           , Selffunction init, selffunction uninit, Selffunction destructor); /** * destructor*/~Defmodule (); protected:         /** * destructor*/selffunction _destructor;}; /** * Defmodule*/defmodule::D efmodule (stdstring ID, stdstring depends, Selffunction PreInit, selffunction in It, Selffunction UnInit, Selffunction destructor): _destructor (destructor) {Selfmodulel Ist* List =selfmodulelist::getinstance (); size_t Offset=0; size_t LastOffset=offset;  while(offset = Depends.find (';', lastoffset))! =-1) {stdstring dep= Depends.substr (LastOffset, offset-LastOffset); Selfmodule& mod = list->Findmodule (DEP); if(&mod = = &selfmodulelist::_nullmodule)                            {Selfmodule module; module._id=DEP; List-addmodule (module); } LastOffset= offset +1; } selfmodule& mod = list->findmodule (ID); if(&mod! = &selfmodulelist::_nullmodule) {Mod._init=Init; Mod._uninit=UnInit; Mod._preinit=PreInit; Mod._depends=depends; }         Else{selfmodule module; module._id=ID; Module._init=Init; Module._uninit=UnInit; Module._preinit=PreInit; Module._depends=depends; List-addmodule (module); }} Defmodule::~Defmodule () {_destructor ();}
Defining a Module List
/** * Module list*/classselfmodulelist{ Public:         /** * Get unique instances * * @Return return globally unique instances*/         Staticselfmodulelist*getinstance () {if(_instance! =NULL) {                            return_instance; }                     if(_instance = =NULL) {_instance=Newselfmodulelist (); }                     return_instance; }           /** * Delete Unique instances*/         Static voiddelinstance () {if(_instance! =NULL) {                            Delete_instance; _instance=NULL; }         }   Public:         /** * Operation prior to initialization*/         voidpreinits () { for(Auto iter = _list.begin (); ITER! = _list.end (); iter++)                   {                            (*iter). _preinit (); }         }           /** * Initialization function*/         voidinits () { for(Auto iter = _list.begin (); ITER! = _list.end (); iter++)                   {                            (*iter). _init (); }         }           /** * Inverse initialization function*/         voiduninits () { for(Auto iter = _list.rbegin (); ITER! = _list.rend (); iter++)                   {                            (*iter). _uninit (); }         }           /** * Add module * * @Param mod * Module*/         voidaddmodule (selfmodule MoD) {_list.push_back (mod); }           /** * Find module * * @Param ID * ID of the module to be found * @Return return the found module */Selfmodule&Findmodule (stdstring ID); /** * null reference for module*/         StaticSelfmodule _nullmodule; protected:         /** * Module list*/Vector<SelfModule>_list; protected: Selfmodulelist (void) {}         ~selfmodulelist (void) {}  Private:         /** * Globally unique instances*/         Staticselfmodulelist*_instance;}; /** * null reference for module*/Selfmodule Selfmodulelist::_nullmodule; /** * Globally unique instances*/selfmodulelist* Selfmodulelist::_instance =NULL; Selfmodule&Selfmodulelist::findmodule (stdstring id) { for(Auto iter = _list.begin (); ITER! = _list.end (); iter++)         {                   if((*iter). _id.compare (id) = =0)                   {                            return(*ITER); }         }         return_nullmodule;}
Module List Object
/**/static defmodule _module (_t ("selfmodulelist")         , _t (""), [] () {}, [] () {}, [] () {         }         , [] () {                   Selfmodulelist::D elinstance ();         });

[Software architecture] modular programming ideas and (C + +) practice

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.