zoukankan      html  css  js  c++  java
  • 装饰模式

     1 package com.java.decorator;
     2 
     3 /**
     4  * 装饰模式
     5  */
     6 public class DecoratorTest {
     7     public static void main(String[] args) {
     8         Person person = new Person("西欧阿飞");
     9         person.show();
    10         XiFu xiFu = new XiFu(person);
    11         System.out.println(xiFu.show() + " 去约会喽~");
    12     }
    13 }
    14 
    15 //抽象组件接口
    16 interface Component {
    17     public String show();
    18 
    19 }
    20 
    21 /**
    22  * 具体构建对象
    23  */
    24 class Person implements Component {
    25     private String name;
    26 
    27     public Person(String name) {
    28         this.name = name;
    29     }
    30 
    31     public Person() {
    32     }
    33 
    34     public String getName() {
    35         return name;
    36     }
    37 
    38     public void setName(String name) {
    39         this.name = name;
    40     }
    41 
    42     @Override
    43     public String show() {
    44         return getName() + " 穿着";
    45     }
    46 
    47 }
    48 
    49 /**
    50  * 抽象装饰者
    51  */
    52 abstract class Decorator extends Person {
    53     private Person person;
    54 
    55     // 没有提供无参构造器
    56 
    57     public Decorator(Person person) {
    58         this.person = person;
    59     }
    60 
    61     @Override
    62     public String show() {
    63         return person.show();
    64     }
    65 }
    66 
    67 /**
    68  * 具体装饰
    69  */
    70 class XiFu extends Decorator {
    71 
    72     public XiFu(Person person) {
    73         super(person);
    74     }
    75 
    76     @Override
    77     public String show() {
    78         return super.show() + " 西服";
    79     }
    80 }
    81 
    82 class ChenShan extends Decorator {
    83 
    84     public ChenShan(Person person) {
    85         super(person);
    86     }
    87 
    88     @Override
    89     public String show() {
    90         return super.show() + " 衬衫";
    91     }
    92 
    93 }
  • 相关阅读:
    Pytorch多进程最佳实践
    torchvision简介
    Pytorch数据变换(Transform)
    Pytorch自定义数据库
    DenseNet笔记
    Focal Loss笔记
    Pytorch数据读取框架
    二叉树的先序、中序、后序递归与非递归实现遍历
    Group Normalization笔记
    sap 怎么导出sap的各种表
  • 原文地址:https://www.cnblogs.com/yonxin/p/12554819.html
Copyright © 2011-2022 走看看