JAVA design mode for cainiao-adapter mode, decoration mode, and proxy Mode
Before the festival, I shared my learning experience on the three design modes, namely the adapter mode, the decoration mode, and the proxy mode.
However, it seems that the more you write, the more confused you are. These three modes all have some similarities and are always confused.
I will study again today and share my learning experience with you. This is a combination of actual projects in my work.
The design pattern is actually a solution. JAVA, as an object-oriented language, always implements interfaces and inherits abstract classes for all design patterns, sometimes it seems that the interface is written to implement the interface. Therefore, in the instruction code of the three modes, we have defined the interfaces, so we can get confused.
Let's take a long time to introduce the three design patterns.
In the adapter mode, an adapter allows a class that cannot work together because the interface is not compatible. The method is to wrap its own interface in an existing class.
The decoration device mode, the original can not meet the existing needs, enhance the original.
Proxy mode. The method of the other class is called for the same class, and direct operation is not performed for this method.
The adapter features compatibility. In terms of code, the adapter class has the same interface as the original class and holds a new target object.
Just like an adapter with three-hole to two-hole, it has a three-hole plug that can be inserted into the three-hole socket, and the two-hole socket can be inserted into the two-hole plug.
The adapter mode is to transform the original 3 holes.
When using the adapter mode, we must simultaneously hold the original object, adaptive object, and target object ....
The ornament mode is characterized by enhancement. It is characterized by the fact that the decoration class and all the decoration classes must implement the same interface, and they must hold the decorated objects and can be decorated infinitely.
Proxy mode is characterized by isolation, isolating the relationship between the call class and the called class, and calling through a proxy class.
In general, there are three sentences:
1. the adapter mode converts a class (a) to another class (B) in some way ).
2. The decoration mode adds some new functions to another class (B) based on the original class ().
3. the proxy mode converts a class (a) to a specific operation class (B ).
Let's talk a little bit about Lori. Then we are happy to come to the example time. This is the actual example at work.
The company has an ORDER system dedicated to providing ORDER management interfaces for O2O Mall, WAP Mall, mobile APP, and other client calls.
When the ORDER system is launched, it already contains complete data operations.
The current mobile APP needs to be upgraded, and the old interface may not be able to meet the requirements. For example, the old O2O order delivery interface must provide the deadline and order number, and the new APP may not provide the deadline, you only need to provide the order number to extend the time.
The old interface cannot be modified, because the old order interface is for all platforms, the new interface requirements must be adapted to the original interface...
Solution: add the AppAdepter class to adapt to the old interface and develop it to the new interface ....
First, the original interface and implementation class are provided.
Original interface:
/*** Original interface, required to input orderId, time **/public interface SourceOrderApi {public void updateDate (String orderId, String date, String client );}
Implementation class:
Public class SourceOrderApiImpl implements SourceOrderApi {@ Overridepublic void updateDate (String orderId, String date, String client) {System. out. println (client + "the validity period of the order" + orderId + "has been extended to" + date );}}
In this set of code, the client needs to input orderId, validity period date, and client name to extend the value...
Main method:
public class Test {public static void main(String[] args) {SourceOrderApi sourceOrderApi = new SourceOrderApiImpl();sourceOrderApi.updateDate("123456", "2014-10-15", "user");}}
Running result:
For new interfaces, the client does not have to input the date parameter.
Public interface AppOrderApi {// you only need to input the order Id to public void updateDate (String orderId, String client );}
New Implementation class:
Public class implements AppOrderApi {SourceOrderApi sourceOrderApi; public AppOrderApiImpl () {sourceOrderApi = new SourceOrderApiImpl () ;}@ Overridepublic void updateDate (String orderId, String client) {// The adaptation method is random here, but the original interface sourceOrderApi must be called to ensure full compatibility. updateDate (orderId, "9999-12-31", client );}}
Main method:
public class Test {public static void main(String[] args) {AppOrderApi appOrderApi = new AppOrderApiImpl();appOrderApi.updateDate("123456", "user");}}
Running result:
In this set of code, the new implementation class holds the object of the old interface, that is, the object is new...
Then, perform the adaptation operation in the new method. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature/Signature + 907/Signature/aoaM8L3A + signature/JoaM8L3A + signature/Signature + signature + 3b7fzOW1xM/uxL/Signature + sLroaM8L3A + CjxwPr + signature + G + weight/weight + weight/ro6zL + 8O709C5zLaotcTKtc/WtPrC66OsztK088G/weight + weight/weight + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141015/20141015091547279.jpg" alt = "\">
You only need to convert the original interface to another interface that the customer wants, that is, the adapter mode!
Conversion is nothing more than 1. inherit the original class or implement the original interface 2. Hold the object of the original Interface
Then implement the target interface ....
The first is the adaptation of the class, and the second is the adaptation of the object! That's all ....
Then let's look back at the proxy mode. The code is similar to the adapter mode!
For another example, our company's system needs to be upgraded again. Our original interface has not been added to the security mechanism, which allows anyone to call this interface at will. Now, the company needs to modify this interface, it can only be called by the admin Client. Other users must enter the account and password before calling it.
The original interface and class do not need to be modified. We only need to add a new proxy...
The biggest difference between the proxy mode and the adapter mode is that the proxy mode implements the same interface as the original object, while the adapter class matches the new interface. To put it bluntly, implement a new interface.
Proxy type:
Public class implements SourceOrderApi {SourceOrderApi sourceOrderApiImpl; public implements () {sourceOrderApiImpl = new SourceOrderApiImpl () ;}@ Overridepublic void updateDate (String orderId, String date, String client) {// determine. if it is admin, update it; otherwise, enter the account password if ("admin ". equals (client) {sourceOrderApiImpl. updateDate (orderId, date, client);} else {System. out. println ("the account is not admin and you do not have the query permission. Please enter the admin Operation ");}}}
Main method:
public class Test {public static void main(String[] args) {SourceOrderApi proxySourceOrderApiImpl = new ProxySourceOrderApiImpl();proxySourceOrderApiImpl.updateDate("123456", "2014-10-15", "user");proxySourceOrderApiImpl.updateDate("123456", "2014-10-15", "admin");}}Running result:
The proxy class must implement the original interface and the object holding the original interface to be called the proxy class.
In this way, we do not need to modify the original implementation class and use a proxy class for filtering. Some may be wondering why they should not be directly modified in the original implementation class. Remember the basic principles of the JAVA design pattern and disable the modification internally.
If you don't have the other class, what do you do if someone else just provides you with a JAR package that can be operated? Decompile the JAR package and modify it? If the original interface can be called directly in some places, wouldn't it affect other places if you modify the original implementation class? You cannot modify the original code. This is the basic principle.
Finally, it is the decoration device mode. The decoration device mode is better understood than the above two modes, with the greatest difference.
If this order is extended, it can not only extend the validity period of order delivery, but also extend the validity period of order return.
This is a typical decoration device. You need to enrich the functions of the original interface without modifying the original interface.
Public class NewSourceOrderApiImpl implements SourceOrderApi {SourceOrderApi sourceOrderApi; public NewSourceOrderApiImpl (SourceOrderApi sourceOrderApi) {this. sourceOrderApi = sourceOrderApi;} @ Overridepublic void updateDate (String orderId, String date, String client) {sourceOrderApi. updateDate (orderId, date, client); System. out. println (client + "the refund period for the order" + orderId + "has been extended to" + date );}}Main method:
public class Test {public static void main(String[] args) {SourceOrderApi sourceOrderApi = new NewSourceOrderApiImpl(new SourceOrderApiImpl());sourceOrderApi.updateDate("123456", "2014-10-15", "user");}}Running result:
In the decorator mode, you must have the decorated and decorated classes .. In this set of code, the object of the original SourceOrderApi is the decorated class, and the newly created NewSourceOrderApiImpl is the decoration class. The decoration class must pass the decorated object as a parameter.
This is different from the code in the proxy mode. The proxy mode must own this object and does not need to be passed in from the outside. However, the decoration mode must be passed in from the outside without any order. You can choose to change the order according to the actual needs of the Code, just as you can put cabbage or meatballs in hot pot first.
In terms of usage, the proxy mode focuses on isolation restrictions, so that external users cannot access your actual calling objects, such as permission control. The decoration mode focuses on function expansion, implement more functions in the same method.
I have read a lot of articles about these three models, and I have thoroughly understood them.
Now you can go to the next step to learn...
Please point out any omissions or errors.