Bifalgorithm post · Computer science one · Composition

How does composition allow objects to own instances of other objects?

How does composition allow objects to own instances of other objects? (Computer science one · Composition) — "Another important concept when designing classes…

Ideas in this flowchart

  • La composición en el diseño de clases
  • La composición es un mecanismo mediante el cual un objeto está formado por otros objetos.
  • Un objeto puede poseer una instancia de otro objeto.
  • La clase Student puede incorporar una fecha de nacimiento.
  • Una fecha de nacimiento contiene varios datos, como año, mes, día, hora o configuración regional.
  • Generalmente es preferible utilizar las clases de fecha y hora proporcionadas por el lenguaje.
  • PHP 5.2 introdujo el objeto DateTime, que permite representar y comparar fechas y horas.
  • Las clases definidas por el usuario también pueden poseer instancias de otras clases.
  • Student puede poseer una colección de objetos Course para representar el horario del estudiante.
  • La posesión de una colección de objetos suele denominarse agregación.
  • Surge la cuestión de quién debe crear las instancias de $dateOfBirth y $schedule.
  • El usuario externo crea una instancia de DateTime y la pasa al constructor.
  • El código externo proporciona una fecha como cadena y el constructor crea la instancia de DateTime.
  • El usuario crea un arreglo completo de instancias de Course y lo proporciona al constructor.
Feed/

How does composition allow objects to own instances of other objects?

No cover

Channel

Computer science one

1 page

"Another important concept when designing classes is composition. Composition is a mechanism by which an object is made up of other objects. One object is said to “own” an instance of another object. To illustrate the importance of composition, we could extend the design of our Student class to include a date of birth. However, a date of birth is also made up of multiple pieces of data (a year, a month, a date, and maybe even a time and/or locale). We could design our own date/time class to model this, but its generally best to use what the language already provides. PHP 5.2 introduced the DateTime object in which there is a lot of functionality supporting the representation and comparison of dates and time. We can take this concept further and have our own user-defined classes own instances of each other. For example, we could define a Course class and then update our Student class to own a collection of Course objects representing a student’s class schedule (this type of collection ownership is sometimes referred to as aggregation rather than composition). Both of these design updates beg the question: who is responsible for instantiating the instances of $dateOfBirth and the $schedule ? Should we force the “outside” user of our Student class to build a DateTime instance and pass it to a constructor? Should we allow the outside code to simply provide us a date of birth as a string and make the constructor responsible for creating the proper DateTime instance? Do we require that a user create a complete array of Course instances and provide it to the constructor at instantiation?"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.