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.
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.
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.
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.
Then in the service module, the client accesses the proxy object instead of the original object, where the permissions of users can be managed.
Last updated