Add code examples, and documentation
This commit is contained in:
15
patterns/decorator/ShapeDecorations/Shapes/Circle.cs
Normal file
15
patterns/decorator/ShapeDecorations/Shapes/Circle.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace ShapeDecorations.Shapes;
|
||||
|
||||
public class Circle : IShape
|
||||
{
|
||||
public void Draw()
|
||||
{
|
||||
Console.WriteLine("Drawing a circle");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace ShapeDecorations.Shapes.Decorators;
|
||||
|
||||
/// <summary>
|
||||
/// See the RedColor class for details on the implementation
|
||||
/// </summary>
|
||||
public class BlueBorder : ShapeDecorator
|
||||
{
|
||||
/// <summary>
|
||||
/// See the RedColor class for details on the implementation
|
||||
/// </summary>
|
||||
public BlueBorder(IShape decorated)
|
||||
: base(decorated) { }
|
||||
|
||||
public override void Draw()
|
||||
{
|
||||
_decorated.Draw();
|
||||
Console.WriteLine("\tWith a blue border");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace ShapeDecorations.Shapes.Decorators;
|
||||
|
||||
public class HugeSize : ShapeDecorator
|
||||
{
|
||||
/// <summary>
|
||||
/// See the RedColor class for details on the implementation
|
||||
/// </summary>
|
||||
/// <param name="decorated">
|
||||
/// The IShape (whtether concrete IShape or concrete
|
||||
/// ShapeDecorator) to be decorated
|
||||
/// </param>
|
||||
public HugeSize(IShape decorated)
|
||||
: base(decorated) { }
|
||||
|
||||
public override void Draw()
|
||||
{
|
||||
_decorated.Draw();
|
||||
Console.WriteLine("\tThat is enourmous in size");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace ShapeDecorations.Shapes.Decorators;
|
||||
|
||||
public class RedColor : ShapeDecorator
|
||||
{
|
||||
/// <summary>
|
||||
/// Public ctor. Calls the base class with the passed IShape, thus decorating
|
||||
/// it.
|
||||
/// Implementation is left empty, as the base class does all the work needed.
|
||||
/// </summary>
|
||||
/// <param name="decorated"></param>
|
||||
public RedColor(IShape decorated)
|
||||
: base(decorated) { }
|
||||
|
||||
/// <summary>
|
||||
/// Performs the actual drawing logic, but first calls the decorated member,
|
||||
/// adding its own logic afterwards.
|
||||
/// </summary>
|
||||
public override void Draw()
|
||||
{
|
||||
// First calls decorated member
|
||||
_decorated.Draw();
|
||||
|
||||
// Then adds additional drawing logic
|
||||
Console.WriteLine("\tWhich is red in color");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace ShapeDecorations.Shapes.Decorators;
|
||||
|
||||
public abstract class ShapeDecorator : IShape
|
||||
{
|
||||
/// <summary>
|
||||
/// Maintains a reference to the shape which it decorates
|
||||
/// this allows implementors to add additional steps to the "drawing" process.
|
||||
/// Marked "protected" so that inheriting classes have access to it.
|
||||
/// </summary
|
||||
protected IShape _decorated;
|
||||
|
||||
/// <summary>
|
||||
/// Acceps a reference to any IShape.
|
||||
/// Since the ShapeDecorator itself IS an IShape, this
|
||||
/// decorator can also decorate other decorators
|
||||
/// However, since a ShapeDecorator has to be instantiated with an existing
|
||||
/// IShape,
|
||||
/// at some point in the chain of decoration, there must be a concrete shape
|
||||
/// (specifically, a concrete IShape that was createad without having another
|
||||
/// IShape passed to it)
|
||||
/// </summary>
|
||||
/// <param name="decorated"></param>
|
||||
public ShapeDecorator(IShape decorated)
|
||||
{
|
||||
_decorated = decorated;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marking this abstract satisfies the implementation of IShape,
|
||||
/// but forces inheriting classes to implement their own drawing logic
|
||||
/// </summary>
|
||||
public abstract void Draw();
|
||||
}
|
||||
6
patterns/decorator/ShapeDecorations/Shapes/IShape.cs
Normal file
6
patterns/decorator/ShapeDecorations/Shapes/IShape.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace ShapeDecorations.Shapes;
|
||||
|
||||
public interface IShape
|
||||
{
|
||||
void Draw();
|
||||
}
|
||||
15
patterns/decorator/ShapeDecorations/Shapes/Rectangle.cs
Normal file
15
patterns/decorator/ShapeDecorations/Shapes/Rectangle.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
#region
|
||||
|
||||
using System;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace ShapeDecorations.Shapes;
|
||||
|
||||
public class Rectangle : IShape
|
||||
{
|
||||
public void Draw()
|
||||
{
|
||||
Console.WriteLine("Drawing a Rectangle");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user