12. Proxy mode in Javascript design mode ---- proxy
Understanding and using the design pattern can cultivate our good Object-Oriented Programming habits. At the same time, in practical applications, we can enjoy the pleasure of having the best experience. This section describes the proxy mode in the Javascript design mode.
Concept
Gof: provides a proxy for other objects to control access to this object.
Proxy is a more useful mode, and there are many variants. The application scenario covers the large structure from the small structure to the entire system. Proxy is the meaning of proxy. We may have the concept of proxy server.
The proxy concept can be interpreted as: there is an intermediate layer between the starting point and the destination, meaning the proxy.
Why use proxy?
1. users of different levels of authorization mechanisms have different access rights to the same object. For example, in the jive forum system, proxy is used to control the authorization mechanism. There are two types of users who access the Forum: registered users and tourists (unregistered users), jive uses a proxy like forumproxy to control the access permissions of these two users to the Forum.
2. A client cannot directly operate on an object, but must interact with the object.
Two specific cases are given:
(1) if the object is a large image that takes a long time to be displayed, when the image is included in the document, open the document using the editor or browser, opening a document must be fast and cannot wait until the processing of the large image is completed. In this case, you need to create an image proxy to replace the real image.
(2) if the object is on a remote server on the Internet and it may be slow due to network speed, we can replace the object with a proxy.
In short, the principle is that objects with high overhead are created only when they are used. This principle can save us a lot of valuable Java memory. therefore, some people think that Java consumes resources and memory. I think this is also related to programming ideas.
Proxy mode example
var Publication = new Interface('Publication', ['getIsbn', 'setIsbn', 'getTitle', 'setTitle', 'getAuthor', 'setAuthor', 'display']);var Book = function(isbn, title, author) { //...} // implements Publicationimplements(Book,Publication);/* Library interface. */var Library = new Interface('Library', ['findBooks', 'checkoutBook', 'returnBook']);/* PublicLibrary class. */var PublicLibrary = function(books) { //...};// implements Libraryimplements(PublicLibrary,Library); PublicLibrary.prototype = { findBooks: function(searchString) { //... }, checkoutBook: function(book) { //... }, returnBook: function(book) { //... }};/* PublicLibraryProxy class, a useless proxy. */var PublicLibraryProxy = function(catalog) { this.library = new PublicLibrary(catalog);};// implements Libraryimplements(PublicLibraryProxy,Library);PublicLibraryProxy.prototype = { findBooks: function(searchString) { return this.library.findBooks(searchString); }, checkoutBook: function(book) { return this.library.checkoutBook(book); }, returnBook: function(book) { return this.library.returnBook(book); }};
I will not explain this example. I should be able to understand ho...
Remark: If you are very interested in the Javascript design pattern, Baidu, (^ o ^ )/~