-
Notifications
You must be signed in to change notification settings - Fork 7.1k
enhance top description #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7681bbd
enhance yop document
hojatm-huma b2df6c0
changed line len to 80 char
hojatm-huma 0faf800
changed example and TL;DR part to have more clarification
hojatm-huma 7e6254c
change TL;DR to mention proxy can add logic
hojatm-huma 8dbfbd6
add changes to example to contain client real-subject usage
hojatm-huma 0f16335
changed output to doctest
hojatm-huma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,87 @@ | ||
| """ | ||
| *What is this pattern about? | ||
| Proxy is used in places where you want to add functionality to a class without | ||
| changing its interface. The main class is called `Real Subject`. A client should | ||
| use the proxy or the real subject without any code change, so both must have the | ||
| same interface. Logging and controlling access to the real subject are some of | ||
| the proxy pattern usages. | ||
|
|
||
| *References: | ||
| https://refactoring.guru/design-patterns/proxy/python/example | ||
| https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Fronting.html | ||
|
|
||
| *TL;DR | ||
| Provides an interface to resource that is expensive to duplicate. | ||
| Add functionality or logic (e.g. logging, caching, authorization) to a resource | ||
| without changing its interface. | ||
| """ | ||
|
|
||
| import time | ||
|
|
||
| class Subject: | ||
| """ | ||
| As mentioned in the document, interfaces of both RealSubject and Proxy should | ||
| be the same, because the client should be able to use RealSubject or Proxy with | ||
| no code change. | ||
|
|
||
| Not all times this interface is necessary. The point is the client should be | ||
| able to use RealSubject or Proxy interchangeably with no change in code. | ||
| """ | ||
|
|
||
| def do_the_job(self, user): | ||
| raise NotImplementedError() | ||
|
|
||
| class SalesManager: | ||
| def talk(self): | ||
| print("Sales Manager ready to talk") | ||
|
|
||
| class RealSubject(Subject): | ||
| """ | ||
| This is the main job doer. External services like payment gateways can be a | ||
| good example. | ||
| """ | ||
|
|
||
| class Proxy: | ||
| def do_the_job(self, user): | ||
| print(f'I am doing the job for {user}') | ||
|
|
||
|
|
||
| class Proxy(Subject): | ||
| def __init__(self): | ||
| self.busy = 'No' | ||
| self.sales = None | ||
|
|
||
| def talk(self): | ||
| print("Proxy checking for Sales Manager availability") | ||
| if self.busy == 'No': | ||
| self.sales = SalesManager() | ||
| time.sleep(0.1) | ||
| self.sales.talk() | ||
| self._real_subject = RealSubject() | ||
|
|
||
| def do_the_job(self, user): | ||
| """ | ||
| logging and controlling access are some examples of proxy usages. | ||
| """ | ||
|
|
||
| print(f'[log] Doing the job for {user} is requested.') | ||
|
|
||
| if user == 'admin': | ||
| self._real_subject.do_the_job(user) | ||
| else: | ||
| time.sleep(0.1) | ||
| print("Sales Manager is busy") | ||
| print(f'[log] I can do the job just for `admins`.') | ||
|
|
||
|
|
||
| def client(job_doer, user): | ||
| job_doer.do_the_job(user) | ||
|
|
||
| def main(): | ||
| """ | ||
| >>> proxy = Proxy() | ||
|
|
||
| >>> real_subject = RealSubject() | ||
|
|
||
| >>> client(proxy, 'admin') | ||
| [log] Doing the job for admin is requested. | ||
| I am doing the job for admin | ||
|
|
||
| >>> client(proxy, 'anonymous') | ||
| [log] Doing the job for anonymous is requested. | ||
| [log] I can do the job just for `admins`. | ||
|
|
||
| >>> client(real_subject, 'admin') | ||
| I am doing the job for admin | ||
|
|
||
| class NoTalkProxy(Proxy): | ||
| def talk(self): | ||
| print("Proxy checking for Sales Manager availability") | ||
| time.sleep(0.1) | ||
| print("This Sales Manager will not talk to you", "whether he/she is busy or not") | ||
| >>> client(real_subject, 'anonymous') | ||
| I am doing the job for anonymous | ||
| """ | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| p = Proxy() | ||
| p.talk() | ||
| p.busy = 'Yes' | ||
| p.talk() | ||
| p = NoTalkProxy() | ||
| p.talk() | ||
| p.busy = 'Yes' | ||
| p.talk() | ||
|
|
||
| ### OUTPUT ### | ||
| # Proxy checking for Sales Manager availability | ||
| # Sales Manager ready to talk | ||
| # Proxy checking for Sales Manager availability | ||
| # Sales Manager is busy | ||
| # Proxy checking for Sales Manager availability | ||
| # This Sales Manager will not talk to you whether he/she is busy or not | ||
| # Proxy checking for Sales Manager availability | ||
| # This Sales Manager will not talk to you whether he/she is busy or not | ||
| import doctest | ||
| doctest.testmod() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.