Difference between revisions of "Proxy Pattern"
From Logic Wiki
(Created page with "Category:Extreme Programming Category:Design Patterns Category:OOP == Video == [https://www.youtube.com/watch?v=NwaabHqPHeM https://www.youtube.com/watch?v=Nwaab...") |
|||
| Line 17: | Line 17: | ||
[[File:Proxy.gif]] | [[File:Proxy.gif]] | ||
| + | == Implementation == | ||
| + | === Interface === | ||
| + | <pre class="brush:csharp;"> | ||
| + | interface IBookParser{ | ||
| + | int getNumPages(); | ||
| + | } | ||
| + | </pre> | ||
| + | === Concrete === | ||
| + | <pre class="brush:csharp;"> | ||
| + | class BookParser: IBookParser{ | ||
| + | public BookParser(string book){ | ||
| + | // expensive parsing | ||
| + | } | ||
| + | public int getNumPages(){ | ||
| + | ... | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | === Concrete Proxy === | ||
| + | <pre class="brush:csharp;"> | ||
| + | public class LazyBookParserProxy : IBookParser{ | ||
| + | private string _book; | ||
| + | public LazyBookParserProxy(string book){ | ||
| + | _book = book; | ||
| + | } | ||
| + | private BookParser parser = null; | ||
| + | public int getNumPages(){ | ||
| + | if(this.parser == null) | ||
| + | { | ||
| + | this.parser = new BookParser(_book) | ||
| + | } | ||
| + | return this.parser.getNumPages(); | ||
| + | } | ||
| + | </pre> | ||
Latest revision as of 20:11, 28 January 2019
Contents
Video
https://www.youtube.com/watch?v=NwaabHqPHeM
Definition
In proxy pattern, a class represents functionality of another class. This type of design pattern comes under structural pattern.
In proxy pattern, we create object having original object to interface its functionality to outer world.
Proxy Types
- Remote
- Virtual : instead of calling an expensive resource, we call proxy and proxy caches the resources and returns a cheap result.
- Protection
Implementation
Interface
interface IBookParser{
int getNumPages();
}
Concrete
class BookParser: IBookParser{
public BookParser(string book){
// expensive parsing
}
public int getNumPages(){
...
}
}
Concrete Proxy
public class LazyBookParserProxy : IBookParser{
private string _book;
public LazyBookParserProxy(string book){
_book = book;
}
private BookParser parser = null;
public int getNumPages(){
if(this.parser == null)
{
this.parser = new BookParser(_book)
}
return this.parser.getNumPages();
}
