Design Mode C ++ implementation (8)-proxy Mode

Source: Internet
Author: User

The design patterns in the software field provide developers with an effective way to use expert design experience. Objects are used in the design model.Programming LanguageImportant features: encapsulation, inheritance, polymorphism, true understanding of the essence of the design model is a long process that requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: the basis for reusable object-oriented software" (DP. This article describes how to implement the proxy mode.

[DP] definition: provides a proxy for other objects to control access to this object. There are four common cases: (1) remote proxy, (2) virtual proxy, (3) proxy protection, and (4) Smart reference. This article mainly introduces two situations: Virtual proxy and smart reference.

Consider a document editor that can embed graphical objects in a document. The cost of creating some graphic objects is high. However, opening a document must be fast, so we should avoid creating all the objects with high overhead at once when opening the document. The proxy mode can be used here. When opening a document, the graphical object proxy is opened instead of the graphical object proxy to replace the real image. When you need to open the image, the proxy is still responsible for opening the image. This is an example given in [DP. The following is a UML diagram of the proxy mode.

The simple implementation is as follows:

Class image {public: image (string name): m_imagename (name) {} virtual ~ Image () {} virtual void show () {} protected: String m_imagename;}; Class bigimage: public image {public: bigimage (string name): image (name ){}~ Bigimage () {} void show () {cout <"show big image:" <m_imagename <Endl ;}}; class bigimageproxy: public image {PRIVATE: bigimage * m_bigimage; public: bigimageproxy (string name): image (name), m_bigimage (0 ){}~ Bigimageproxy () {Delete m_bigimage;} void show () {If (m_bigimage = NULL) m_bigimage = new bigimage (m_imagename); m_bigimage-> show ();}};

Customer calls:

 
Int main () {image * image = new bigimageproxy ("proxy.jpg"); // The proxy image-> show (); // when necessary, the proxy is responsible for opening the delete image; return 0 ;}

in this case, the virtual proxy is used. The following are two smart reference examples. One is auto_ptr in C ++, and the other is smart_ptr. I implemented it myself. The Code Implementation of auto_ptr is provided first:

 template 
  
    class auto_ptr {public: explicit auto_ptr (T * p = 0): pointee (P) {} auto_ptr (auto_ptr 
   
     & RHs): pointee (RHS. release ()){}~ Auto_ptr () {Delete pointee;} auto_ptr 
    
      & operator = (auto_ptr 
     
       & RHs) {If (this! = & RHs) reset (RHS. release (); return * This;} T & operator * () const {return * pointee;} t * operator-> () const {return pointee ;} T * Get () const {return pointee;} t * release () {T * oldpointee = pointee; pointee = 0; return oldpointee;} void reset (T * p = 0) {If (pointee! = P) {Delete pointee; pointee = P ;}} PRIVATE: T * pointee ;}; 
     
    
   
  

After reading the above code, we can find that the auto_ptr class is a proxy. The customer only needs to operate the auto_prt object, instead of dealing with the pointee pointer to be proxy. Auto_ptr provides exception security for dynamically assigned objects. Because it uses an object to store resources that need to be automatically released, and then relies on the object's destructor to release resources. In this way, the customer does not need to pay attention to the release of resources, which is automatically completed by the auto_ptr object. One key in implementation is to reload the reference operators and arrow operators, so that the use of auto_ptr is similar to that of real pointers.

We know that there is no garbage collection mechanism in C ++ and it can be compensated by the smart pointer. The following shows an implementation of the smart pointer, using the reference counting policy.

 template 
  
    class smart_ptr {public: smart_ptr (T * p = 0): pointee (P ), count (New size_t (1) {}// the initial count value is 1smart_ptr (const smart_ptr & RHs): pointee (RHS. pointee), count (RHS. count) {++ * count;} // copy the constructor and Add 1 ~ To the count ~ Smart_ptr () {decr_count ();} // destructor. The count is reduced by 1. Garbage collection is performed when the count is reduced to 0, that is, smart_ptr & operator = (const smart_ptr & RHs) is released) // The overload value assignment operator {// assign values to itself, because if the value is assigned to itself, the counter first minus 1, plus 1, and does not change ++ * count; decr_count (); pointee = RHS. pointee; Count = RHS. count; return * This;} // reload the arrow operator and the quote operator. No pointer check T * operator-> () {return pointee;} is provided ;} const T * operator-> () const {return pointee;} T & operator * () {return * pointee;} const T & operator * () const {return * pointee ;} size_t get_refcount () {return * count;} // obtain the reference counter value PRIVATE: T * pointee; // The actual pointer, represented by size_t * count; // reference counter void decr_count () // counter minus 1 {If (-- * COUNT = 0) {Delete pointee; Delete count ;}}; 
  

I have a blogArticleCopyright, reprinted please indicate the sourceHttp://blog.csdn.net/wuzhekai1985

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.