/** * 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()); }
private void decoratorPatternBtn_Click(object sender, RoutedEventArgs e) { CircleDecorator c = new CircleDecorator(); Console.WriteLine(c.StrFunc()); ColoredShape cc = new ColoredShape("red", c); Console.WriteLine(cc.StrFunc()); // Create a decorated Window with horizontal and vertical scrollbars DPWindow decoratedWindow = new HorizontalScrollBarDecorator( new VerticalScrollBarDecorator(new SimpleWindow())); // Print the Window's description Console.WriteLine(decoratedWindow.GetDescription()); Coffee kafica = new SimpleCoffee(); Utils.printInfo(kafica); kafica = new WithMilk(kafica); Utils.printInfo(kafica); kafica = new WithSprinkles(kafica); Utils.printInfo(kafica); }
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 circle = new Circle(2); var colored1 = new ColoredShape(circle, "red"); var colored2 = new ColoredShape(colored1, "blue"); WriteLine(circle.AsString()); WriteLine(colored1.AsString()); WriteLine(colored2.AsString()); }
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()); }
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()); }
/** * 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 RunCompositeDecoratorPattern() { StructuralDesignPatterns.DecoratorPattern.Square square = new StructuralDesignPatterns.DecoratorPattern.Square(1.23f); Console.WriteLine(square.AsString()); ColoredShape redSquare = new ColoredShape(square, "red"); Console.WriteLine(redSquare.AsString()); TransperentShape transperentShape = new TransperentShape(redSquare, 0.2f); Console.WriteLine(transperentShape.AsString()); }
public static void Main() { // polymorphic reference of interface type IDrawable i = new ColoredShape("Cirle", 10, 20, "Red"); i.Draw(); // can't call Color on i // polymorphic reference of interface type IHasColor c = new ColoredShape("Square", 5, 5, "Blue"); Console.WriteLine(c.Color); // can't call Draw on c // multiple polymorphism }
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()); }
public override Value Invoke(List Arguments) { Shape = new ColoredShape(); Firefly.AddToRenderList(Shape); FilledPoly = new Array(); OutlinePoly = new Array(); Identifiers["FilledPolygons"] = new Reference(FilledPoly); Identifiers["OutlinePolygons"] = new Reference(OutlinePoly); Identifiers["SetPolygons"] = new Reference(new ExternalFunction("SetPolygons", false, SetPolygons)); Identifiers["X"] = new Reference(new ExternalProperty("X", false, XChange, () => new Number(Shape.X))); Identifiers["Y"] = new Reference(new ExternalProperty("Y", false, YChange, () => new Number(Shape.Y))); Identifiers["Rotation"] = new Reference(new ExternalProperty("Rotation", false, X => Shape.Rotation = (float)((Number)X).Val, () => new Number(Shape.Rotation))); return(this); }
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 ColoredShape[] ParseFromText(string text) { ReguexAttribute attr = typeof(ColoredShape).GetTypeInfo().GetCustomAttribute <ReguexAttribute>(); string regexPerson = attr.Pattern; var shapeMatches = Regex.Matches(text, regexPerson); ColoredShape[] shapes = new ColoredShape[shapeMatches.Count]; for (int i = 0; i < shapeMatches.Count; i++) { var match = shapeMatches[i]; string color = match.Groups[1].Value; string shape = match.Groups[2].Value; shapes[i] = new ColoredShape(color, shape); } return(shapes); }
/** * 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# }
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()); }