Bifalgorithm post · Computer science one · Common Methods

What is composition in object-oriented class design?

What is composition in object-oriented class design? (Computer science one · Common Methods) — "Another important concept when designing classes is composition…

Ideas in this flowchart

  • La composición es un concepto importante al diseñar clases.
  • La composición permite que un objeto esté formado por otros objetos.
  • En la composición, un objeto posee una instancia de otro objeto.
  • La clase Student puede ampliarse para incluir una fecha de nacimiento.
  • Una fecha de nacimiento contiene varios datos: año, mes, fecha y posiblemente hora o configuración regional.
  • Es preferible utilizar las funcionalidades de fecha y hora proporcionadas por el lenguaje.
  • PHP 5.2 introdujo el objeto DateTime para 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 que represente el horario del estudiante.
  • La posesión de una colección de objetos se denomina a veces agregación.
  • Las actualizaciones de diseño plantean quién debe crear las instancias de dateOfBirth y schedule.
  • El usuario externo crea una instancia de DateTime y la pasa al constructor.
  • El usuario proporciona una fecha como texto y el constructor crea la instancia DateTime adecuada.
  • El usuario crea una matriz completa de instancias de Course y la proporciona al constructor.
Feed/

What is composition in object-oriented class design?

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.