设计模式-结构型模式-装饰

结构型模式:结构型对象模式不是对接口或实现进行组合的.而是描述了如何对一些对象进行组合,从而实现新功能的一些方法.
decorator:动态的给一个对象添加一些额外的职责.

package structure.decorator;
/**
 *  The top abstract where concrete component and decorator
 *  should be derived from
 */
public interface Component  {
    public abstract void PrintString(String s);
}

 

package structure.decorator;
/**
 *  A Concrete Component
 */
import java.io.*;

public class ConcreteComponent implements Component {
    public ConcreteComponent() {
    }
    public void PrintString(String s) {
        System.out.println("Input String is:" + s);
    }
}

 

package structure.decorator;
/**
 *  The Concrete Decorator
 */
import java.io.*;

public class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component c) {
        super(c);
    }
    public void PrintString(String s) {
        super.PrintString(s);
        PrintStringLen(s);
    }
    public void PrintStringLen(String s) {
        System.out.println("The length of string is:" + s.length());
    }
}

 

package structure.decorator;
/**
 *  The Decorator
 */
public class Decorator implements Component {
    private Component component;
    public Decorator(Component c) {
        component = c;
    }
    public void PrintString(String s) {
        component.PrintString(s);
    }
}

 

package structure.decorator;
/**
 *  A simple test
 */
public class Test1  {
    public static void main(String[] args) {
        Component myComponent = new ConcreteComponent();
        myComponent.PrintString("A test String");
        Decorator myDecorator = new ConcreteDecoratorA(myComponent);
        myDecorator.PrintString("A test String");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值