OOP composition

I have a question about the composition of the PLO.

Let's say a mother has 0 or plus children, and a child has one and only one biological mother.

To illustrate this, I did the following:

public class Mother : ObservableObject
{
    // [...]

    ObservableCollection<Child> Children {get; set;}
}

public class Child : ObservableObject
{
    public Child(Mother mother)
    {
        this.Mother = mother;

        // Adding the child to the mother children collection
        mother.Children.Add(this);
    }

    public Mother Mother {get; set;}
}

      

but I'm wondering if it's okay to add the child to the mother's collection, or should I go with the following:

Mother mother = new Mother();

Child child = new Child(mother);
mother.Children.Add(child);

      

Thank:)

+3


source to share


3 answers


I would prefer,



public class Mother : ObservableObject
{
    // ...

    public Child GiveBirth()
    {
        var newBorn = new Child(this);
        this.Children.Add(newBorn);
        return newBorn;
    }

    // ...
}

      

+5


source


I think the simulation doesn't work a bit. A Mother

and a are Child

semantically related to each other, but they are instances of the same object. They both are Person

.

Creation Person

is an operation performed Person

. So it Person

shouldn't have a public constructor, but rather a factory method that takes care of this logic. Something like that:

public class Person : ObservableObject
{
    private Person()
    {
        Children = new ObservableCollection<Person>();
    }

    public Person Mother { get; private set; }
    public ObservableCollection<Person> Children { get; private set; }

    public Person Procreate()
    {
        var child = new Person();
        child.Mother = this;
        this.Children.Add(child);
        return child;
    }
}

      

This simulation is still a little limited, for example we're talking about useless reproduction here. Therefore, we are not modeling people yet. Perhaps we need to add a father?

public class Person : ObservableObject
{
    private Person()
    {
        Children = new ObservableCollection<Person>();
    }

    public Person Mother { get; private set; }
    public Person Father { get; private set; }
    public ObservableCollection<Person> Children { get; private set; }

    public Person Procreate(Person father)
    {
        var child = new Person();
        child.Mother = this;
        child.Father = father;
        this.Children.Add(child);
        father.Children.Add(child);
        return child;
    }
}

      

We want to add some checking for zeros and which of course not. Now we have also found that we need to indicate gender groups. (While family structures can vary considerably, the act of creating a person is fairly well established.) So we can continue to add features like this. We may indeed subclass them at some point, but those subclasses are likely to become mostly semantic end-to-end objects with hard-coded defaults for that superclass Person

.



But just for fun, try adding floors ...

public class Person : ObservableObject
{
    private Person(Sex gender, Person mother, Person father)
    {
        // TODO: Check for null mother and father
        this.Gender = gender;
        this.Mother = mother;
        this.Father = father;
        Children = new ObservableCollection<Person>();
    }

    public Sex Gender { get; private set; }
    public Person Mother { get; private set; }
    public Person Father { get; private set; }
    public ObservableCollection<Person> Children { get; private set; }

    public Person Procreate(Person father)
    {
        // TODO: Check for null father, confirm gender of father
        var child = new Person(PickRandomGender(), this, father);
        this.Children.Add(child);
        father.Children.Add(child);
        return child;
    }

    private Sex PickRandomGender() { /.../ }

    public enum Sex
    {
        Female,
        Male
    }
}

      

Okay, that was fun. Removed a bit by moving some logic to the constructor. But now there is another problem ... fathers can reproduce. It sounds like it hurts. Now it looks like we're ready to subclass:

public class Person : ObservableObject
{
    protected Person(Sex gender, Person mother, Person father)
    {
        // TODO: Check for null mother and father
        this.Gender = gender;
        this.Mother = mother;
        this.Father = father;
        Children = new ObservableCollection<Person>();
    }

    public Sex Gender { get; private set; }
    public Person Mother { get; private set; }
    public Person Father { get; private set; }
    public ObservableCollection<Person> Children { get; private set; }

    protected Sex PickRandomGender() { /.../ }

    public enum Sex
    {
        Female,
        Male
    }
}

public class Woman : Person
{
    // TODO: Override Gender with a hard-coded value

    public Person Procreate(Person father)
    {
        // TODO: Check for null father, confirm gender of father
        var child = new Person(PickRandomGender(), this, father);
        this.Children.Add(child);
        father.Children.Add(child);
        return child;
    }
}

      

(Should we also subclass Man

?) Semantically seems cleaner, but are there any male-specific operations or attributes that are not shared by women? Possibly, but our models are not detailed yet.)

Looking back, the classes Mother

and Child

appear to be limited and short-sighted at the moment. A woman is not necessarily a mother, and all people are children. As you can imagine, there are many possibilities to add to this system. But, following the same general process of building such a domain, it should take that into account.

+2


source


Let's say you have a building named class. This class building has a function called BuildRooms (). There is another classroom that has functions to make rooms. You create Room objects like r1, r2, r3, etc. Now this building has 3 rooms. We can open doors and close doors using methods inside the Room class. The class building consists of rooms. This means that this building has 3 rooms. Write the code in whatever language you prefer. This is a composition. The building has rooms, so there is a Has-A relationship.

    class building{

    void make_rooms()
    {
        room r1=new room(), r2=new room();
        r1.open();
        r2.close();
    }
}
class room{
    void open()
     {

     }
      void close()
      {

       }    

    }

      

0


source







All Articles