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> *)" ([email protected][email protected]@@[email protected]@@[email protected]@Z)
1>test.obj : error LNK2001: 无法解析的外部符号 "public: virtual class ISpecification<int> * __thiscall CompositeSpecification<int>::Or(class ISpecification<int> *)" ([email protected][email protected]@@[email protected]@@[email protected]@Z)
1>test.obj : error LNK2001: 无法解析的外部符号 "public: virtual class ISpecification<int> * __thiscall CompositeSpecification<int>::Not(void)" ([email protected][email protected]@@[email protected]@@XZ)
1>D:\vsproject\pattern_sln\Debug\specification.exe : fatal error LNK1120: 3 个无法解析的外部命令
没想明白,等下次解决。