代理模式和装饰器模式的区别

1:代理模式:
为其它对象提供一种代理以控制对目标对象的访问。即第三方对象通过访问代理对象而达到访问目标对象之目的,与此同时,代理对象在访问目标对象前后加入特定的逻辑以实现功能的扩展。
以静态代理为例:package static_proxy;

public interface Movable {
public void move();

}
package static_proxy;

import java.util.Random;

public class Tank implements Movable {
@Override
public void move() {
System.out.println(" Tank is moving ");
try {
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

package static_proxy;

public class TankLogProxy implements Movable {
private Movable obj;
public TankLogProxy(Movable obj) {
super();
this.obj = obj;
}
@Override
public void move() {
System.out.println("the log is begin ");
obj.move();
System.out.println("the log is end ");
}
}

package static_proxy;

public class TankTimeProxy implements Movable {
private Movable obj;
public TankTimeProxy(Movable obj) {
super();
this.obj = obj;
}
@Override
public void move() {
long begintime = System.currentTimeMillis();
System.out.println(" Tank is begining to move !");
obj.move();
long endtime = System.currentTimeMillis();
System.out.println(" Tank is stop !");
System.out.println("move time : "+(endtime-begintime));
}
}

package static_proxy;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
Movable tankTime = new TankTimeProxy(new Tank());
Movable tankLog = new TankLogProxy(tankTime);
tankLog.move();
}
}

代理模式:目标对象和代理对象实现同一接口;代理对象访问目标对象时在其前后加入一定的逻辑实现功能扩展;可多次代理。

2.装饰者模式:

动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更加灵活。


package decorator;

public abstract class car_parent {
// 汽车抽象父类
private String make_address;
private int speed;

public String getMake_address() {
return make_address;
}

public void setMake_address(String make_address) {
this.make_address = make_address;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public abstract void print_face();

}


package decorator;

public abstract class decorator_parent extends car_parent {
// 装饰者父类
protected car_parent car_parent_ref;

public decorator_parent(car_parent carParentRef) {
super();
car_parent_ref = carParentRef;
}
@Override
public void print_face() {
car_parent_ref.print_face();
}

}

package decorator;
public class decorator_audi_red extends decorator_parent {
public decorator_audi_red(car_parent carParentRef) {
super(carParentRef);
}
@Override
public void print_face() {
super.print_face();
System.out.println("给 奥迪 喷涂鸦 - 颜色为 红色火焰");
}
}

package decorator;
public class decorator_audi_purple extends decorator_parent {

public decorator_audi_purple(car_parent carParentRef) {
super(carParentRef);
}
@Override
public void print_face() {
super.print_face();
System.out.println("给 奥迪 喷涂鸦 - 颜色为 紫色霞光");
}
}

package decorator;

public class main_run {
public static void main(String[] args) {

car_parent audi_sub_ref = new audi_sub();
audi_sub_ref.setMake_address("北京市朝阳区");
audi_sub_ref.setSpeed(200);

decorator_audi_red decorator_audi_red_ref = new decorator_audi_red(audi_sub_ref);

decorator_audi_purple decorator_audi_purple_ref = new decorator_audi_purple(decorator_audi_red_ref);
decorator_audi_purple_ref.print_face();
}
}

装饰者模式:装饰者和被装饰者应继承或实现同一父类或接口,以表示它们为同一类对象(非必须);装饰者对象对被装饰者对象增加一定的职责;可多次装饰。

可以发现:代理模式和装饰者模式上在语法形式上几乎完全一样,那么它们的区别在哪里呢?
装饰者模式:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更加灵活
代理模式:为其它对象提供一种代理以控制对这个对象的访问。


其实,它们的着重点一个在于“增加”职责,另一个在于“控制”访问。这是它们最本质的区别。

由此可以看到:学习设计模式重点在于“语义”上把握,而不是追求它的“形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值