数码资讯
装饰者模式 静态装饰组合
选购提示
关注价格、性能、续航、售后和真实使用场景,理性比较后再下单。
静态装饰组合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()); } }
声明:本文内容用于数码产品信息整理与选购参考,具体价格、库存、售后政策以官方渠道和电商页面实时信息为准。