結合網上的知識,用c++實現了specification模式,網上specification

來源:互聯網
上載者:User

結合網上的知識,用c++實現了specification模式,網上specification

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 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 個無法解析的外部命令

沒想明白,等下次解決。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.