zoukankan      html  css  js  c++  java
  • 装饰模式

    本文章,摘抄自:2018黑马程序最新面试题汇总

    顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例。

    1 public interface ISourceable {
    2 
    3     public void method();
    4 }
    1 public class Source implements ISourceable {
    2 
    3     @Override
    4     public void method() {
    5         System.out.println("the original method");
    6     }
    7 
    8 }
     1 public class Decorator implements ISourceable {
     2 
     3     private ISourceable source;
     4 
     5     public Decorator(ISourceable source) {
     6         super();
     7         this.source = source;
     8     }
     9 
    10     @Override
    11     public void method() {
    12         System.out.println("before decorator");
    13         source.method();
    14         System.out.println("after decorator");
    15     }
    16 
    17 }

    测试方法:

    1 @Test
    2     public void test() {
    3         ISourceable sourceable = new Source();
    4         ISourceable obj = new Decorator(sourceable);
    5         obj.method();
    6     }
  • 相关阅读:
    Yii AR Model 查询
    学习进度4
    学习进度三
    个人每日总结7
    个人每日总结6
    个人每日总结5
    个人每日总结4
    个人冲刺承担的任务项目的用户模板和用户场景模板
    个人每日总结3
    个人每日总结2
  • 原文地址:https://www.cnblogs.com/ffeiyang/p/9542792.html
Copyright © 2011-2022 走看看