Design patterns: Proxy

The proxy pattern is quite similar to the Bridge pattern, but the purpose of the Proxy is different, its purpose is to create an object that will represent another object, so in a nutshell, more precisely about this pattern further. I will try to present the majority of examples in several languages m.i.n in C ++, Java, C # and python, there will be a link to github at the end of the entry.

 

Intent

  • Controls access to classes and its objects, indirectly through a different class and objects.
  • Creates packaging for classes to protect them from excessive complexity.
  • It creates objects (usually those that eat up a lot of processor resources) when they are really needed.

 

Problem

You need to create some resource-consuming objects, but you do not want to create their instances quickly. A proxy will be useful here.

Or another example, you want to create a class, but it must be well-encapsulated and controlled, such a class may, for example, represent a system of making transfers in the bank system.

 

Use when:

  • You want to create an object that is expensive to create, then use a virtual proxy that will create the object on demand.
  • You want to have limited access to the facility, e.g. on a password
  • You want to create an object that will represent objects in a different address space, eg it can be a proxy class that uses data stored on another server for operation.

 

Structure

Below the UML diagram of the proxy pattern, it can be seen that only in the proxy class we create a real object.

 

Example

Below is the schema of the proxy pattern in the code, it looks more or less that:

When we run this program, we must enter the correct password in the console, in this case it is root, if we type it we create a class object, in this pattern we create the object when we need it, so we do not do it through Dependency Injection, but we created for it a special method that creates this object, when we enter the correct password, you can create a factory in this pattern so that class instances are well-encapsulated but in this simple example we will not play with it.

Result

 

An example of life taken

We will make an example on the principle of making transfers in the banking system, you can withdraw money on the basis of a bank card from the bank and thanks to it we can control how much money we have in the bank, how much we want to pay out, etc. This is shown in the picture below:

Let’s see how it looks in the code:

It is long this example, but I think that it shows well action an proxy pattern.

I attributed $ 10,000 to our account 🙂 (it’s a pity that it’s just fairytale money 😥😥 )

As you can see first, the client enters a pin and then the proxy class checks if it is correct ✅, if it’s correct, it opens access to the account, in our example it appears in the form of the BankAccount class object using the factory pattern.

And we set the values true, false depending on whether the pin is correct. And finally, in the client if the pin is correct ✅ then we call the methods of the BankAccount class, if not ❌ then we only display the appropriate message.

Result

 

Relations with design patterns

  • The adapter provides a different interface for the object. Proxy provides the same interface. The decorator provides an extended interface.
  • The decorator and proxy have different goals, but similar structures. Both describe how to provide an intermediate level to another object, and implementations retain a reference to the object to which they send requests.

 

Summary

That’s all about Proxy 🙂.

Link to github with the whole code from this article: https://github.com/Slaw145/ProxyTutorial

This content also you can find on my steemit blog https://steemit.com/design-patterns/@slawas/design-patterns-proxy

And on medium: https://medium.com/@sawomirkowalski/design-patterns-proxy-757b722b6ca8

In the next article, we will talk about the Flyweight pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb: Devman.pl-Sławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

 
If that post was useful for you share it with your friends :)

Post a comment

Be first!

avatar
  Subscribe  
Notify about