Coloring the decorator's memory

I have this base class with the following interface:

abstract class Base
{
  abstract public object Val
  {
    get;
  }
}

For any derived classes, the Valvalue must be specified at the time the object is created.
The question is: how can I make a derived class do this (hopefully at compile time)?
I tried to add a constructor:

abstract class Base
{
  public Base(object value)
  {
    val = value;
  }

  private object val;

  ...
}

, , ( ).
- , - Decorator/Wrapper, GoF. , , .

+1
2

:

abstract class Base 
{
    public Base(object val)
    {
        this.Val = val;
    }

    public object Val { get; private set; }
}

, :

public class Derived : Base
{
    public Derived(object val) : base(val) { }
}
+2

, :

public override object Val {
    // add any decoration effects here if needed
    get { return tail.Val; }
}

tail - , .


, , ( ) - :

abstract class BaseClass {
    protected BaseClass(object val) {...}
}
class ConcreteType : BaseClass {
    public ConcreteType(object val)
        : base(val) { }
}

..

+2

All Articles