Combined with the knowledge of the Internet, the specification mode is realized by C + +

Source: Internet
Author: User

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: Defines the entry point of the console application.
//

#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 <; 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;
}

Feeling fine, but run-time error, prompt:

>test.obj:error LNK2001: unresolved external symbol "Public:virtual class ispecification<int> * __thiscall compositespecificat Ion<int>::and (class ispecification<int> *) "([email protected][email protected]@@[email protected]@@[email protected]@z)
1>test.obj:error LNK2001: unresolved external symbol "Public:virtual class ispecification<int> * __thiscall Compositespecifica Tion<int>::or (class ispecification<int> *) "([email protected][email protected]@@[email protected]@@[email protected]@z)
1>test.obj:error LNK2001: unresolved external symbol "Public:virtual class ispecification<int> * __thiscall Compositespecifica Tion<int>::not (void) "([email protected][email protected]@@[email protected]@ @XZ)
1>d:\vsproject\pattern_sln\debug\specification.exe:fatal error Lnk1120:3 an unresolved external command

Don't want to understand, wait for the next solution.

Combined with the knowledge of the Internet, the specification mode is realized by C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.