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

    package aaa;

    public interface Human {
    public void wearClothes();

    public void walkToWhere();
    }

    //定义装饰者
    public abstract class Decorator implements Human {
    private Human human;

    public Decorator(Human human) {
    this.human = human;
    }

    public void wearClothes() {
    human.wearClothes();
    }

    public void walkToWhere() {
    human.walkToWhere();
    }
    }

    //下面定义三种装饰,这是第一个,第二个第三个功能依次细化,即装饰者的功能越来越多
    public class Decorator_zero extends Decorator {

    public Decorator_zero(Human human) {
    super(human);
    }

    public void goHome() {
    System.out.println("进房子。。");
    }

    public void findMap() {
    System.out.println("书房找找Map。。");
    }

    @Override
    public void wearClothes() {
    // TODO Auto-generated method stub
    super.wearClothes();
    goHome();
    }

    @Override
    public void walkToWhere() {
    // TODO Auto-generated method stub
    super.walkToWhere();
    findMap();
    }
    }

    public class Decorator_first extends Decorator {

    public Decorator_first(Human human) {
    super(human);
    }

    public void goClothespress() {
    System.out.println("去衣柜找找看。。");
    }

    public void findPlaceOnMap() {
    System.out.println("在Map上找找。。");
    }

    @Override
    public void wearClothes() {
    // TODO Auto-generated method stub
    super.wearClothes();
    goClothespress();
    }

    @Override
    public void walkToWhere() {
    // TODO Auto-generated method stub
    super.walkToWhere();
    findPlaceOnMap();
    }
    }

    public class Decorator_two extends Decorator {

    public Decorator_two(Human human) {
    super(human);
    }

    public void findClothes() {
    System.out.println("找到一件D&G。。");
    }

    public void findTheTarget() {
    System.out.println("在Map上找到神秘花园和城堡。。");
    }

    @Override
    public void wearClothes() {
    // TODO Auto-generated method stub
    super.wearClothes();
    findClothes();
    }

    @Override
    public void walkToWhere() {
    // TODO Auto-generated method stub
    super.walkToWhere();
    findTheTarget();
    }
    }

    //定义被装饰者,被装饰者初始状态有些自己的装饰
    public class Person implements Human {

    @Override
    public void wearClothes() {
    // TODO Auto-generated method stub
    System.out.println("穿什么呢。。");
    }

    @Override
    public void walkToWhere() {
    // TODO Auto-generated method stub
    System.out.println("去哪里呢。。");
    }
    }
    //测试类,看一下你就会发现,跟java的I/O操作有多么相似
    public class Test {
    public static void main(String[] args) {
    Human person = new Person();
    Decorator decorator = new Decorator_two(new Decorator_first(
    new Decorator_zero(person)));
    decorator.wearClothes();
    decorator.walkToWhere();
    }
    }

  • 相关阅读:
    python列表(包含列表方法)
    python数据类型和运算符
    python计算机初识
    python运行过程,变量,符号
    python·if语句
    python`while循环
    <select></select> php表单怎么传值
    thinkPhp3 空操作
    thinkphp3.1和3.2的<模板替换>的区别
    thinkphp3 空操作 如果为空会怎么样 empty name=""
  • 原文地址:https://www.cnblogs.com/zh-0802/p/6179962.html
Copyright © 2011-2022 走看看