With regards to the GoF design patterns, is it possible to combine the Factory and Proxy patterns and if so where might this design be used?
-
Which Factory design pattern? The GoF includes two of them. In general it is possible to combine any two patterns (or any ten patterns for that matter). Pattern examples will be language specific, e.g. there is a large collection for [Java](https://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries). – jaco0646 Apr 10 '20 at 13:28
1 Answers
I think any patterns can be combined. It just depends on the need.
Some of the usecases Proxy pattern:
1. Control access
Ex: Read Account Details but not modify.
2. To do Lazy instantiation of objects whose construction requires costly resources.
Just for the sake of giving an example of a combination of factory and proxy, i will create a random problem of the first usecase.
Imagine a bank with different kind of employees.
Manager,Accountant,Cashier and so on.
Every customer has a account, which can do operations like,readAcountDetails(), addMoney(),removeMoney(),updateAccountDetails() and so on.
Based on his given role,each employee of the bank has rights to perform only specific operations.
This access right problem can be solved by proxy pattern(one of the approach).i.e by giving a proxy for each role in the bank,like
Managerproxy: can do only updateAccountDetails(),for other operation ,access denied.
CashierProxy: addMoney(),removeMoney(),for other operation ,access denied.
AccountantProxy :readAcountDetails(),for other operation ,access denied.
So now we can have a Factory which return a proxy classes for different types of roles in the bank

- 161
- 1
- 6