Home >Java >javaTutorial >Examples of proxy pattern and decoration pattern in Java
This article mainly introduces the proxy mode and decoration mode of Java design patterns. It analyzes the concepts, functions, Java implementation methods and related usage precautions of the proxy mode and decoration mode with specific examples. Friends in need can refer to the following
The examples in this article describe the proxy mode and decoration mode of Java design patterns. Share it with everyone for your reference, the details are as follows:
The reason why I put these two models together is because I found that these two models are almost identical!
I also searched some information from the Internet and found that there are some differences between the two. We also clarified this confusion while studying.
Definition:
Proxy mode provides a proxy for other objects to control access to this object.
Decoration mode dynamically adds some additional responsibilities to an object.
The proxy mode is easy to understand. It is to encapsulate an object again, and then access the encapsulated object.
Because the proxy object has replaced the proxy object.
Decoration mode adds functions to an object. The image is that of standing aside as a waiter to provide services.
So the fundamental difference is that the decoration mode object is still there, not replacing the original object, but providing services on the side.
The opinions expressed in this article are personal understandings and I am only responsible for them.
Examples are given below:
package test.design.proxydecorator; /** * 接口定义行为:吃饭 * @author lushuaiyin * */ public interface Eatable { void eatFood(); }
package test.design.proxydecorator; /** * 实现类 * @author lushuaiyin * */ public class Man implements Eatable{ @Override public void eatFood() { System.out.println("I am eating food! Happy!"); } }
The above are common interfaces and implementations, and the following are pattern examples
Agent Mode
##
package test.design.proxydecorator; /** * 代理模式 * 代理对象把被代理对象封装,像一个包装盒把被代理对象包裹起来。 * 它不改变被代理对象的原有逻辑,只是增加来了一些自己的服务,像一个代理一样。 * 代理从使用上给调用者的感觉就是你已经取代了原来的对象。 * 就像皇太后取代小皇帝实行垂帘听政。 * @author Administrator * */ public class ProxyMan implements Eatable{ private Eatable eatable; public ProxyMan(){ System.out.println("proxy: I am proxy object. I will help you create a object of Man that wnt to be proxyed"); this.eatable=new Man(); //注意代理对象把被代理对象封装,在内部有实际的被代理对象,这个调用者是不知道的。 } @Override public void eatFood() { //代理对象把被代理对象的功能封装,蛋不改变其内部逻辑,只是增加一些服务。 System.out.println("proxy: I know you are hungry,so I cook for you some food."); this.eatable.eatFood(); System.out.println("proxy: Now you eat up food. Let me help you clean the dishes."); } }
Decorator Mode
package test.design.proxydecorator; /** * 装饰的抽象. * 也可以不用抽象类,简单的父类也可以,只要实现装饰功能. * 抽象只是为了增加一层封装,规定装饰者必有的装饰功能而已。 * @author lushuaiyin * */ public abstract class DecoratorMan { protected Eatable eatable; //使用构造函数或者set方法(或其他方法)把接口对象传入。 //注意,必须保证这个对象的传入,最好还是用构造函数。 public DecoratorMan(Eatable eatable){ this.eatable=eatable; } public void eatFood(){ this.eatable.eatFood(); }; }
package test.design.proxydecorator; /** * 装饰模式 * 装饰对象时独立于原来的对象的。它和被装饰的对象有关系,但是是独立的对象。 * 装饰对象更像一个酒店服务生,为别人提供服务,所以他还是他自己,他有自己的实际存在。 * @author lushuaiyin * */ public class Decorator extends DecoratorMan{ public Decorator(Eatable eatable) { super(eatable); } //重写父类方法 public void eatFood(){ decoratorServiceCookFood();//装饰的具体行为 super.eatable.eatFood(); decoratorServiceCleanDishes();//装饰的具体行为 }; public void decoratorServiceCookFood(){ System.out.println("Decorator: I know you are hungry,so I cook for you some food."); } public void decoratorServiceCleanDishes(){ System.out.println("Decorator: Now you eat up food. Let me help you clean the dishes."); } }The most critical thing is the call, which is also the main difference between the two modes!
package test.design.proxydecorator; public class TestMain { /** * @param args */ public static void main(String[] args) { //代理模式 System.out.println("代理模式:"); Man m1=new Man();//通常情况下 m1.eatFood(); System.out.println("---------------------------"); //代理模式者直接取代某对象,你连你想要见的人的面都见不到。 //它说你要见的人已经把所有事委托于我,他会的我会;他不会的,我也会。我就是他的替代增强版。 Eatable e1=new ProxyMan(); e1.eatFood(); System.out.println("------------分割---------------"); System.out.println("装饰模式:"); Man m2=new Man();//通常情况下 m2.eatFood(); System.out.println("---------------------------"); //装饰模式者站在一边提供各种服务. //装饰者和被装饰者都在场,装饰者提供服务,赚取小费。 Decorator d1=new Decorator(m2); d1.eatFood(); } }Print:
代理模式: I am eating food! Happy! --------------------------- proxy: I am proxy object. I will help you create a object of Man that wnt to be proxyed proxy: I know you are hungry,so I cook for you some food. I am eating food! Happy! proxy: Now you eat up food. Let me help you clean the dishes. ------------分割--------------- 装饰模式: I am eating food! Happy! --------------------------- Decorator: I know you are hungry,so I cook for you some food. I am eating food! Happy! Decorator: Now you eat up food. Let me help you clean the dishes.
The above is the detailed content of Examples of proxy pattern and decoration pattern in Java. For more information, please follow other related articles on the PHP Chinese website!