Policy-based design

简介:        One problem which often arises during programming is how to build a base set of functionality which can be extended by...

       One problem which often arises during programming is how to build a base set of functionality which can be extended by the user, while still being modular enough to make it easy to replace only certain parts of an implementation without having to resort to copy & paste techniques. I guess everybody of us has faced this problem at least once, and came up with different solutions. There is a powerful and elegant technique called policy-based design for solving this kind of problem.

      Simple example

#include<iostream>
#include<string>
using namespace std;

template<typename output_policy,typename language_policy>
class HelloWorld:public output_policy,public language_policy
{
    using output_policy::Print;
    using language_policy::Message;

public:
    void Run()
	{
		Print(Message());
    }
};

class HelloWorld_OutputPolicy_WriteToCout
{
protected:
	template<typename message_type>
    void Print(message_type message)
    {
		std::cout << message << std::endl;
    }
};

class HelloWorld_LangguagePolicy_English
{
protected:
	std::string Message()
    {
        return "Hello World!";
    }
};

class HelloWorld_LanguagePolicy_German
{
protected:
	std::string Message()
    {
        return "Hello Policy!";
    }
};

int main()
{
    typedef HelloWorld<HelloWorld_OutputPolicy_WriteToCout,HelloWorld_LangguagePolicy_English> myPolicyClass;
    myPolicyClass hello1;
    hello1.Run();

    typedef HelloWorld<HelloWorld_OutputPolicy_WriteToCout,HelloWorld_LanguagePolicy_German> myOtherPolicyClass;
    myOtherPolicyClass hello2;
    hello2.Run();
}

      Makefile

all:policy

# which compiler
CC=g++ 
# Where are include file kept
INCLUDE = .

# Where to install
INSTDIR = /usr/local/bin

# Options for development
CFLAGS = -g -Wall -ansi

policy:policy.o
	$(CC) -o policy policy.o

policy.o:policy.cpp
#	$(CC) -I$(INCLUDE) $(CFLAGS) -c msgqueue.c
#	$(CC) -D_REENTRANT -c msgqueue.c -lpthread
	$(CC) -c policy.cpp -o policy.o

clean:
	-rm  policy.o policy

install:policy
	@if [-d $(INSTDIR) ];\
        then \
        cp policy $(INSTDIR);\
        chmod a+x $(INSTDIR)/policy;\
        chmod og-w $(INSTDIR)/policy;\
        echo "Install in $(INSTDIR)";\
    else \
       echo "Sorry,$(INSTDIR) does not exist";\
    fi 

      Runing result:

       Hello World

       Hello Policy

目录
相关文章
|
机器学习/深度学习 算法
尝试理解论文SPOT1的代码1:Supported Policy Optimization for Offline Reinforcement Learning
尝试理解论文SPOT1的代码1:Supported Policy Optimization for Offline Reinforcement Learning
137 0
|
设计模式 分布式计算 Kubernetes
译|Design patterns for container-based distributed systems(上)
译|Design patterns for container-based distributed systems
87 0
|
设计模式 缓存 监控
译|Design patterns for container-based distributed systems(下)
译|Design patterns for container-based distributed systems(下)
72 0
|
算法 数据挖掘 开发者
basic concept| 学习笔记
快速学习 basic concept。
basic concept| 学习笔记
|
机器学习/深度学习 自然语言处理 PyTorch
Re6:读论文 LeSICiN: A Heterogeneous Graph-based Approach for Automatic Legal Statute Identification fro
Re6:读论文 LeSICiN: A Heterogeneous Graph-based Approach for Automatic Legal Statute Identification fro
Re6:读论文 LeSICiN: A Heterogeneous Graph-based Approach for Automatic Legal Statute Identification fro
|
SQL 编译器 API
Efficiently Compiling Efficient Query Plans for Modern Hardware 论文解读
这应该是SQL查询编译的一篇经典文章了,作者是著名的Thomas Neumann,主要讲解了TUM的HyPer数据库中对于CodeGen的应用。 在morsel-driven那篇paper 中,介绍了HyPer的整个执行框架,会以task为单位处理一个morsel的数据,而执行的处理逻辑(一个pipeline job)就被编译为一个函数。这篇paper则具体讲如何实现动态编译。
442 0
Efficiently Compiling Efficient Query Plans for Modern Hardware 论文解读
|
机器学习/深度学习 人工智能 编解码
Paper:《Graph Neural Networks: A Review of Methods and Applications》解读(二)
Paper:《Graph Neural Networks: A Review of Methods and Applications》
|
机器学习/深度学习 数据可视化 数据挖掘
Paper:《Graph Neural Networks: A Review of Methods and Applications》解读(一)
Paper:《Graph Neural Networks: A Review of Methods and Applications》
Basic Concepts of Genetic Data Analysis
Basic Concepts of Genetic Data Analysis
906 0
|
JavaScript 前端开发
Guidelines for Function Compute Development - Troubleshoot Timeout Issues
Endless codes and endless bugs When you write code, you may inadvertently introduce some hidden bugs, even if you test a large proportion of the codes to the maximum extent possible.
1637 0