|
| 1 | +<div id="top"></div> |
| 2 | + |
| 3 | +# Proxy Design Pattern |
| 4 | +also Known as `Surrogate` |
| 5 | + > GOF: |
| 6 | + > - structural pattern. |
| 7 | + > - Provide a surrogate (replacement) or placeholder for another object to control access to it. |
| 8 | + >> - A proxy controls access to the original object, |
| 9 | + >> - allowing you to perform something either before or after the request gets through to the original object. |
| 10 | +
|
| 11 | +image form: https://refactoring.guru/design-patterns/proxy#:~:text=Proxy%20is%20a%20structural%20design,through%20to%20the%20original%20object. |
| 12 | + |
| 13 | +<p align="center"> |
| 14 | + <img style="background-color:#5547" src = "assets/proxy_structure.png" > |
| 15 | +</p> |
| 16 | +<p align="center"> |
| 17 | + <img style="background-color:#5547" src = "assets/proxy_structure_java_insider.gif" > |
| 18 | +</p> |
| 19 | + |
| 20 | +<details> |
| 21 | + <summary> <h2 style="display: inline;"> Sections</h2> </summary> |
| 22 | + |
| 23 | +- [Definitions](#Definitions) |
| 24 | +- [What problems can it solve](#What-problems-can-it-solve) |
| 25 | +- [What solution does it describe](#What-solution-does-it-describe) |
| 26 | +- [Applicability](#Applicability) |
| 27 | +- [Examples](#Examples) |
| 28 | + - [Image Example](#Image-Example) |
| 29 | + - [3rd party youtube example](#3rd-party-youtube-example) |
| 30 | + - [Router Example](#Router-Example) |
| 31 | +- [Sources](#Sources) |
| 32 | +</details> |
| 33 | + |
| 34 | + |
| 35 | +## Definitions |
| 36 | + |
| 37 | +- <details> |
| 38 | + <summary> <h3 style="display: inline;"> Tutorial Point</h3> </summary> |
| 39 | + - a class represents functionality of another class. |
| 40 | + </details> |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +- <details> |
| 45 | + <summary> <h3 style="display: inline;"> Wikipedia</h3> </summary> |
| 46 | + |
| 47 | + - A proxy: in its most general form, is a **class functioning as an interface to something else**. |
| 48 | + - The proxy could interface to anything: |
| 49 | + - a `network connection`, a` large object in memory`, a `file`, or` some other resource` |
| 50 | + - that is expensive or impossible to duplicate. |
| 51 | + > - In short, a proxy is a wrapper or agent object that is being called by the client |
| 52 | + >- to access the real serving object behind the scenes. |
| 53 | + >- Use of the proxy can simply be forwarding to the real object, or can provide additional logic. |
| 54 | + >- In the proxy, extra functionality can be provided, |
| 55 | + >--- |
| 56 | + - > for example --- |
| 57 | + - > caching when operations on the real object are resource intensive, |
| 58 | + - > or checking preconditions before operations on the real object are invoked. |
| 59 | + - > For the client, usage of a proxy object is similar to using the real object, because both implement the same interface. |
| 60 | + </details> |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## What problems can it solve |
| 65 | + |
| 66 | +1. The access to an object should be controlled. |
| 67 | +2. Additional functionality should be provided when accessing an object. |
| 68 | + - >When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights. |
| 69 | +
|
| 70 | + |
| 71 | + |
| 72 | +## What solution does it describe |
| 73 | +<!-- wikipedia --> |
| 74 | +- <details> |
| 75 | + <summary> <h3 style="display: inline;">wikipedia</h3> </summary> |
| 76 | + |
| 77 | + >- `wikipedia`: This makes it possible to work through a Proxy object to perform additional functionality when accessing a subject. For example, to check the access rights of clients accessing a sensitive object. |
| 78 | + </details> |
| 79 | +
|
| 80 | +<!-- GOF Motivation --> |
| 81 | +- <details> |
| 82 | + <summary> <h3 style="display: inline;">GOF Motivation</h3> </summary> |
| 83 | + |
| 84 | + - `Gof`: One reason for controlling access to an object is to defer (delay) the full cost of its creation and initialization until we actually need to use it. |
| 85 | + - **Example** Consider a document editor that can embed graphical objects in a document. |
| 86 | + - Some graphical objects, like large raster images, can be expensive to create. |
| 87 | + - But opening a document should be fast, |
| 88 | + - so we should avoid creating all the expensive objects at once when the document is opened. |
| 89 | + |
| 90 | + > This isn't necessary anyway, because not all of these objects will be visible in the document at the same time. |
| 91 | + --- |
| 92 | + - Full Example in dart: <a href="image_example/" target="_blank"> click here </a> |
| 93 | + - Example Source GOF : <a href="https://books.google.com.eg/books/about/Design_Patterns.html?id=6oHuKQe3TjQC&printsec=frontcover&source=kp_read_button&hl=en&redir_esc=y#v=onepage&q&f=false" target="_blank">Design Patterns: Elements of Reusable Object-Oriented Software</a> |
| 94 | + </details> |
| 95 | + |
| 96 | +## Applicability |
| 97 | +Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several common situations in which the Proxy pattern is applicable: |
| 98 | +### 1. remote proxy: |
| 99 | + - provides a local representative for an object in a different address space. |
| 100 | + |
| 101 | +### 2. virtual proxy: |
| 102 | +- creates expensive objects on demand. |
| 103 | +- The ImageProxy described in the Motivation is an example of such a proxy. |
| 104 | + |
| 105 | +### 3. protection proxy: |
| 106 | +- controls access to the original object. |
| 107 | +- Protection proxies are useful when objects should have different access rights. |
| 108 | + |
| 109 | +### 4. smart reference: |
| 110 | +> also called `smart pointers` |
| 111 | +- is a replacement for a bare pointer that performs additional actions when an object is accessed. |
| 112 | +- Typical uses include |
| 113 | + 1. counting the number of references to the real object so that it can be freed automatically when there are no more references. |
| 114 | + 1. loading a persistent object into memory when it's first referenced. |
| 115 | + 1. checking that the real object is locked before it's accessed to ensure that no other object can change it. |
| 116 | + |
| 117 | +### the UML class and sequence diagram below. |
| 118 | + |
| 119 | +<p align="center" ><img src = "assets/Proxy_Design_Pattern_UML_wiki.jpg"></p> |
| 120 | + |
| 121 | +> - To act as substitute for a subject, a proxy must implement the Subject interface. |
| 122 | +> - Clients can't tell whether they work with a subject or its proxy. |
| 123 | +
|
| 124 | +## Examples |
| 125 | + |
| 126 | +### Image Example |
| 127 | +- Example in dart: <a href="image_example/" target="_blank"> click here </a> |
| 128 | +- Example Source: GOF && https://www.tutorialspoint.com/design_pattern/proxy_pattern.htm |
| 129 | + |
| 130 | + |
| 131 | +### 3rd party youtube example |
| 132 | +- Example in dart: <a href="3rd_party_youtube_example/" target="_blank"> click here </a> |
| 133 | +- Example Source: https://refactoring.guru/design-patterns/proxy/java/example |
| 134 | + |
| 135 | +### Router example |
| 136 | +- Example in dart: <a href="router_example/" target="_blank"> click here </a> |
| 137 | +- this example to show the protection proxy |
| 138 | + |
| 139 | + |
| 140 | +<table> |
| 141 | + <tr> |
| 142 | + <td><img src="assets/proxy_simplify.jpeg"></td> |
| 143 | + <td><img style="background-color:#5547" src="assets/Proxy_example.png"><td> |
| 144 | + </tr> |
| 145 | +</table> |
| 146 | + |
| 147 | +## Sources |
| 148 | + |
| 149 | +- https://refactoring.guru/design-patterns/proxy#:~:text=Proxy%20is%20a%20structural%20design,through%20to%20the%20original%20object. |
| 150 | + |
| 151 | +- GOF Book |
| 152 | +- https://www.tutorialspoint.com/design_pattern/proxy_pattern.htm |
| 153 | +- https://en.wikipedia.org/wiki/Proxy_pattern |
| 154 | + |
| 155 | +<p align="right">(<a href="#top">back to top</a>)</p> |
0 commit comments