Observer (C ++ implementation)

Source: Internet
Author: User

// Observer. cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include <iostream>
# Include <list>
# Include <algorithm>

 

Using namespace STD;

 

Typedef int state;

 

Class observer;

Class subject
{
Public:
Subject (): m_nsubjectstate (-1 ){}
Virtual ~ Subject ();

Void y ();
Void attach (Observer * pobserver );
Void detach (Observer * pobserver );

Virtual void setstate (State nstate );
Virtual State getstate ();

Protected:
State m_nsubjectstate;
List <Observer *> m_listobserver;
};

Class observer
{
Public:
Observer (): m_nobserverstate (-1 ){}
Virtual ~ Observer (){}

Virtual void Update (subject * psubject) = 0;

Protected:
State m_nobserverstate;
};

Class concreatesubject
: Public subject
{
Public:
Concreatesubject (): subject (){}
Virtual ~ Concreatesubject ()
{

}

Virtual void setstate (State nstate );
Virtual State getstate ();

};

Class concreateobserver: public observer
{
Public:
Concreateobserver (): Observer (){}
Virtual ~ Concreateobserver (){}

Virtual void Update (subject * psubject );
};

Void subject: attach (Observer * pobserver)
{
Cout <"attach an observer" <Endl;
M_listobserver.push_back (pobserver );
}

Void subject: Detach (Observer * pobserver)
{
STD: List <Observer *>: iterator ITER;
Iter = find (m_listobserver.begin (), m_listobserver.end (), pobserver );
If (m_listobserver.end ()! = ITER)
{
M_listobserver.erase (ITER );
Cout <"detach an observer" <Endl;
}
Else
{
Cout <"No this observer, so cannot detach it" <Endl;
}
}

Void subject: Policy ()
{
Cout <"policy to all observers" <Endl;
List <Observer *>: iterator ITER;
For (iter = m_listobserver.begin (); iter! = M_listobserver.end (); ITER ++)
{
(* ITER)-> Update (this );
}
}

Void subject: setstate (State nstate)
{
M_nsubjectstate = nstate;
}

State subject: getstate ()
{
Return m_nsubjectstate;
}

Subject ::~ Subject ()
{

STD: List <Observer *>: iterator ITER, temp;

For (iter = m_listobserver.begin (); iter! = M_listobserver.end ();)
{
Temp = ITER;
++ ITER;
Delete (* temp );
}
 
M_listobserver.clear ();
}

Void concreatesubject: setstate (State nstate)
{
STD: cout <"setstate by concreatesubject/N ";
M_nsubjectstate = nstate;
}

State concreatesubject: getstate ()
{
STD: cout <"getstate by concreatesubject/N ";
Return m_nsubjectstate;
}
Void concreateobserver: Update (subject * psubject)
{
If (null = psubject)
Return;

M_nobserverstate = psubject-> getstate ();

STD: cout <"The obeserverstate is" <m_nobserverstate <STD: Endl;
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Observer * P1 = new concreateobserver ();
Observer * P2 = new concreateobserver ();

Subject * P = new concreatesubject ();
P-> attach (P1 );
P-> attach (P2 );
P-> setstate (8 );
P-> Policy ();

P-> detach (P1 );
P-> setstate (10 );
P-> Policy ();

Delete P;
Return 0;
}

 

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.