Comparison of related modes
The Adapter and Proxy models have established a "thin layer" around the object. However, Adapter provides a different interface for the object, but Proxy is the same interface, but it is only postponed in the process or data transmission.
The Decorator mode is still the same interface to which the corresponding object is provided, but its purpose is to provide additional functions for the original object (usually virtual functions). Instead, the Proxy mode is the control of the access to the included object.
Adapter mode
As the name suggests, Adapter mode is used to convert a class program interface to another class. How to convert it? You can write a class with a desirable interface and use it to communicate with the class of the different interfaces. At any time, you can use this mode as long as we want to work with unrelated classes. There are two ways to implement it: inheritance, or combination. When using inheritance, we derive a new class from the less suitable class, then add the appropriate method to the new class to match the interface. When using a combination, we include the original class in the new class, and add a method to pass the corresponding interface call. These two methods are also called Class Adapters and Object Adapters, which are more likely to understand and implement. Example: This is a lot of examples. For example, in a student status management system, we have to showcase information, and this information may be in several places in different formats, such as XML, some are array, etc., and the order of information storage is different. For example, some students have names, they are vice versa (assuming, actually unlikely), and we hope to display with fixed format, this time you can use Adapter mode. The Proxy mode Proxy mode is suitable for when you want to display a complex, time-consuming object as a simple object. What does that mean? If the object you want to build is very time-consuming and consumes computer resources, use the Proxy mode, you can postpone the object's implementation until you really need this object. A proxy usually has the same way as the object of its agent, and it passes the call to the actual object when it actually needs. In the following cases, you can use the Proxy mode: 1, an object takes a long time to load, such as a big picture; 2, some of the results to spend a lot of time to calculate, and you need to show a time Temporary results; 3, the object is on the remote machine, load it very slow through the network, especially in the peak period of the network; 4. Object Access has permission restrictions, Proxy can verify the user's permissions. Example: For example, we have to display pictures and text on the window, and they are typically typed in a certain format. And the loading of the picture is often slow, and we don't want to affect the layout before the picture is successful. At this time we can use Proxy, create an IMGPROXY class, the window does not operate to the image object, turn to this class interaction, we can set a clock in this class, return a simple but equivalent image when just called, Only for a while, the actual picture is successfully loaded after successful.
Code is as follows: public class ImageProxy {private bool done; private Timer timer; // ----- public ImageProxy () {// create a timer thread and start it timer = new Timer (new TimerCallback (timerCall), this, 5000 , 0);} // ----- // Called When Timer Completes Private Void TimerCall (Object Obj) {DONE = true; timer.dispose ();} // ----- public image getImage () { Imager img; if (done) img = new femAlimage (); else img = new quickimage (); returnim.getImage ();}} Effect is like: Bridge mode looks like it seems that Bridge mode and Adapter model may be similar, because They all convert an interface to another. However, the meaning of Adapter mode is to make one or more interfaces look like the class we expect; and Bridge mode is separated by the interface and its implementation so that you can easily modify its implementation without modifying Interface code. In Bridge mode, it is necessary to use the "abstract" concept. It is used to define the interface of the interface, and the abstract implementation is an extension and implementation interface; this example is very common, a classic example is the same data Different display: histogram, pie chart, etc. (...)