zoukankan      html  css  js  c++  java
  • 设计模式学习笔记——修饰模式(Decorator Pattern)

    学习TerryLee的设计模式颇有感触,留下以下笔记以作日后参考。

    代码
    //--------------------------------------------------------
    //修饰模式也是常用于类的扩展,这种方法可以使类的组合变得十分灵活。
    //--------------------------------------------------------

    #region 基础类

    public interface IPerson
    { }

    public class Man : IPerson
    { }

    public class Women : IPerson
    { }

    #endregion

    //--------------------------------------------------------
    //我们现在需要给男人和女人穿上衣服。
    //怎么做?
    //You can use Decorator Pattern.
    //--------------------------------------------------------

    #region 新增的类

    public class ClothesWapper : IPerson
    {
    IPerson Person;

    public ClothesWapper(IPerson person)
    {
    Person
    = person;
    }
    }

    public class Coat : ClothesWapper
    {
    public Coat(IPerson person)
    :
    base(person)
    { }
    }

    public class Trouser : ClothesWapper
    {
    public Trouser(IPerson person)
    :
    base(person)
    { }
    }

    #endregion

    #region 客户端调用

    public class App
    {
    public static void Main()
    {
    //给男人穿上外套
    Man man = new Man();
    ClothesWapper coatMan
    = new Coat(man);

    //给男人穿上裤子
    ClothesWapper trouserMan = new Trouser(man);

    //男人同时穿上外胎和裤子
    ClothesWapper coatAndTrouserMan = new Trouser(coatMan);
    }
    }

    #endregion
  • 相关阅读:
    Java注解入门
    两种求素数
    几个经典的递归小程序
    Java8新特性——接口的默认方法和类方法
    SSH框架总结
    初识SSH框架
    Mybatis中DAO层接口没有写实现类,Mapper中的方法和DAO接口方法是怎么绑定到一起的
    使用SQL查询所有数据库名和表名
    mybatis中#{}和${}的区别
    SOCKET, TCP/UDP, HTTP, FTP 浅析
  • 原文地址:https://www.cnblogs.com/chuifeng/p/1916616.html
Copyright © 2011-2022 走看看