• java 装饰者类


    装饰者模式:增强一个类的功能还可以让装饰者类之间互相装饰。


    装饰者模式和继承的区别:

    继承实现的增强类:
      优点:代码结构清晰,而且实现简单
      缺点:对于每一个的需要增强的类都要创建具体的子类来帮助其增强,这样会导致继承体系过于庞大。


    装饰模式实现的增强类:
      优点:内部可以通过多态技术对多个需要增强的类进行增强
      缺点:需要内部通过多态技术维护需要增强的类的实例。进而使得代码稍微复杂。

    下面来个例子来实现装饰者模式的效果

     1 //1.创建一个需要继承的父类(或者需要扩展功能的类)
     2 class Person {
     3     public void study() {
     4         System.out.println("学习");
     5     }
     6 }
     7 
     8 //2.装饰者类(子类)
     9 class Student1 extends Person{
    10     //2.1子类中维护一个父类(被装饰者类)
    11     Person student;
    12     //2.2构造方法,里面传一个被装饰者类的对象
    13     public Student1(Person student) {
    14         this.student = student;
    15     }
    16     
    17     //2.3复写被装饰者类的 需要扩展的方法
    18     @Override
    19     public void study() {
    20         student.study();
    21         //2.4对方法进行扩展,添加“读书”这一方法
    22         System.out.println("读书");    
    23     }
    24     
    25 }
    26 
    27 //3.类似前面的对被装饰者方法进行扩展另一个方法
    28 class Student2 extends Person{
    29     Person student;
    30     public Student2(Person student) {
    31         this.student  = student ;
    32     }
    33 
    34     @Override
    35     public void study() {
    36         // TODO Auto-generated method stub
    37         student.study();
    38         //另一个扩展方法,“写字”
    39         System.out.println("写字");
    40     }
    41 }
    42 
    43 
    44 class Student3 extends Person{
    45     Person student;
    46     
    47     public Student3(Person student) {
    48         this.student  = student ;
    49     }
    50 
    51     @Override
    52     public void study() {
    53         // TODO Auto-generated method stub
    54         student.study();
    55         //另一个扩展方法,“画画”
    56         System.out.println("画画");
    57     }
    58 }
    59 
    60 
    61 public class readerExt {
    62 
    63     public static void main(String[] args) {
    64         
    65         //5.创建被装饰者类的对象
    66         Person person = new Person();
    67         person.study();  //学习
    68         
    69         //5.1创建装饰者类(子类)的对象,传入被装饰者类的对象
    70         Student1 s1 = new Student1(person);
    71         s1.study();  //学习,读书
    72         
    73         Student2 s2 = new Student2(person);
    74         s2.study();  //学习,写字
    75         
    76         //5.2 装饰者 互相装饰
    77         Student3 s3 = new Student3(s2);
    78         s3.study();  //学习,读书,画画
    79         
    80     }    
    81 }
  • 相关阅读:
    [quote]HowTo Format Date For Display or Use In a Shell Script
    [quote] standard Input and Output Redirection
    [quote] Re: [ECOS] printf and diag_printf go to nowhere 2
    [quote] Re: [ECOS] printf and diag_printf go to nowhere
    [quote] DMA engine in Linux Kernel
    [linux]How to set PATH in shell script, and keep it avaiable even after it exits
    use AWK to extract some lines according to some patterns in file
    [Reprinted] Howto Use Linux Watchdog
    Hadoop 1 ecosystem
    Java Comparable & Comparator
  • 原文地址:https://www.cnblogs.com/bigerf/p/6143314.html
Copyright © 2020-2023  润新知