Proxy mode: Provides a proxy for other objects to control access to this object.
Proxy:
Save a reference so that the agent can access the entity. If the interface between Realsubject and subject is the same, proxy references subject, which is equivalent to holding a subject pointer in the proxy class, which points to realsubject;
Provides an interface that is the same as the subject interface, so that proxies can be used instead of entities;
Control access to the entity and may be responsible for creating and deleting it;
Other features depend on the type of proxy, for example:
The remote agent is responsible for encoding the request and its parameters and sending the encoded request to entities in different address spaces;
Virtual agents can cache additional information about an entity in order to delay access to it;
The protection agent checks to see if the caller has the necessary access rights to implement a request.
Subject: Define the common interface between Realsubject and proxy, so that proxy can be used wherever the realsubject is used;
Realsubject: Defines the entity that proxy is acting on.
1, the remote agent for an object in different address space to provide local agent;
2, virtual agent based on the need to create a large-cost object;
3, the protection agent controls the access of the original object; The protection agent is used when the object should have different access rights;
4. The smart reference replaces the simple pointer, which performs some additional operations when accessing the object, and its typical uses include:
A reference count that points to the actual object so that it can be automatically freed when there is no reference to the object;
Reference count Smart pointer:
#include <iostream> #include <windows.h>using namespace std; #define SAFE_DELETE (P) if (p) {DELETE p; p = NULL; }class Krefcount{public:krefcount (): M_ncount (0) {}public:unsigned AddRef () {return interlockedincrement (&m_ ncount); }unsigned Release () {return interlockeddecrement (&m_ncount);} void Reset () {m_ncount = 0;} private:unsigned long M_ncount;}; Template <typename T>class smartptr{public:smartptr (void): M_pdata (NULL) {m_preference = new Krefcount (); M_preference->addref (); } smartptr (t* pValue): M_pdata (pValue) {m_preference = new krefcount (); M_preference->addref (); } smartptr (const smartptr<t>& SP): M_pdata (Sp.m_pdata), M_preference (sp.m_preference) { M_preference->addref (); } ~smartptr (void) {if (m_preference && m_preference->release () = = 0) {Safe_del ETE (m_pdata); Safe_delete(m_preference); }} inline t& operator* () {return *m_pdata; } inline t* operator-> () {return m_pdata; } smartptr<t>& operator= (const smartptr<t>& SP) {if (this = &SP) { if (m_preference && m_preference->release () = = 0) {safe_delete (m_pdata); Safe_delete (m_preference); } m_pdata = Sp.m_pdata; M_preference = Sp.m_preference;m_preference->addref (); } return *this; } smartptr<t>& operator= (t* pValue) {if (m_preference && m_preference->release () = = 0) {Safe_delete (m_pdata); Safe_delete (m_preference); } m_pdata = PValue; M_preference = new Krefcount;m_preference->addref (); return *this; } t* Get () {t* ptr = NULL; ptr = m_pdata; return ptr; } VOID Attach (t* pobject) {if (m_preference->release () = = 0) {safe_delete (m_pdata); Safe_delete (m_preference); } m_pdata = Pobject; M_preference = new Krefcount; M_preference->addref (); } t* Detach () {t* ptr = NULL; if (m_pdata) {ptr = m_pdata; M_pdata = NULL; M_preference->reset (); } return ptr; }private:krefcount* m_preference; T* m_pdata;}; Class Ctest{public:ctest (int b): A (b) {}private:int A;}; int main () {smartptr<ctest> pSmartPtr1 (new CTest (10)); Smartptr<ctest> pSmartPtr2 (new ctest);p SmartPtr1 = PSMARTPTR2;}
When smart pointers are implemented using reference counting, this is the best example of using proxy mode. In the above example, Smartptr is a proxy class, and t* M_pdata is the actual data. SMARTPTR proxies The actual data, to implement the pointer behavior, add a reference count, thus implementing the smart pointer.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[C + + design mode] Proxy agent mode