Section 12 Chapter 68 - Object Oriented Design Principles Flashcards
Aggregation
Occurs when a class is a container of other classes but these contained classes can exist without their container. The container is said to be an aggregation of its contained objects
Composition
Occurs when a class is a container of other classes and these contained objects cannot exist without their container. If the container is destroyed then so are all the objects contained in it. The container is said to be a composition of its contained objects
UML symbol for aggregation
Line between two objects with a white diamond at the container
UML symbol for composition
Line between two objects with a black diamond at the container
Polymorphism
A pattern in OOP where classes can have different functionality while sharing a common interface, commonly achieved by child classes overriding the methods of their super class. It can also be described as the ability of a programming language to process objects differently depending on their class
Overriding
Defining a method with the same name and arguments as that of a method in the parent class, when the class in question inherits from the parent class. If the method of this name is called from an instance of the child class, the code for the overriding method will be executed rather than the code of the parents method
Why OOP is used (6)
- Forces designers to go through more extensive planning
- Due to encapsulation, individual classes can be tested independently
- Abstraction: Once a class is created the knowledge of how it is exactly implemented is not needed
- New objects can be created without affecting old ones
- Re usability since basic objects (such as a graph or a tree) can be used for many different things
- Easier to maintain as errors tend to be more localised
“Favour composition over inheritance”
Composition is generally preferred to inheritance since it allows greater flexibility, since composition is a less rigid relationship. For instance if classes B and C both inherit from class A, it would then not be possible to create a class D which is a combination of classes B and C using only inheritance
“Programming to an interface”
In the case that many, often unrelated, classes need to be able to respond to the same messages it can be useful to implement an interface. An interface is a collection of abstract methods that classes can implement and then define appropriately. Polymorphism can be used in this case since all these classes will have a common set of methods.
“Encapsulate what varies”
A strategy used to reduce maintenance. When something changes in a program (for instance in different specifications) the part that changes should be encapsulated so that only the inner workings of that part of the program need to change and not the rest of the program and how they communicate.
Class diagram sign for public
+
Class diagram sign for private
-
Class diagram sign for protected
#
How a class in a class diagram is set out
Name
Attributes
Methods