Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proxy. PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access.

Similar presentations


Presentation on theme: "Proxy. PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access."— Presentation transcript:

1 Proxy

2 PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access However, the ”user” (i.e programmer) should not be aware of this

3 PBA WEB – BEWP 3 The Proxy pattern Why would we want to use a Proxy…? Several possible reasons: –Security –Less costly computations –Bookkeeping –Caching –Remoting Using a Proxy must not change the functionality as such

4 PBA WEB – BEWP 4 Constructing a Proxy The object we wish to create a Proxy for has the class Subject, and implements the interface SubjectInterface The Proxy class must therefore also implement the interface SubjectInterface User only accesses the functionality through the interface!

5 PBA WEB – BEWP 5 Constructing a Proxy SubjectInterface Method1 Method2 Method3 … SubjectProxy Method1 Method2 Method3 … Subject Method1 Method2 Method3 …

6 PBA WEB – BEWP 6 User code public void UserMethod(SubjectInterface si) { si.Method1();... } User does not know if the real type of si is a Subject or SubjectProxy Setting up the Proxy is done elsewhere

7 PBA WEB – BEWP 7 User cannot tell the difference! The Proxy pattern Our code Subject Interface SubjectProxySubject

8 PBA WEB – BEWP 8 Inside the Proxy The actual implementation of the Proxy will of course vary with the purpose Usually, the Proxy will hold a reference to the real object, and (sometimes) make calls to the real object

9 PBA WEB – BEWP 9 Inside the Proxy public class SubjectProxy : SubjectInterface { private Subject theSubject;... public SubjectProxy() { theSubject = new Subject(); } public void Method1() { theSubject.Method1(); }... } Not a very useful Proxy…

10 PBA WEB – BEWP 10 Inside the Proxy The previous implemen- tation of a Proxy was not very useful – does the same as direct access How can we change the implementation to better suit certain scenarios?

11 PBA WEB – BEWP 11 Inside the Proxy Scenario 1: Subject is very expensive to create Low probability that subject is ever called during a run of the program Solution: Delay creation of subject

12 PBA WEB – BEWP 12 Inside the Proxy public class SubjectProxy : SubjectInterface { private Subject theSubject; public SubjectProxy() { // Postpone creation of Subject } private void CreateSubject() { if (theSubject == null) theSubject = new Subject(); }

13 PBA WEB – BEWP 13 Inside the Proxy public class SubjectProxy : SubjectInterface { // (continued…) public void Method1() { CreateSubject(); // Make sure Subject is ready theSubject.Method1(); } public void Method2() { CreateSubject(); // Make sure Subject is ready theSubject.Method2(); }... }

14 PBA WEB – BEWP 14 Inside the Proxy Scenario 2: Method calls to the subject are very expensive Same input always gives same output Solution: Make a cache of results, and check the cache before calling subject

15 PBA WEB – BEWP 15 Inside the Proxy public class SubjectProxy : SubjectInterface { private Subject theSubject; Dictionary cache; public Result ExpensiveMethod(Input in) { if (cache.ContainsKey(in)) return cache[in]; else { Result res = theSubject.ExpensiveMethod(in); cache.Add(in, res); return res; }

16 PBA WEB – BEWP 16 Inside the Proxy Scenario 3: Some methods can only be called by users with certain user rights Solution: Obtain and check user rights, before forwarding calls to subject

17 PBA WEB – BEWP 17 Inside the Proxy public class SubjectProxy : SubjectInterface {... public Result Method1() { UserRights theUserRights = GetUserRights(); if (CheckUserRights(theUserRights, ”Method1”)) { return theSubject.Method1(); else { // E.g. throw exception... }


Download ppt "Proxy. PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access."

Similar presentations


Ads by Google