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();
    }
    }

  • 相关阅读:
    ionic + cordova+angularJs 搭建的H5 App完整版总结
    在DevExpress程序中使用GridView直接录入数据的时候,增加列表选择的功能
    【Web动画】SVG 线条动画入门
    闲来无聊,研究一下Web服务器 的源程序
    PHP实现RTX发送消息提醒
    关于AngularJS(1)
    项目总结12:bootstrap-select下拉框模糊搜索
    JAVA读取XML文件并解析获取元素、属性值、子元素信息
    项目总结11:Centos部署JDK+Tomcat+MySQL文档(阿里云-网易云-华为云)
    项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题
  • 原文地址:https://www.cnblogs.com/zh-0802/p/6179962.html
Copyright © 2011-2022 走看看