MyObject has a shape and the shape have to be chosen and passed as argument to the ctor, can’t exist a MyObject whitout a shape and the shape can’t vary along his life. It happens often in real life.
namespace JackNova.ConsoleClient.Test.Fun
{
abstract class Shape { }
class Circle : Shape { }
class Square : Shape { }
class Triangle : Shape { }
static class Shapes
{
public static Circle Circle { get { return new Circle(); } }
public static Square Square { get { return new Square(); } }
public static Triangle Triangle { get { return new Triangle(); } }
}
class MyObject
{
public Shape Shape { get; private set; }
public MyObject(Shape shape)
{
this.Shape = shape;
}
}
class Test
{
static void Run()
{
MyObject coolOne = new MyObject(Shapes.Circle);
}
}
}
I think I’m violating some principle here, the open closed one for example.
My purpose is to simplify developing at design time. As you can see when I instanciate MyObject I don’t have to remeber wich kind of objects I can pass as argument but they are instanciated and passed by the abstract class.
Do you think this sucks?
It’s fine to have an abstract class and a class which takes a reference to an instance of that abstract class in the constructor and sets an effectively-readonly property.
I would question your use of properties in the static class though:
new Circle()?