我很少读到关于复合模式的文章,我想知道它是否适用于以下情况,
我发现“复合实体对象可以表示粗粒度对象及其所有相关的依赖对象”。
public class PatientRegistrationDTO
{
public string RegistrationNo;
public string ID;
public DateTime AdmitDate;
}
public class PersonDTO
{
public string ID{ get; set; }
public string FullName { get; set; }
public string FirstName { get; set; }
}通过使用这两个对象,我需要创建
public class Patient
{
public string ID{ get; set; }
public string FullName { get; set; }
public DateTime AdmitDate;
}在这里,我可以将复合模式用于企业应用程序吗?
我可以在下面添加一些类吗?
public class PatientDTO
{
public static Patient ConvertToEntity(PatientRegistrationDTO pregDTO, PersonDTO person)
{
Patient p = new Patient();
p.ID= pregDTO.ID;
p.FullName = person.FullName;
p.AdmitDate = pregDTO.AdmitDate;
return p;
}
}发布于 2013-02-07 22:19:19
复合模式意味着当您在复合上调用方法perform() (perform()是IComponent接口契约的一部分)时,调用被重定向到它的所有子组件,直到它们是执行实际操作的叶元素。
在您的情况下,我认为粗粒度和细粒度之间绝对没有区别,哪个实体组/将其他实体组表示为一个组合。
所以我会说不,复合模式实际上不适用于这里。
wikipedia类图可能表明了我的观点:(在这个图中执行=操作)

发布于 2013-02-08 17:15:17
您的类允许外部力量通过public成员修改数据。因为这些应该是实体对象,所以强制数据的不可变性。如果需要更改数据的一部分,则丢弃陈旧的对象,并使用新的数据创建一个新的对象。示例:
public sealed class PatientRegistrationDTO
{
private readonly string registrationNo;
private readonly string id;
private readonly DateTime admitDate;
public PatientRegistrationDTO(string registrationNo, string id, DateTime admitDate)
{
this.registrationNo = registrationNo;
this.id = id;
this.admitDate = admitDate;
}
public string RegistrationNo
{
get
{
return this.registrationNo;
}
}
public string ID
{
get
{
return this.id;
}
}
public DateTime AdmitDate
{
get
{
return this.admitDate;
}
}
}
public sealed class PersonDTO
{
private readonly string id;
private readonly string fullName;
private readonly string firstName;
public PersonDTO(string id, string fullName, string firstName)
{
this.id = id;
this.fullName = fullName;
this.firstName = firstName;
}
public string ID
{
get
{
return this.id;
}
}
public string FullName
{
get
{
return this.fullName;
}
}
public string FirstName
{
get
{
return this.firstName;
}
}
}最后,我要用构图来做这件事:
public sealed class Patient
{
private readonly PatientRegistrationDTO patientRegistration;
private readonly PersonDTO person;
public Patient(PatientRegistrationDTO patientRegistration, PersonDTO person)
{
// Appropriate nullity checks as needed here.
this.patientRegistration = patientRegistration;
this.person = person;
}
public string ID
{
get
{
return this.patientRegistration.ID;
}
}
public string FullName
{
get
{
return this.person.FullName;
}
}
public DateTime AdmitDate
{
get
{
return this.patientRegistration.AdmitDate;
}
}
}https://softwareengineering.stackexchange.com/questions/186287
复制相似问题