• 装饰者模式 静态装饰组合


    静态装饰组合Static Decorator Compsition

    public abstract class Shape
        {
            public abstract string AsString();
        }
        public class Circel : Shape
        {
            float ridus;
    
            public Circel():this(0.5f)
            {
    
            }
            public Circel(float ridus)
            {
                this.ridus = ridus;
            }
    
            public override string AsString()
            {
                return $"Circle Ridus:{ridus}";
            }
    
            public void Resize(float size)
            {
                ridus *= size;
            }
        }
    
        public class Square : Shape
        {
            float Side;
    
            public Square():this(0.5f)
            {
    
            }
            public Square(float side)
            {
                Side = side;
            }
    
            public override string AsString()
            {
                return $"Square Side: {Side}";
            }
        }
        public class ColorShape : Shape
        {
            Shape shape;
            string color;
    
            public ColorShape(Shape shape, string color)
            {
                this.shape = shape;
                this.color = color;
            }
    
            public override string AsString()
            {
                return $"{shape.AsString()} Color :{color}";
            }
        }
    
        public class Transparent : Shape
        {
            Shape shape;
            float percentage;
    
            public Transparent(Shape shape, float _percentage)
            {
                this.shape = shape;
                this.percentage = _percentage;
            }
    
            public override string AsString()
            {
                return $"{shape.AsString()} Transparent:{percentage * 100.0}%";
            }
        }
        public class ColorShape<T> : Shape where T : Shape, new()
        {
            private string color;
            private T shape = new T();
            public ColorShape():this("Blue")
            { 
            }
            public ColorShape(string color)
            {
                this.color = color;
            }
    
           
            public override string AsString()
            {
                return $"{shape.AsString()} Color :{color}";
            }
        }
    
        public class TransparentShape<T> : Shape where T : Shape, new()
        {
            private float transparency;
    
            public TransparentShape(float transparency)
            {
                this.transparency = transparency;
            }
    
            private T shape = new T();
            public TransparentShape() : this(0)
            {
            }  
            public override string AsString()
            {
                return $"{shape.AsString()} Transparent :{transparency}";
            }
        }
    
    
    
        class Program
        {
            static void Main(string[] args)
            { 
                var redSquare = new ColorShape<Square>();
                Console.WriteLine(redSquare.AsString());
    
                var tranSquare = new TransparentShape<Circel>();
                Console.WriteLine(tranSquare.AsString());
    
                var tranRedSquare =new TransparentShape<ColorShape<Square>>(0.6f);
                Console.WriteLine(tranRedSquare.AsString());
            }
    
        }
  • 相关阅读:
    jQuery 源码解析(二十四) DOM操作模块 包裹元素 详解
    jQuery 源码解析(二十三) DOM操作模块 替换元素 详解
    jQuery 源码解析(二十二) DOM操作模块 复制元素 详解
    jQuery 源码分析(二十一) DOM操作模块 删除元素 详解
    jQuery 源码分析(二十) DOM操作模块 插入元素 详解
    jQuery 源码分析(十九) DOM遍历模块详解
    python 简单工厂模式
    python 爬虫-协程 采集博客园
    vue 自定义image组件
    微信小程序 image组件坑
  • 原文地址:https://www.cnblogs.com/Zingu/p/16288969.html
Copyright © 2020-2023  润新知