设计模式(8)--Decorator--装饰器模式--结构型
1.模式定义:
2.模式特点:
3.使用场景:
4.模式实现:

(1)抽象构件(Component)角色:
public interface Component { 
  public void sampleOperation(); 
}
(2)具体构件(ConcreteComponent)角色:
public class ConcreteComponent implements Component { 
  @Override 
  public void sampleOperation() { 
    // 写相关的业务代码 
  } 
}
(3)装饰(Decorator)角色:
public class Decorator implements Component{
    private Component component;
    public Decorator(Component component){
        this.component = component;
    }
    @Override
    public void sampleOperation() {
        // 委派给构件
        component.sampleOperation();
    }
}
(4)具体装饰(ConcreteDecorator)角色:
public class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }
    @Override
    public void sampleOperation() {
     super.sampleOperation();
        // 写相关的业务代码
    }
}
public class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }
    @Override
    public void sampleOperation() {
      super.sampleOperation();
        // 写相关的业务代码
    }
}
5.优缺点:
(1)装饰器优点:
(2)装饰器缺点:
6.注意事项
7.应用实例

(1)抽象构件角色“齐天大圣”接口定义了一个move()方法,这是所有的具体构件类和装饰类必须实现的。
//大圣的尊号
public interface TheGreatestSage {
public void move();
}
(2)具体构件角色“大圣本尊”猢狲类
public class Monkey implements TheGreatestSage {
    @Override
    public void move() {
        //代码
        System.out.println("Monkey Move");
    }
}
(3)抽象装饰角色“七十二变”
public class Change implements TheGreatestSage {
    private TheGreatestSage sage;
    public Change(TheGreatestSage sage){
        this.sage = sage;
    }
    @Override
    public void move() {
        // 代码
        sage.move();
    }
}
(4)具体装饰角色“鱼儿”
public class Fish extends Change {
    public Fish(TheGreatestSage sage) {
        super(sage);
    }
    @Override
    public void move() {
        // 代码
        System.out.println("Fish Move");
    }
}
(5)具体装饰角色“鸟儿”
public class Bird extends Change {
    public Bird(TheGreatestSage sage) {
        super(sage);
    }
    @Override
    public void move() {
        // 代码
        System.out.println("Bird Move");
    }
}
(6)客户端类
public class Client {
    public static void main(String[] args) {
        TheGreatestSage sage = new Monkey();
        // 第一种写法
        TheGreatestSage bird = new Bird(sage);
        TheGreatestSage fish = new Fish(bird);
        // 第二种写法
        //TheGreatestSage fish = new Fish(new Bird(sage));
        fish.move();
    }
}



TheGreatestSage sage = new Monkey();
TheGreatestSage bird = new Bird(sage);
Monkey sage = new Monkey();
Bird bird = new Bird(sage);
TheGreatestSage sage = new Monkey();
Bird bird = new Bird(sage);
bird.fly();


public abstract class InputStream implements Closeable {
    public abstract int read() throws IOException;
    public int read(byte b[]) throws IOException {}
    public int read(byte b[], int off, int len) throws IOException {}
    public long skip(long n) throws IOException {}
    public int available() throws IOException {}
    public void close() throws IOException {}
    public synchronized void mark(int readlimit) {}
    public synchronized void reset() throws IOException {}
    public boolean markSupported() {}
}
public class FilterInputStream extends InputStream {
    protected FilterInputStream(InputStream in) {}
    public int read() throws IOException {}
    public int read(byte b[]) throws IOException {}
    public int read(byte b[], int off, int len) throws IOException {}
    public long skip(long n) throws IOException {}
    public int available() throws IOException {}
    public void close() throws IOException {}
    public synchronized void mark(int readlimit) {}
    public synchronized void reset() throws IOException {}
    public boolean markSupported() {}
}
public class PushbackInputStream extends FilterInputStream {
    private void ensureOpen() throws IOException {}
    public PushbackInputStream(InputStream in, int size) {}
    public PushbackInputStream(InputStream in) {}
    public int read() throws IOException {}
    public int read(byte[] b, int off, int len) throws IOException {}
    public void unread(int b) throws IOException {}
    public void unread(byte[] b, int off, int len) throws IOException {}
    public void unread(byte[] b) throws IOException {}
    public int available() throws IOException {}
    public long skip(long n) throws IOException {}
    public boolean markSupported() {}
    public synchronized void mark(int readlimit) {}
    public synchronized void reset() throws IOException {}
    public synchronized void close() throws IOException {}
}
public class IOTest {
    public static void main(String[] args) throws IOException {
        // 流式读取文件
        DataInputStream dis = null;
        try{
            dis = new DataInputStream(
                    new BufferedInputStream(
                            new FileInputStream("test.txt")
                    )
            );
            //读取文件内容
            byte[] bs = new byte[dis.available()];
            dis.read(bs);
            String content = new String(bs);
            System.out.println(content);
        }finally{
            dis.close();
        }
    }
}
设计模式(8)--Decorator--装饰器模式--结构型的更多相关文章
- 设计模式(十):Decorator装饰者模式 -- 结构型模式
		
