I am wondering how one would force one variable to always point to what another variable is pointing to.
Considering the following program :
using System;
public class Program
{
public static void Main()
{
Person p = new Person{Name = "Aelphaeis", Age = -1};
p.introBehavior.Introduce();
}
class Person
{
public String Name { get;set; }
public Int32 Age { get;set; }
public IIntroductionBehavior introBehavior{get;set;}
public Person(){
Name = "over";
Age = 9000;
introBehavior = new IntroductionBehavior(Name, Age);
}
}
class IntroductionBehavior : IIntroductionBehavior
{
String Name;
Int32 Age;
public IntroductionBehavior(string name, int age){
Age = age;
Name = name;
}
public void Introduce(){
Console.WriteLine("I'm " + Name + " and I am " + Age + " years old");
}
}
interface IIntroductionBehavior
{
void Introduce();
}
}
If you run this program you will get
I'm over and I am 9000 years old
The desired behavior would be for the Name and Age inside of the IntroductionBehavior to point to whatever the value of the properties inside of Person are pointing too. It should print :
I'm Aelphaeis and I am -1 years old
If this is not possible what type of redesign would could I implement to ensure that IntroductionBehavior will always print the values of Person without making IntroductionBehavior aware of Person?
Edit: A lot of people seem to be confused about why I don't want to make IntroductionBehavior Aware of Person.
Introduction is actually intended to do an operation on a repository. The variables name and age represent the repository which will be manipulated. I'm trying to loosely couple things so that they are easy to debug.
Give IntroductionBehavior reference to person:
class IntroductionBehavior : IIntroductionBehavior
{
private Person person;
public IntroductionBehavior(Person person){
this.person = person;
}
public void Introduce(){
Console.WriteLine("I'm {0} and I am {1} years old",
person.Name, person.Age); // NOTE: use string format
}
}
class Person
{
public String Name { get; set; }
public Int32 Age { get; set; }
public IIntroductionBehavior introBehavior { get; set; }
public Person(){
Name = "over";
Age = 9000;
introBehavior = new IntroductionBehavior(this);
}
}
Thus IntroductionBehavior will get name and age values or person at time when you'll ask it to introduce.
Another option is creating IntroductionBehavior instance in property getter instead of creating it in constructor:
public IIntroductionBehavior introBehavior
{
get { return new IntroductionBehavior(Name, Age); }
}
Thus IntroductionBehavior will capture name and age values of person at time when you get it instead of time of person creation. NOTE: if you'll update person's name or age between getting introBehavior and introducing it, then you will see old data.
Of course, usage of IntroductionBehavior class is arguable..
Usually what you then do in C# is pass the parent of the object to its constructor. In this case IntroductionBehavior would store the parent Person.
For example:
class IntroductionBehavior : ...
{
public Person Parent {get; private set;}
public IntroductionBehavior(Person parent)
{
Parent = parent;
}
...
}
Then you can use Parent.Age to get the age.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With