修改后的specification模式c++实现,去掉了接口名的I前缀
来源:互联网 发布:淘宝返利怎么看返多少 编辑:程序博客网 时间:2024/05/07 01:44
//specification.h
#pragma once
template<class T>
class Specification
{
public:
virtual bool IsSatisfiedBy(T candidate) = 0;
virtual Specification<T>* And(Specification<T>* other) = 0;
virtual Specification<T>* Or(Specification<T>* other) = 0;
virtual Specification<T>* Not() = 0;
};
template<class T>
class CompositeSpecification : public Specification<T>
{
public:
virtual bool IsSatisfiedBy(int candidate) = 0;
virtual Specification<T>* And(Specification<T>* other);
virtual Specification<T>* Or(Specification<T>* other);
virtual Specification<T>* Not();
};
template<class T>
class AndSpecification : public CompositeSpecification<T>
{
public:
AndSpecification(Specification<T>* x, Specification<T>* y)
{
m_one = x;
m_other = y;
}
virtual bool IsSatisfiedBy(int candidate)
{
return m_one->IsSatisfiedBy(candidate) && m_other->IsSatisfiedBy(candidate);
}
private:
Specification<T>* m_one;
Specification<T>* m_other;
};
template<class T>
class OrSpecification : public CompositeSpecification<T>
{
public:
OrSpecification(Specification<T>* x, Specification<T>* y)
{
m_one = x;
m_other = y;
}
virtual bool IsSatisfiedBy(int candidate)
{
return m_one->IsSatisfiedBy(candidate) || m_other->IsSatisfiedBy(candidate);
}
private:
Specification<T>* m_one;
Specification<T>* m_other;
};
template<class T>
class NotSpecification : public CompositeSpecification<T>
{
public:
NotSpecification(Specification<T>* x)
{
m_wrapped = x;
}
virtual bool IsSatisfiedBy(int candidate)
{
return !m_wrapped->IsSatisfiedBy(candidate);
}
private:
Specification<T> *m_wrapped;
};
//CompositeSpecification
template<class T>
Specification<T>* CompositeSpecification<T>::And(Specification<T>* other)
{
return new AndSpecification<T>(this, other);
}
template<class T>
Specification<T>* CompositeSpecification<T>::Or(Specification<T>* other)
{
return new OrSpecification<T>(this, other);
}
template<class T>
Specification<T>* CompositeSpecification<T>::Not()
{
return new NotSpecification<T>(this);
}
//test.cpp
#include "specification.h"
#include <vector>
#include <algorithm>
using namespace std;
class OddSpecification : public CompositeSpecification<int>
{
public:
virtual bool IsSatisfiedBy(int candidate)
{
return candidate % 2 != 0;
}
};
class PositiveSpecification : public CompositeSpecification<int>
{
public:
virtual bool IsSatisfiedBy(int candidate)
{
return candidate > 0;
}
};
int main(int argc, char* argv[])
{
vector<int> vint;
for (int i = -5; i < 10; i++)
vint.push_back(i);
Specification<int> *oddSpec = new OddSpecification();
Specification<int> *positiveSpec = new PositiveSpecification();
Specification<int> *oddAndPositiveSpec = oddSpec->And(positiveSpec);
for (vector<int>::iterator it = vint.begin(); it != vint.end(); ++it)
{
if (oddAndPositiveSpec->IsSatisfiedBy(*it))
//if (oddSpec->IsSatisfiedBy(*it))
printf("%d\n", *it);
}
delete oddSpec;
delete positiveSpec;
delete oddAndPositiveSpec;
system("PAUSE");
return 0;
}
//vs2013测试通过
- 修改后的specification模式c++实现,去掉了接口名的I前缀
- 结合网上的知识,用c++实现了specification模式
- 如何修改类名、文件夹名的前缀
- ORACLE10g在计算机修改了计算机名后的处理办法
- Trie 前缀树的c 实现
- 用sed -i修改文件,去掉 “行首” 带“@”的首字母@
- 在Win xp上安装oracle 9i数据库后,马上修改计算名,监听服务起不了的解决方法
- 修改:类shell程序的简化实现,尝试消除ctrl+c,结果处理完信号后挑出了while循环
- db2修改计算机名后需要做的配置修改
- a类继承base,实现接口i,base父类实现了接口i的方法,请问a还需要实现接口方法吗?
- 彻底去掉css前缀的方法
- 去掉修改后extjs中grid 左上角的红三角
- 修改后的小米文件管理器(去掉远程管理)
- mysql批量删除指定前缀的表,批量修改表名的SQL语句
- mysql批量删除指定前缀的表,批量修改表名的SQL语句
- mysql批量删除指定前缀的表,批量修改表名的SQL语句
- mysql批量删除相同前缀的表和修改表名
- TFS 用户的配置,修改计算机名后的处理
- [Android]反编译apk + eclipse中调试smali
- Please complete the security check to access www.springframework.org
- 预编译,编译,汇编,链接,这些困扰我很久的东西今天是个了结了
- Ubuntu 14.04下Redis安装及简单测试
- 如何为eclipse设置编码格式?
- 修改后的specification模式c++实现,去掉了接口名的I前缀
- bb安装在sd卡时磁盘大小扩展
- Android SDK Android NDK Android Studio 官方下载地址
- Fix "/dev/sda1 unexpected inconsistency, run fsck manually"
- 让别人快乐地捐钱:英国人的慈善教育
- 如何更改tomcat的默认目录
- 第18周OJ测试项目6--用数字造数字
- ocp|ocm考证系列文章!
- 补 实验三 栈和队列实验