1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法,或者须要给方法添加更多的功能(魅力),你也许会仅仅继 ...
 - Decorator装饰者模式(结构型模式)
		
1.需求 假设让我们去设计FCL中的Stream类,该类具有流类的基本功能,除了有各种不同类型的流外(如内存流.文件流.网络流等等),但是在不同的业务场景下,如处理银行业务,需要给相关的内存流进行加密 ...
 - 12、Decorator 装饰器 模式 装饰起来美美哒 结构型设计模式
		
1.Decorator模式 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式(Decorator Pattern)允许向一个现 ...
 - 设计模式学习心得<装饰器模式 Decorator>
		
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包装 ...
 - 设计模式入门之装饰器模式Decorator
		
//装饰模式定义:动态地给一个对象加入一些额外的职责. //就添加功能来说.装饰模式比生成子类更为灵活 //这也提现了面向对象设计中的一条基本原则,即:尽量使用对象组合,而不是对象继承 //Compo ...
 - 设计模式のDecoratorPattern(装饰器模式)----结构模式
		
一.产生背景 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...
 - 重学 Java 设计模式:实战装饰器模式(SSO单点登录功能扩展,增加拦截用户访问方法范围场景)
		
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 对于代码你有编程感觉吗 很多人写代码往往是没有编程感觉的,也就是除了可以把功能按照固 ...
 - Java设计模式12:装饰器模式
		
装饰器模式 装饰器模式又称为包装(Wrapper)模式.装饰器模式以多客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式的结构 通常给对象添加功能,要么直接修改对象添加相应的功能, ...
 - Decorator(装饰器模式)
		
装饰器模式允许我们根据运行时不同的情景动态地为某个对象调用前后添加不同的行为动作. <?php class HtmlTemplate { // any parent class methods ...
 
随机推荐
- Javasript 正则匹配任意字符
			
今天在写Js匹配任意字符的时候发现使用.不好使,当输入内容有回车的时候就会失效. 后来上网查,发现js的任意字符不包括\n. 本来想写一个trim后长度为10的正则验证,最后使用[\s|\S]来代替任 ...
 - [CF337D]邪恶古籍-树状dp
			
Problem 邪恶古籍 题目大意 给出一些关键点,求这棵树上到最远关键点距离小于等于d的有多少个. Solution 一个非常简单的树形dp.然而我被这道题给玩坏了. 在经过分析以后,我们发现只需要 ...
 - 由max_allowed_packet引发的mysql攻防大战
			
1.原因 程序的sql语句比较长.max_allowed_packet默认是1024.于是就报错了.一开始手动改 global max_allowed_packet ,改完后.莫名奇妙被还原.后来改配 ...
 - 学习笔记TF030:实现AlexNet
			
ILSVRC(ImageNet Large Scale Visual Recognition Challenge)分类比赛.AlexNet 2012年冠军(top-5错误率16.4%,额外数据15.3 ...
 - Linux通过shell执行自动化部署
			
背景 通过shell判断是否存在补丁更新,进行自动化的部署 代码 #!/bin/sh #Edit:何彦霆 #version: beta #执行环境初始化 source /hxspace/product ...
 - (转)Centos搭建FTP服务器
			
场景:ftp服务器对于在Linux服务器上进行文件操作太方便,在安装软件时候,大的软件也可以先上传再进行安装! 1 搭建FTP服务器 1.1 检查vsftpd 查看是否已经安装vsftpd rpm - ...
 - if else 和switch case以及continue,break的区别
			
1,if 经常用于做区间判断 或者 固定值: break和continue的使用 break:用来结束循环结构或者switch case continue:结束此次循环进入 ...
 - 深入浅出数据结构C语言班(11)——简要介绍算法时间复杂度
			
在接下来的数据结构博文中,我们将会开始接触到一些算法,也就是"解决某个问题的方法",而解决同一个问题总是会存在不同的算法,所以我们需要在不同的算法之中做出抉择,而做出抉择的根据往往 ...
 - Python中的可变对象和不可变对象
			
Python中的可变对象和不可变对象 什么是可变/不可变对象 不可变对象,该对象所指向的内存中的值不能被改变.当改变某个变量时候,由于其所指的值不能被改变,相当于把原来的值复制一份后再改变,这会开辟一 ...
 - Qt5.5.1和Qt5.3.2编译OCI驱动教程及验证方法
			
我们都知道oracle数据库的强大,并且好多企业或者教学用到数据库时都会推荐使用.但是Qt因为版权问题没有封装oracle数据库专用驱动,网上也有一大堆说法和教程,但是或多或少的都有问题.下面废话不多 ...