Decorator (C ++ implementation)

Source: Internet
Author: User

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

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

Using namespace STD;

 

Class component
{
Public:
Component ()
{
}
Virtual ~ Component ()
{
}

Virtual void operation () = 0;
};

 

 

 

Class concretecomponent: public component
{
Public:
Concretecomponent ()
{
Cout <"Construction of concretecomponet" <Endl;
}
Virtual ~ Concretecomponent ()
{
}

Virtual void operation ()
{
Cout <"opearation of concretecomponet" <Endl;
}
};

 

 

 

Class decorator: public component
{
Public:
Decorator (component * m_p): m_pcomponet (m_p)
{

}

Virtual ~ Decorator ()
{
Delete m_pcomponet;
M_pcomponet = NULL;
}

Virtual void operation ()
{
 M_pcomponet-> operation ();
}

PRIVATE:
Component * m_pcomponet;

};

Class concretedecoratea: Public decorator
{
Public:
Concretedecoratea (component * m_p): decorator (m_p)
{
Cout <"Construction of concretedecoratea" <Endl;
}
Virtual ~ Concretedecoratea ()
{
}
Virtual void operation ()
{
Cout <"Operation of concretedecoratea" <Endl;
Decorator: Operation ();
Drawline ();
}

PRIVATE:
Void drawline ()
{
Cout <"----------------------------" <Endl;
}

};

Class concretedecorateb: Public decorator
{
Public:
Concretedecorateb (component * m_p): decorator (m_p)
{
Cout <"Construction of concretedecorateb" <Endl;
}
Virtual ~ Concretedecorateb ()
{
}
Virtual void operation ()
{
Cout <"Operation of concretedecorateb" <Endl;
Decorator: Operation ();
Drawnumber ();
}
 
PRIVATE:
Void drawnumber ()
{
Cout <"1-2-3-4-5-6-7-8-9-0" <Endl;
}

};
Int _ tmain (INT argc, _ tchar * argv [])
{
Component * pcomponent = new concretecomponent ();
Decorator * pdecorator1 = new concretedecoratea (pcomponent );
Decorator * pdecorator2 = new concretedecorateb (pdecorator1 );
Pdecorator2-> operation ();

Return 0;
}

 

 

Result:

 

 

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.