📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page

Was this helpful?

  1. Chapter 13: Design Patterns

Proxy Pattern

The proxy pattern provides a proxy for other objects which controls the access permission of objects. Let's look at this example, where we have a abstract class Movie and a derived class MovieSite which provides free movies and VIP movies. Free movies are for general users, while VIP movies are for members.

class Movie {
public:
    virtual void freeMovie() = 0;
    virtual void vipMovie() = 0;
};
​
class MovieSite : public Movie {
public:
    virtual void freeMovie() {
        cout << "Free Movie" << endl;
    }
    virtual void vipMovie() {
        cout << "VIP Movie" << endl;
    }
};

However, if we write the class like this, its objects can access both freeMovie() and vipMovie(), which means we have to judge the authority of users in the service layer. It is apparently not a good OOP design.

int main() {
    MovieSite *p = new MovieSite();
    p->freeMovie(); // Free Movie
    p->vipMovie();  // VIP Movie
    delete p;
    return 0;
}

A better approach is to provide a proxy class which controls the access permission of users. Here we have a FreeMovieProxy which acts as a proxy of free movies. The class combines a MovieSite object as its member. In this way, the freeMovie() method of the delegate class MovieSite is called through the freeMovie() method of its proxy class. Moreover, since general users can not watch VIP movies, the vipMovie() method of MovieSite can not be called through the proxy.

class FreeMovieProxy : public Movie {
public:
    FreeMovieProxy() {
        pMovie = new MovieSite();
    }
    ~FreeMovieProxy() {
        delete pMovie;
    }
    virtual void freeMovie() {
        pMovie->freeMovie();
    }
    virtual void vipMovie() {
        cout << "Permission denied!" << endl;
    }
private:
    MovieSite *pMovie;
}

Similarly, we have a VipMovieProxy class which acts as a proxy of VIP movies. In this class, users have the full permissions to freeMovie() and vipMovie() of MovieSite.

class VipMovieProxy : public Movie {
public:
    VipMovieProxy() {
        pMovie = new MovieSite();
    }
    ~VipMovieProxy() {
        delete pMovie;
    }
    virtual void freeMovie() {
        pMovie->freeMovie();
    }
    virtual void vipMovie() {
        pMovie->vipMovie();
    }
private:
    MovieSite *pMovie;
}

Then in the service module, the client accesses the proxy object instead of the original object, where the permissions of users can be managed.

int main() {
    MovieSite *p = new FreeMovieProxy();
    p->freeMovie(); // Free Movie
    p->vipMovie();  // Permission denied!
    delete p;
    return 0;
}
PreviousFactory PatternNextDecorator Pattern

Last updated 4 years ago

Was this helpful?