zoukankan      html  css  js  c++  java
  • java_IO_装饰器

    装饰器模式

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。

    这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

     1 package ioStudy;
     2 
     3 
     4 /**
     5  * 装饰器模式
     6  * 1.抽象接口
     7  * 2.具体对象
     8  * 3.抽象装饰者
     9  * 4.具体装饰者
    10  * */
    11 public class DecorateStudy1 {
    12     
    13     public static void main(String[] args) {
    14         Drink coffee = new Coffee();
    15         
    16         Decorate milk = new Milk(coffee);
    17         System.out.println(milk.info()+"===>"+milk.cost());
    18         
    19         Decorate sugar = new Sugar(coffee);
    20         System.out.println(sugar.info()+"===>"+sugar.cost());
    21         
    22         milk = new Milk(sugar);
    23         System.out.println(milk.info()+"===>"+milk.cost());
    24     }
    25 }
    26 
    27 
    28 interface Drink {
    29     String info();
    30     int cost();
    31 }
    32 
    33 class Coffee implements Drink{
    34     
    35     private String name = "原味咖啡";
    36     private int price = 10;
    37 
    38     @Override
    39     public String info() {
    40         // TODO Auto-generated method stub
    41         return name;
    42     }
    43 
    44     @Override
    45     public int cost() {
    46         // TODO Auto-generated method stub
    47         return price;
    48     }
    49 }
    50 
    51 abstract class Decorate implements Drink {
    52     Drink drink;
    53     public Decorate(Drink drink) {
    54         this.drink = drink;
    55     }
    56     
    57     public String info() {
    58         return drink.info();
    59     }
    60     
    61     public int cost() {
    62         return drink.cost();
    63     }
    64 }
    65 
    66 class Milk extends Decorate{
    67 
    68     public Milk(Drink drink) {
    69         super(drink);
    70     }
    71     
    72     public String info() {
    73         return super.drink.info() + "加入了牛奶";
    74     }
    75     
    76     public int cost() {
    77         return super.drink.cost()*4;
    78     }
    79 }
    80 
    81 class Sugar extends Decorate{
    82 
    83     public Sugar(Drink drink) {
    84         super(drink);
    85     }
    86     
    87     public String info() {
    88         return super.drink.info() + "加入了方糖";
    89     }
    90     
    91     public int cost() {
    92         return super.drink.cost()*3;
    93     }
    94 }

    用BufferedInputStream  BufferedOutputStream  BufferedReader  BufferWriter  包装其他输入输出流  能够提升读写速度。


     

  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10952617.html
Copyright © 2011-2022 走看看