Jun
5
装饰(Decorator)模式:也叫包装模式,Adaptor模式也可以叫包装模式,不过有本质的区别,Adaptor模式没有使原有功能增强
Decorator模式使原有功能增强,而且不是通过子类来实现功能增强的。
装饰模式主要特色:
1.用来扩展特定对象的功能,不是扩充某个类的功能。
2.不需要子类,防止由于子类而导致的复杂和混乱
3.对于一个给定的对象,同事可能有不同的装饰对象,客户端可以通过它的需要选择合适的装饰对象
java.io包下类大量使用到装饰模式。
装饰模式主要角色
1.抽象构件角色:给出一个抽象接口,以规范准备接受附加责任的对象
2.具体构件角色:定义一个将要接收附加责任的类
3.装饰角色:持有一个构件对象的实例,并定义一个与抽象构件接口一致的接口
4.具体装饰角色:负责给构件对象"贴上"附加的责任
Java代码
Decorator模式使原有功能增强,而且不是通过子类来实现功能增强的。
装饰模式主要特色:
1.用来扩展特定对象的功能,不是扩充某个类的功能。
2.不需要子类,防止由于子类而导致的复杂和混乱
3.对于一个给定的对象,同事可能有不同的装饰对象,客户端可以通过它的需要选择合适的装饰对象
java.io包下类大量使用到装饰模式。
装饰模式主要角色
1.抽象构件角色:给出一个抽象接口,以规范准备接受附加责任的对象
2.具体构件角色:定义一个将要接收附加责任的类
3.装饰角色:持有一个构件对象的实例,并定义一个与抽象构件接口一致的接口
4.具体装饰角色:负责给构件对象"贴上"附加的责任
Java代码
package com.pattern.decorator;
/**
* 抽象构件角色
*/
public interface Component {
public void doSomething();
}
/**
* 抽象构件角色
*/
public interface Component {
public void doSomething();
}
package com.pattern.decorator;
/**
* 抽象构件角色
*/
public interface Component {
public void doSomething();
}
/**
* 抽象构件角色
*/
public interface Component {
public void doSomething();
}
package com.pattern.decorator;
/**
* 具体构件角色
*/
public class ConcreteComponent implements Component {
public void doSomething() {
System.out.println("功能A");
}
}
/**
* 具体构件角色
*/
public class ConcreteComponent implements Component {
public void doSomething() {
System.out.println("功能A");
}
}
package com.pattern.decorator;
/**
* 具体构件角色
*/
public class ConcreteComponent implements Component {
public void doSomething() {
System.out.println("功能A");
}
}
/**
* 具体构件角色
*/
public class ConcreteComponent implements Component {
public void doSomething() {
System.out.println("功能A");
}
}
package com.pattern.decorator;
/**
* 装饰角色
*/
public class Decorator implements Component {
//拥有一个抽象构件对象
private Component component;
public Decorator(Component component){
this.component = component;
}
public void doSomething() {
component.doSomething();
}
}
/**
* 装饰角色
*/
public class Decorator implements Component {
//拥有一个抽象构件对象
private Component component;
public Decorator(Component component){
this.component = component;
}
public void doSomething() {
component.doSomething();
}
}
package com.pattern.decorator;
/**
* 装饰角色
*/
public class Decorator implements Component {
//拥有一个抽象构件对象
private Component component;
public Decorator(Component component){
this.component = component;
}
public void doSomething() {
component.doSomething();
}
}
/**
* 装饰角色
*/
public class Decorator implements Component {
//拥有一个抽象构件对象
private Component component;
public Decorator(Component component){
this.component = component;
}
public void doSomething() {
component.doSomething();
}
}
package com.pattern.decorator;
/**
* 具体装饰角色一
*/
public class ConcreteDecorator1 extends Decorator {
public ConcreteDecorator1(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能B");
}
}
/**
* 具体装饰角色一
*/
public class ConcreteDecorator1 extends Decorator {
public ConcreteDecorator1(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能B");
}
}
package com.pattern.decorator;
/**
* 具体装饰角色一
*/
public class ConcreteDecorator1 extends Decorator {
public ConcreteDecorator1(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能B");
}
}
/**
* 具体装饰角色一
*/
public class ConcreteDecorator1 extends Decorator {
public ConcreteDecorator1(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能B");
}
}
package com.pattern.decorator;
/**
* 具体装饰角色二
*/
public class ConcreteDecorator2 extends Decorator {
public ConcreteDecorator2(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能C");
}
}
/**
* 具体装饰角色二
*/
public class ConcreteDecorator2 extends Decorator {
public ConcreteDecorator2(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能C");
}
}
package com.pattern.decorator;
/**
* 具体装饰角色二
*/
public class ConcreteDecorator2 extends Decorator {
public ConcreteDecorator2(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能C");
}
}
/**
* 具体装饰角色二
*/
public class ConcreteDecorator2 extends Decorator {
public ConcreteDecorator2(Component component){
super(component);
}
public void doSomething(){
super.doSomething();
this.doAnotherthing();
}
private void doAnotherthing(){
System.out.println("功能C");
}
}
package com.pattern.decorator;
/**
* 客户端
*/
public class Client {
public static void main(String[] args){
//扩充了component 对象的功能。
Component component = new ConcreteDecorator2(new ConcreteDecorator1(new ConcreteComponent()));
component.doSomething();
}
}
/**
* 客户端
*/
public class Client {
public static void main(String[] args){
//扩充了component 对象的功能。
Component component = new ConcreteDecorator2(new ConcreteDecorator1(new ConcreteComponent()));
component.doSomething();
}
}
适配器(Adapter)模式(包装模式)


