首页 > 编程语言 > 详细

结合网上的知识,用c++实现了specification模式

时间:2014-12-25 00:13:36      阅读:255      评论:0      收藏:0      [点我收藏+]

specification.h

#pragma once

template<class T>
class ISpecification
{
public:
 virtual bool IsSatisfiedBy(T candidate) = 0;
 virtual ISpecification<T>* And(ISpecification<T>* other) = 0;
 virtual ISpecification<T>* Or(ISpecification<T>* other) = 0;
 virtual ISpecification<T>* Not() = 0;
};

template<class T>
class AndSpecification;
template<class T>
class OrSpecification;
template<class T>
class NotSpecification;

template<class T>
class CompositeSpecification : public ISpecification<T>
{
public:
 virtual bool IsSatisfiedBy(int candidate) = 0;
 virtual ISpecification<T>* And(ISpecification<T>* other);
 virtual ISpecification<T>* Or(ISpecification<T>* other);
 virtual ISpecification<T>* Not();
};

template<class T>
class AndSpecification : public CompositeSpecification<T>
{
public:
 AndSpecification(ISpecification<T>* x, ISpecification<T>* y);
 virtual bool IsSatisfiedBy(int candidate);
 
private:
 ISpecification<T>* m_one;
 ISpecification<T>* m_other;
};

template<class T>
class OrSpecification : public CompositeSpecification<T>
{
public:
 OrSpecification(ISpecification<T>* x, ISpecification<T>* y);
 virtual bool IsSatisfiedBy(int candidate);
 
private:
 ISpecification<T>* m_one;
 ISpecification<T>* m_other;
};

template<class T>
class NotSpecification : public CompositeSpecification<T>
{
public:
 NotSpecification(ISpecification<T>* x);
 virtual bool IsSatisfiedBy(int candidate);
private:
 ISpecification<T> *m_wrapped;
};

specification.cpp

// specification.cpp : 定义控制台应用程序的入口点。
//

#include "specification.h"

template<class T>
ISpecification<T>* CompositeSpecification<T>::And(ISpecification<T>* other)
{
 return new AndSpecification<T>(this, other);
}
template<class T>
ISpecification<T>* CompositeSpecification<T>::Or(ISpecification<T>* other)
{
 return new OrSpecification<T>(this, other);
}
template<class T>
ISpecification<T>* CompositeSpecification<T>::Not()
{
 return new NotSpecification<T>(this);
}


template<class T>
AndSpecification<T>::AndSpecification(ISpecification<T>* x, ISpecification<T>* y)
{
 m_one = x;
 m_other = y;
}
template<class T>
bool AndSpecification<T>::IsSatisfiedBy(int candidate)
{
 return m_one->IsSatisfiedBy(candidate) && m_other->IsSatisfiedBy(candidate);
}


template<class T>
OrSpecification<T>::OrSpecification(ISpecification<T>* x, ISpecification<T>* y)
{
 m_one = x;
 m_other = y;
}

template<class T>
bool OrSpecification<T>::IsSatisfiedBy(int candidate)
{
 return m_one->IsSatisfiedBy(candidate) || m_other->IsSatisfiedBy(candidate);
}

template<class T>
NotSpecification<T>::NotSpecification(ISpecification<T>* x)
{
 m_wrapped = x;
}

template<class T>
bool NotSpecification<T>::IsSatisfiedBy(int candidate)
{
 return !m_wrapped->IsSatisfiedBy(candidate);
}

 

 

 

test.cpp


#include "stdafx.h"
#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 _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vint;
 for (int i = -5; i < 10; i++)
  vint.push_back(i);

 ISpecification<int> *oddSpec = new OddSpecification();
 ISpecification<int> *positiveSpec = new PositiveSpecification();
 ISpecification<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;
 return 0;
}

感觉没什么问题,但是运行时错误,提示:

>test.obj : error LNK2001: 无法解析的外部符号 "public: virtual class ISpecification<int> * __thiscall CompositeSpecification<int>::And(class ISpecification<int> *)" (?And@?$CompositeSpecification@H@@UAEPAV?$ISpecification@H@@PAV2@@Z)
1>test.obj : error LNK2001: 无法解析的外部符号 "public: virtual class ISpecification<int> * __thiscall CompositeSpecification<int>::Or(class ISpecification<int> *)" (?Or@?$CompositeSpecification@H@@UAEPAV?$ISpecification@H@@PAV2@@Z)
1>test.obj : error LNK2001: 无法解析的外部符号 "public: virtual class ISpecification<int> * __thiscall CompositeSpecification<int>::Not(void)" (?Not@?$CompositeSpecification@H@@UAEPAV?$ISpecification@H@@XZ)
1>D:\vsproject\pattern_sln\Debug\specification.exe : fatal error LNK1120: 3 个无法解析的外部命令

没想明白,等下次解决。

结合网上的知识,用c++实现了specification模式

原文:http://blog.csdn.net/xidwong/article/details/42132971

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!