Skip to content

bekirduran/proxy_pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Proxy Pattern Example

The Proxy design pattern is a class functioning as an interface to another class or object. A Proxy could be for anything, such as a network connection, an object in memory, a file, or anything else you need to provide an abstraction between.

Types of proxies,

  • Virtual Proxy: An object that can cache parts of the real object, and then complete loading the full object when necessary.

  • Remote Proxy: Can relay messages to a real object that exists in a different address space.

  • Protection Proxy: Apply an authentication layer in front of the real object.

  • Smart Reference: An object whose internal attributes can be overridden or replaced.

In this example i try to explain the Virtual Proxy lets look at:

IRequestLayer.py class is interface

PostManagement.py class is implemented from IRequestLayer.py

PostProxy.py class is implemented from IRequestLayer.py

MainApplication.py we called our classes and run our code

Output: as you can see in the first call, it took the data from post management and brought it, then brought it from the cache.

With help of Virtual Proxy Design Pattern:

  • A virtual proxy can cache elements of a real subject before loading the full object into memory.. With the memory saving technique, The advantage of this approach is a faster application start-up time, as it is not required to created and load all of the application objects.



In this example i try to explain the Protection Proxy lets look at:

In general, objects in an application interact with each other to implement the overall application functionality. Most application object are generally accessible to all other objects in the application. At times, it may be necessary to restrict the accessibility of an object only to a limited set of client objects based on their access rights. When a client object tries to access such an object, the client is given access to the services provided by the object only if the client can furnish proper authentication credentials. In such cases, a separate object can be designated with the responsibility of verifying the access privileges of different client objects when they access the actual object. In other words, every client must successfully authenticate with this designated object to get access to the actual object functionality. Such an object with which a client needs to authenticate to get access to the actual object can be referred as an object authenticator which is implemented using the Protection Proxy.

IStaff.py class is interface

IDbAccessProxy.py is interface

Employee.py class is implemented from IStaff.py

Administrator.py class is implemented from IStaff.py

DbAccessProxy.py class some busines logic operation class

Output: Thanks to the protection proxy, we made the authorization restriction thanks to the interfaces.

With help of Protection Proxy Design Pattern:

  • A protection proxy can provide an authentication layer. For example, an NGINX proxy can add Basic Authentication restriction to an HTTP request.

About

Simple Proxy Pattern Example on Python

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages