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

    using System;
    using System.Collections.Generic;
    using System.Text;

    //装饰者模式
    namespace designMode
    {
    class Person
    {
    public Person()
    {

    }

    private string name;
    public Person(string name)
    {
    this.name = name;
    }

    public virtual void Show()
    {
    Console.WriteLine(
    "装扮的{0}", name);
    }
    }

    class Finery : Person
    {
    protected Person component;

    //打扮
    public void Decorate(Person component)
    {
    this.component = component;
    }

    public override void Show()
    {
    if (component != null)
    {
    component.Show();
    }
    }
    }

    class TShirts : Finery
    {
    public override void Show()
    {
    {
    Console.WriteLine(
    "大T恤");
    base.Show();
    }
    }
    }

    class BigTrouser : Finery
    {
    public override void Show()
    {
    {
    Console.WriteLine(
    "垮裤");
    base.Show();
    }
    }
    }

    class WearSneakers : Finery
    {
    public override void Show()
    {
    {
    Console.WriteLine(
    "破球鞋");
    base.Show();
    }
    }
    }

    class WearSuit : Finery
    {
    public override void Show()
    {
    {
    Console.WriteLine(
    "西装");
    base.Show();
    }
    }
    }

    class WearTie : Finery
    {
    public override void Show()
    {
    {
    Console.WriteLine(
    "领带");
    base.Show();
    }
    }
    }

    class WearLeatherShoes : Finery
    {
    public override void Show()
    {
    {
    Console.WriteLine(
    "破球鞋");
    base.Show();
    }
    }
    }

    }

      

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace designMode
    {
    class Program
    {
    static void Main(string[] args)
    {

    //装饰者模式
    Person xc = new Person("小菜");
    Console.WriteLine(
    "\n第一张装扮:");

    WearSneakers pqx
    = new WearSneakers();
    BigTrouser kk
    = new BigTrouser();
    TShirts dtx
    = new TShirts();

    pqx.Decorate(xc);
    kk.Decorate(pqx);
    dtx.Decorate(kk);
    dtx.Show();

    Console.WriteLine(
    "\n第二种装扮:");

    WearLeatherShoes px
    = new WearLeatherShoes();
    WearTie ld
    = new WearTie();
    WearSuit xz
    = new WearSuit();

    px.Decorate(xc);
    ld.Decorate(px);
    xz.Decorate(ld);
    xz.Show();

    Console.Read();
    }
    }
    }

      

  • 相关阅读:
    linux搭建svn服务器
    Cmder添加到右键菜单
    linux系统配置本地软件仓库
    pom文件parent标签的使用,parent版本号报红线(很明显引用的是本地自己的包)
    Redis学习记录-001
    (概念总结)快速了解JVM结构和工作原理
    Java 设计模式(七)《抽象工厂模式》
    多线程间通信wait(),notify(),notifyAll()
    快速了解数据结构
    JDK1.8 Consumer & Supplier 什么意思
  • 原文地址:https://www.cnblogs.com/cjingzm/p/2124395.html
Copyright © 2011-2022 走看看