/** * The problem With dynamic decorator , that we can apply dynamic[run-time] decorator at the same type * {itself} means we can apply ColoredShape on other ColoredShape , which not make sense! * And there is no way to prevent this behaviour without affect decorate dynamic features * */ public static void Run() { // there is no constructor parameters forwarding in C# , unlike C++ // so we can not pass the side of the redSquare to ColoredShape constructor // i know, With complex properties passing solution we can solve this problem // but this will leads to very complex behaviours , so No. var redSquare = new ColoredShape <Square>("red"); WriteLine(redSquare.AsString()); // static decorator with composition var blackTransparentCircle = new TransparentShape <ColoredShape <Circle> >(30.2f); // No we can not change circle radius or ColoredShape Color // But There is a Solution in // D_7_StaticDecoratorCompositionWithInternalAccess.cs WriteLine(blackTransparentCircle.AsString()); // we can not access nested properties with casting to lower version of class // since no inheritance here , but we can still access every thing in smoth // way if we make it public blackTransparentCircle.Shape.Color = "Green"; blackTransparentCircle.Shape.Shape.Radius = 23.0f; WriteLine(blackTransparentCircle.AsString()); }
public void AsString_DecorateWithSquareAndColoredShape_GetTheirInfo() { TransparentShape <ColoredShape <Square> > blackHalfSquare = new TransparentShape <ColoredShape <Square> >(0.4f); var result = blackHalfSquare.AsString(); var expectedResult = "A square with side 0 has the color black has transparency 40"; Assert.That(result, Is.EqualTo(expectedResult)); }
public void AsString_DecorateWithSquareAndColoredShape_GetTheirInfo() { var square = new Square(1.23f); var redSquare = new ColoredShape(square, "red"); var redHalfTransparentSquare = new TransparentShape(redSquare, 0.5f); var result = redHalfTransparentSquare.AsString(); var expectedResult = "A square with side 1.23 has the color red has 50 transparency"; Assert.That(result, Is.EqualTo(expectedResult)); }
static void Main(string[] args) { var redSquare = new ColoredShape <Square>("red"); WriteLine(redSquare.AsString()); var circle = new TransparentShape <ColoredShape <Circle> >(0.4f); WriteLine(circle.AsString()); }
/** * The problem With dynamic decorator , that we can apply dynamic[run-time] decorator at the same type * {itself} means we can apply ColoredShape on other ColoredShape , which not make sense! * And there is no way to prevent this behaviour without affect decorate dynamic features * */ public static void Run() { var square = new Square(1.5f); WriteLine(square.AsString()); var blackSquare = new ColoredShape(square, "Black"); WriteLine(blackSquare.AsString()); var transparentBlackSquare = new TransparentShape(blackSquare, 75.4f); WriteLine(transparentBlackSquare.AsString()); }
static void Main(string[] args) { var cb = new CodeBuilder(); cb.AppendLine("class Foo") .AppendLine("{") .AppendLine("}"); WriteLine(cb); WriteLine(); // adapter-decorator MyStringBuilder s = "hello "; s += "world"; // will work even without op+ in MyStringBuilder // why? you figure it out! WriteLine(s); WriteLine(); // multiple inheritance var dragon = new Dragon { Weight = 123 }; dragon.Fly(); dragon.Crawl(); WriteLine(); // dynamic decorator composition var square = new Square(1.23f); WriteLine(square.AsString()); var redSquare = new ColoredShape(square, "red"); WriteLine(redSquare.AsString()); var redHalfTransparentSquare = new TransparentShape(redSquare, 0.5f); WriteLine(redHalfTransparentSquare.AsString()); // static decorator composition ColoredShape <Circle> blueCircle = new ColoredShape <Circle>("blue"); WriteLine(blueCircle.AsString()); TransparentShape <ColoredShape <Square> > blackHalfSquare = new TransparentShape <ColoredShape <Square> >(0.4f); WriteLine(blackHalfSquare.AsString()); ColoredShape <TransparentShape <Circle> > whiteHalfCircle = new ColoredShape <TransparentShape <Circle> >("white"); WriteLine(whiteHalfCircle.AsString()); }
static void Main(string[] args) { // in C# you cannot set constructor forwarding, so we can't initialize colroedShapre or square var redSquare = new TransparentShape <ColoredShape <Square> >(4.0f); WriteLine(redSquare.AsString()); var circle = new TransparentShape <ColoredShape <Circle> >(4.0f); //circle.Radius doesn't work because we are using template pattern // we are not using inheritance // static approach isn't good enough in C# WriteLine(redSquare.AsString()); }
public static void DynamicDecoratorComposition() { var square = new Square(1.23f); WriteLine(square.AsString()); var redSquare = new ColoredShape(square, "red"); WriteLine(redSquare.AsString()); var redHalfTranparentSquare = new TransparentShape(square, 0.5f); WriteLine(redHalfTranparentSquare.AsString()); }
static void Main(string[] args) { var square = new Square(1.23f); WriteLine(square.AsString()); var redSquare = new ColoredShape(square, "red"); WriteLine(redSquare.AsString()); var redHalfTransparentSquare = new TransparentShape(redSquare, 0.5f); WriteLine(redHalfTransparentSquare.AsString()); }
private static void Main(string[] args) { Shape square = new ColoredShape <Square>("red"); WriteLine(square.AsString()); Shape trCircle = new TransparentShape <ColoredShape <Circle> >(0.25f); WriteLine(trCircle.AsString()); square = new Square(1.23f); WriteLine(square.AsString()); var redSquare = new ColoredShape(square, "red"); WriteLine(redSquare.AsString()); var trRedSquare = new TransparentShape(redSquare, 0.25f); WriteLine(trRedSquare.AsString()); }
static void Main(string[] args) { // SIMPLE DECORATOR ON SEALED CLASS var cb = new CodeBuilder(); cb.AppendLine("class Foo") .AppendLine("{") .AppendLine("}"); Console.WriteLine(cb); // ADAPTER DECORATOR MyStringBuilder s = "hello "; s += "world"; // will work even without op+ in MyStringBuilder // why? you figure it out! Console.WriteLine(s); // DYNAMIC DECORATOR COMPOSITION var square = new Square(1.23f); Console.WriteLine(square.AsString()); var redSquare = new ColoredShape(square, "red"); Console.WriteLine(redSquare.AsString()); var redHalfTransparentSquare = new TransparentShape(redSquare, 0.5f); Console.WriteLine(redHalfTransparentSquare.AsString()); // static ColoredShape <Circle> blueCircle = new ColoredShape <Circle>("blue"); Console.WriteLine(blueCircle.AsString()); TransparentShape <ColoredShape <Square> > blackHalfSquare = new TransparentShape <ColoredShape <Square> >(0.4f); Console.WriteLine(blackHalfSquare.AsString()); Console.ReadLine(); }
public static void Run() { var square = new Square(1.23f); Console.WriteLine(square.AsString()); var redSquare = new ColoredShape(square, "red"); Console.WriteLine(redSquare.AsString()); var redHalfTransparent = new TransparentShape(redSquare, 0.5f); Console.WriteLine(redHalfTransparent.AsString()); var redSquare2 = new ColoredShape2 <Square2>("red"); Console.WriteLine(redSquare2.AsString()); var circle2 = new TransparentShape2 <ColoredShape2 <Circle2> >(0.4f); Console.WriteLine(circle2.AsString()); }
/** * The problem With dynamic decorator , that we can apply dynamic[run-time] decorator at the same type * {itself} means we can apply ColoredShape on other ColoredShape , which not make sense! * And there is no way to prevent this behaviour without affect decorate dynamic features * */ public static void Run() { // there is no constructor parameters forwarding in C# , unlike C++ // so we can not pass the side of the redSquare to ColoredShape constructor // i know, With complex properties passing solution we can solve this problem // but this will leads to very complex behaviours , so No. var redSquare = new ColoredShape <Square>("red"); WriteLine(redSquare.AsString()); // static decorator with composition var blackTransparentCircle = new TransparentShape <ColoredShape <Circle> >(30.2f); // No we can not change circle radius or ColoredShape Color // But There is a Solution in // D_7_StaticDecoratorCompositionWithInternalAccess.cs WriteLine(blackTransparentCircle.AsString()); // Just notice with static decorator composition solution in C# we ends with instances hell // with the fact that we must make every thing public, [review: {D_7_StaticDecoratorCompositionWithInternalAccess.cs}] // To Allow access fields , so remember: here the cost of instantiate any object is very high // since we does not use inheritance , So Just Don't Use It With C# }