In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge of how to achieve the Proxy pattern of C++ design pattern. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
The agency model is easy to understand, that is, to do something instead of others, for example, we need to buy fruit, usually go to the supermarket or fruit store to buy fruit, few people go to the orchard to buy fruit, the orchard is the place to produce fruit, but seldom sell fruit, here, the fruit store, the supermarket becomes the agent.
First define an abstract class that provides all the function interfaces.
1. Define the abstract category of selling fruit, that is, the interface, which is inherited by both orchards and supermarkets.
# pragma onceclass CSellFruits// defines an abstract class {public: CSellFruits (void); virtual ~ CSellFruits (void); virtual void sellapple () = 0Tracer / definition interface, sell Apple virtual void sellorange () = 0include SellFruits.h / definition interface, sell oranges}; # include "SellFruits.h" CSellFruits::CSellFruits (void) {} CSellFruits::~CSellFruits (void) {}
two。 Define specific categories, that is, orchards, orchards produce fruit, but generally do not buy fruit
# pragma once#include "sellfruits.h" # include class COrchard: public CSellFruits {public: COrchard (void); virtual ~ COrchard (void); virtual void sellapple (); virtual void sellorange ();}; # include "Orchard.h" COrchard::COrchard (void) {} COrchard::~COrchard (void) {} void COrchard::sellapple () {printf ("Sell apple\ n");} void COrchard::sellorange () {printf ("Sell orange\ n");}
3. Define a proxy class, a class that acts as an agent for selling fruit
# pragma once#include "sellfruits.h" # include "Orchard.h" # include class CProcySellFruits: public CSellFruits {public: CProcySellFruits (void); virtual ~ CProcySellFruits (void); virtual void sellapple (); virtual void sellorange (); private: CSellFruits * paired SellFruits; / / incoming interface object}; # include "ProcySellFruits.h" CProcySellFruits::CProcySellFruits (void): p_SellFruits (NULL) {} CProcySellFruits::~CProcySellFruits (void) {} void CProcySellFruits::sellapple () {if (this- > p_SellFruits==NULL) {this- > p_SellFruits=new COrchard () / / instantiate} this- > paired SellFruits-> sellapple () with the delegated class; / / sell apples in the orchard} void CProcySellFruits::sellorange () {if (this- > p_SellFruits==NULL) {this- > p_SellFruits=new COrchard (); / / instantiate the this- > paired SellFruits-> sellorange (); / / sell oranges in the orchard}
4. Actual call
CProxySellFruits* p=new CProxySellFruits (); / / use agent class to sell fruit p-> SellApple (); p-> SellOrange (). This is all the content of this article entitled "how to implement the Proxy pattern of C++ Design pattern". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.