Chapter 9 - Software Design - UML and Class Diagrams
Object: An item that has a hidden internal structure. This hidden structure is manipulated or accessed by messages sent by a user programmer to the object.
Message: Request sent to an object to obey one of its hidden methods
Method: An action that manipulates or accessed the internal state of the object. The implementation of this action is hidden from a programmer who sends messages to this object.
Instance variable: The data that represents the internal state of the object (properties/attributes)
Class: A class is a set of objects that share common attributes and behaviours
Classes and Instances
The assumption can be made about the behaviour of objects based on their position within the universe. All objects are instances of a class. The method invoked by an object in response to a message is determined by the class of the receiver. All objects of a given class have the same general attributes or instance variables. All objects of a given class use the same method in response to similar messages
An association is a relationship that exists between one or more objects/classes.
The association is graphically illustrated between two classes as a connecting line. Verb phrases descrive the relationship. Multiplicity defines the minimum and maximum number of occurences of one object/class for a single occurence of the related object/class.
Multiplicity can be defined at both ends of every relationship.
This is a high level overview. Most of the time, standard 3 segment UML Classes would be used (title, attributes, behaviours)
To avoid ambiguiuty, a small black arrow head could go next to "Places" to indicate the direction of the relationship. (See below). The black line between the rectangles is called an "Association"
|Means one and only one|
|Means zero or more|
|means zero or one|
one or more
|one min to four max (inclusive?)|
Labels, roles and navigability
A class may be attached to an assocaiation rather than directly to another class. Such a class is called an Association Class. It can haev data attributes and behaviour just like any other class.
They are used to hold information on each occurrence of a relationship.
From this example, it can't be told that
id is unique to the
student in their dealings with the
univeristy. The solution to this is a qualified association:
Some ID values will be occupied by at most one student, or it will still be available.
Inheritance implies that methods and/or attributes defined in a class can be inherited or reused by another class. It is also referred to as generalisation and specialisation.
Attributes and behaviours (methods) that are common to several types of classes are grouped into their own class, as a generalisation.
The subtypes of the generalised class are called specialisations.
Problems with copying an old class
The code must be re-tested, errors may be introduced during the copying and changes could become inconsistent later.
The new class is a specialised form of the parent but satisfies the specifications of the parent in all respects. EG, a
Window provides general window manipulation operations (move, resize, maximise, minimise, etc). A subclass
TextEditWindow inherits these, but adds facilities to allow the user to display and edit text information.
Exhibits the principle of substitutability.
Window can be replaced by
TextEditWindow in all instances.
The child modifies or overrides some of the behaviour provided by the parent. This is the opposite of sublcassing for specialisation. By overriding some functionality of the parent, the child becomes more specialised, and make the parent more generalised.
This is when the child class class adds new fucntionality without changing any inherited behaviour.
Whereas generalisation modifies (overrides) existing capabilities, extension simply adds new methods to create a subclass that extends the parent's functionality into a quite new application area. A form of specialisation, but with a significant new purpose.
As the functionality of the parent remains available and untouched, the principle of substitutability holds true.
This is when the child and parent exhibit an arbitrary hierarchical relationship. Two classes with similar implementations: one can be created from the other; the direction is unimportnat (the hierarchical relationship is therefore arbitrary)
For example, the code to control a mouse is nearly identical to that required for a graphics tablet. Select one to be the parent with the common code inherited by the other and device-specific code being overridden.
Guarantee that a common interface is maintained.
The parent and child classes implement the same methods. The parent can be a combination of implemented operations and operations that are deferred to the child classes. It is a special case of subclassing for specialisation but realisations of an incomplete abstract specification.
For example, in a snooker table simulation,
GraphicalObject. They all provide realisations for methods
Advantages and disadvantages of inheritance
No need to rewrite code that can be inherited, hence greater reliability and decreased maintenance cost.
Many users or even projects can share the same class. Multiple classes can inherit from a single class.
Consistency of interface
When multiple classes inherit from the same superclass, it can be assumed that the behaviour that they will inherit will be same the same in all cases. This can provide a handy way of accessing and/or adapting existing functionality.
Development and availability of class libraries
When constructed from reusable components, intial implementations can be developed more quickly.
A programmer who reuses a software component needs only to understand the nature of the content and its interface. Not background object processes.
Different objects respond to the same message in different ways. This enables the programmer to generate high-level reusable components that can be tailored to fit different applications.
Costs of interitance
Inherited methods which must deal with arbitrary subclasses are often slower than specialised code
Where one module inherits from another, there is coupling. IE, a dependency which reduces reusability of the module.
Message passing is normally (slightly) slower than traditional procedure calls.
Reduced program understanding
Overuse of inheritance can lead to a concept called the "yo-yo problem" - where several scans up and down the class hierarchy are required to understand the flow of control of a program. Complex hierarchies (and complex architectures generally) may obscure the source of particular functionality - making that functionality more difficult to adapt and maintain.
Overriding a superclass method
As well as inheriting methods from a superclass, a subclass can override a superclass method.
A method can't be overridden if it is
private. If it is
protected, it may be overridden. Similarly with
Where a class inherits from more than one class. Java does not allow this, but interfaces allow for a workaround.
Objects/classes can be made up of other objects/classes. (=aggregation)
For example, a book objects may contain several objects, including cover, table of contents, chapter and index objects. The chapter object may contain page objects, which in turn contains paragraph objects and so on.
By identifying aggregation relationships, we can partition a very complex object and assign behaviours and attributes to the individual objects within it. Multiplicity is also specified for aggregate relationships.
The 'whole' owns the instances of the parts. If the whole is destroyed, the parts are destroyed as well. Composite aggregation forms a tree structure. A single part cannot belong to more than one whole.
Aggregation vs inheritance
Aggregation relates to Instances, whereas Inheritance relates to Classes.
Types of object
Entity objects encapsulate business policy and data for:
- Logical structures - departments, sales
- Business documents - change requests, service requests
- Physical entities - equipment, staff
- Entity objects often encapsulate persistent information that is tracked by the system. For example, Year, Month, Day of sale
- Boundary objects handle interactions between the actors and the system. For example, Button, LCD
Control objects help realise use cases. For example,