首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >企业应用设计模式

企业应用设计模式
EN

Software Engineering用户
提问于 2013-02-07 18:53:48
回答 2查看 1.5K关注 0票数 4

我很少读到关于复合模式的文章,我想知道它是否适用于以下情况,

我发现“复合实体对象可以表示粗粒度对象及其所有相关的依赖对象”。

代码语言:javascript
复制
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; }
}

通过使用这两个对象,我需要创建

代码语言:javascript
复制
public class Patient
  {
        public string ID{ get; set; }
        public string FullName { get; set; }
        public DateTime AdmitDate;
  }

在这里,我可以将复合模式用于企业应用程序吗?

我可以在下面添加一些类吗?

代码语言:javascript
复制
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;
    }
}
EN

回答 2

Software Engineering用户

回答已采纳

发布于 2013-02-07 22:19:19

复合模式意味着当您在复合上调用方法perform() (perform()是IComponent接口契约的一部分)时,调用被重定向到它的所有子组件,直到它们是执行实际操作的叶元素。

在您的情况下,我认为粗粒度和细粒度之间绝对没有区别,哪个实体组/将其他实体组表示为一个组合。

所以我会说不,复合模式实际上不适用于这里。

wikipedia类图可能表明了我的观点:(在这个图中执行=操作)

票数 4
EN

Software Engineering用户

发布于 2013-02-08 17:15:17

您的类允许外部力量通过public成员修改数据。因为这些应该是实体对象,所以强制数据的不可变性。如果需要更改数据的一部分,则丢弃陈旧的对象,并使用新的数据创建一个新的对象。示例:

代码语言:javascript
复制
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;
         }
     }
}

最后,我要用构图来做这件事:

代码语言:javascript
复制
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;
        }
    }
}
票数 1
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/186287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档