class Program
{
static void Main(string[] args)
{
var joe = new PersonImpl(17);
Debug.Assert( joe.IsAdult() == false );
var proxy = new PersonProxyWithTarget( joe );
Debug.Assert(proxy.IsAdult() == false);
proxy.HasBirthday();
Debug.Assert(joe.IsAdult() != proxy.IsAdult()); // <- we have an issue here
}
}
public abstract class Person
{
protected int age;
public abstract int HasBirthday();
public bool IsAdult()
{
return this.age >= 18;
}
}
public class PersonProxyWithTarget : Person
{
private readonly Person target;
public PersonProxyWithTarget(Person target)
{
this.target = target;
}
public override int HasBirthday()
{
CallInterceptors();
return target.HasBirthday();
}
private void CallInterceptors()
{
//calls interceptors
}
}
public class PersonImpl : Person
{
public PersonImpl( int age )
{
this.age = age;
}
public override int HasBirthday()
{
return this.age++;
}
}