装饰者模式 静态装饰组合
静态装饰组合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()); } }