Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I read few articles about composite pattern and I want to know whether its applicable in following situation,

I found that "A Composite Entity object can represent a coarse-grained object and all its related dependent objects"

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; }
}

by using these two objects I need to create

public class Patient
  {
        public string ID{ get; set; }
        public string FullName { get; set; }
        public DateTime AdmitDate;
  }

Can I use Composite pattern for enterprise applications in here?

Is it possible for me to add some class like below?

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;
    }
}
share|improve this question
    
Why not? . . . . –  Robert Harvey Feb 7 '13 at 21:45
    
Why yes?. . . . –  Stephane Rolland Feb 7 '13 at 22:24
    
@RobertHarvey what do u mean by why not? –  DevT Feb 8 '13 at 3:31
    
@StephaneRolland what do u mean by why yes?? –  DevT Feb 8 '13 at 3:31
    
@Tilt I mean the contrary of Robert's not. –  Stephane Rolland Feb 8 '13 at 8:54

2 Answers 2

up vote 3 down vote accepted

The composite pattern implies that when you call a method perform() on a composite (perform() being part of the interface contract of IComponent), the call is redirected to all its sub-components, until they are leaf elements that do an actual thing.

In your case I see absolutely no difference between the coarse and the fine grain, and which entity group/represent others as a composite.

So I would say no, the composite pattern really doesn't apply here.

the wikipedia class diagram may explicit my point of view: ( in this diagram perform = operation) enter image description here

share|improve this answer
    
in enterprise design patter also does it apply same? i felt there's some difference. –  DevT Feb 8 '13 at 4:16
    
First, show me a Department entity, or Service, then that would make sense. –  Stephane Rolland Feb 8 '13 at 8:56
    
in my scenario web service is host to enter PatientRegistrationDTO information. so when user enter those information it takes value of ID and calls the external web service. then it will return object of PersonDTO. my database class (entity class) is Patient . so i want to create Patient object using these two classes. (PatientRegistrationDTO & PersonDTO) –  DevT Feb 8 '13 at 9:15
2  
I would create a class Patient, who would have as private data members, one PatientDTO and one PersonDTO. And I would delegate the exposed public members of class Patient to either PatientDTO or to PersonDTO. It's not really a Design Pattern, it's simply called Aggregation. –  Stephane Rolland Feb 8 '13 at 9:24
1  
@TilT but you could then almost name it an Adapter Pattern, if you define an IPatient interface that the aggregated Patient must implement. But it would be an adapter that adpats two classes. –  Stephane Rolland Feb 8 '13 at 9:36

Your classes are allowing data to be modified by external forces via the public members. Since these are supposed to be Entity objects, enforce the immutability of the data. If you need to change a piece of the data, throw away the stale object and create a new one with the new data. Example:

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;
         }
     }
}

And, finally, I would use composition to do this:

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;
        }
    }
}
share|improve this answer
    
thanx for yous answer. but the problem is i cant change my patient class as u suggested. –  DevT Feb 9 '13 at 7:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.