zoukankan      html  css  js  c++  java
  • C#装饰者模式实例代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 装饰者
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConcreteComponent con = new ConcreteComponent();
                ConcreteDecoratorA a = new ConcreteDecoratorA();
                a.SetComponent(con);
                a.Operation();
                a.AddBehavior();
                Console.WriteLine("============================");
                ConcreteDecoratorB B = new ConcreteDecoratorB();
                B.SetComponent(a);
                a.AddBehavior();
                B.Operation();
                B.AddBehavior();
            }
            public abstract class Component
            {
                public abstract void Operation();
            }
            abstract class Decorator : Component
            {
                private Component component;
                public void SetComponent(Component component)
                {
                    this.component = component;
                }
                public override void Operation()
                {
                    if (component != null)
                    {
                        component.Operation();
                    }
                }
            }
            class ConcreteDecoratorA : Decorator
            {
                public override void Operation()
                {
                    base.Operation();
                }
                public void AddBehavior()
                {
                    Console.WriteLine("装饰A类的操作!");
                }
            }
            class ConcreteDecoratorB : Decorator
            {
                public override void Operation()
                {
                    base.Operation();
                }
                public void AddBehavior()
                {
                    Console.WriteLine("装饰B类的操作!");
                }
            }
            public class ConcreteComponent : Component
            {
                public override void Operation()
                {
                    Console.WriteLine("具体对象的操作!");
                }
            }
        }
    }
  • 相关阅读:
    System.setProperty()
    ExtJs自学教程(1):一切从API開始
    【福利!】英雄联盟国服更新补丁速度极慢的解决方法
    ORACLE触发器具体解释
    我的Hook学习笔记
    Runood-JSP:JSP 客户端请求
    Runoob-JSP:JSP 隐式对象
    Runoob-JSP:JSP 动做元素
    Runoob-JSP:JSP 指令
    Runoob-JSP:JSP 生命周期
  • 原文地址:https://www.cnblogs.com/gc2013/p/4018884.html
Copyright © 2011-2022 走看看