zoukankan      html  css  js  c++  java
  • IO----铺垫:装饰者模式

    什么是设计模式???

    • 设计模式是一套被反复使用、多数人知晓、经过分类编目的优秀代码设计经验的总结。

    什么是装饰者模式???这货接下来可是重头戏,可不能随便了事:Yes ! We need our heart to learn it !

    装饰模式的英文原话:

    • Attach additional responsibilities to an object dynamically keeping the interface.Decorators provide a flexible alternative to subclassing for extending functionality.

    装饰模式的类图如下(图片来自百度):

    接下来看一个比较容易理解的例子:

        //Car.java  相当于上图中的Component
        public interface Car{
            public void show();
        }
        
        //Benz.java  相当于上图的ConcreteComponent
        public class Benz implements Car{
            public void show(){
                System.out.println("This is Bens");
            }
        }
    
        //CarDecorator.java  //相当于上图的Decorator
        public abstract class CarDecorator implements Car{
            private Car car=null;
            public CarDEcorator(Car car){
                this.car=car;
            }
            public void show(){
                this.car.show();
            }
        }
    
        //ConcreteCarDecorator.java  相当于上图的ConreteDecoratorA(B)
        public ConcreteCarDecorator extends CarDecorator{
            public ConcreteCarDecorator(Car car){
                super(car);
            }   
            //This is a expend method!
            public void print(){
                System.out.println("This is on expand method!");
            }
            public void show(){
                super.show();
                //do something other...
            }
        }
    

    接着看一下IO流中的InputStream:

    看到上图是不是很有感触呢?是不是传说中的装饰者模式?IO中其他的流基本都是这种模式,因此各个模块体系结构很相似!

    在InputStream中,最常用的ConcreteComponent就是FileInputStream了,FilterInputStream就是Decorator,常用的ConcreteDecorator就是DataInputStrsam和BufferedInputStream

    通过使用不同的ConcreteDecorator包装FileInputStream,便可对到对File对象执行不同操作!

    好了,关于装饰者模式,我所认识的就是这样了!然后关于具体的InputStream和其他的IO模块,之后将逐一学习!

  • 相关阅读:
    用 WP7开发包 安装 WP7程序(XAP文件)
    在Windows 2003,XP上安装Windows Phone 7开发工具
    AutoResetEvent实现单并发控制
    .net源码研究(1)HashTable
    聚簇索引(Clustered Index)和非聚簇索引 (Non Clustered Index)
    信号量(semaphore)支持 多并发(n>=1)同步锁
    AtlasControlToolkit应用点滴(一)
    依赖注入dependency injection
    AtlasControlToolkit.CascadingDropDownNameValue自定义用法
    Thread.Join
  • 原文地址:https://www.cnblogs.com/realsoul/p/5929464.html
Copyright © 2011-2022 走看看