我试图了解如何在使用面向对象编程时对两个对象之间的“复杂”关系进行建模。
我可以理解如何管理简单的关系,例如当对象之间存在 1:1 或 1:M 映射时,即;
1:1 关系
public Car
{
public string Manufacturer { get; set; }
public string Model { get; set; }
public Engine Engine { get; set; } // 1:1 relationship here
}
public Engine
{
public int NumberOfCylinders { get; set; }
public Car Car { get; set; } // 1:1 relationship here
}
1:M 关系
public Father
{
public string FullName { get; set; }
public List<Child> Children { get; set; } // 1:M relationship here
}
public Child
{
public string FullName { get; set; }
public Father Father { get; set; } // 1:M relationship here
}
..但问题是,当关系更复杂时,我如何管理两个对象之间的关系?
例如,假设有一个任务可以由 Joe或John 完成(即任何一个人都可以完成任务)。我将如何建模?
public Task
{
public string Description { get; set; }
// what do I put here?
}
public Person
{
public string FullName { get; set; }
// what do I put here?
}
var joe = new Person() { FullName = "Joe" };
var john = new Person() { FullName = "John" };
var task = new Task() { Description = "Task that can be completed by either Joe or John" };
我确信必须有一个通用模式可以用来模拟这样的情况,但我一直找不到解决方案!