Computer Science › Section H · Software engineering › Topic H.5

Tools of the trade: UML and design patterns

Before a large system is built, its designers need ways to picture it, share it and check it. Diagrams, data dictionaries, version control and design patterns are the everyday tools that let a team agree on a design before and while writing the code.

Why software engineers draw

A design held in one person's head cannot be reviewed, divided among a team or explained to a client. Software engineers therefore record designs in standard notations, so that anyone trained in the notation reads a diagram the same way. Some tools date from the decades when imperative design dominated; the structure chart from "Modularity: coupling and cohesion" is one, and it fits only function-based designs. Others were built for object-oriented design. All of them help designers think clearly and help clients, analysts and programmers communicate without misunderstanding.

Dataflow diagrams and data dictionaries

A dataflow diagram follows the data rather than the actions. Arrows show data moving, processes mark where it is transformed, and boxes show where it comes from or is stored. For a school canteen, a pupil's card number and order flow into a Process order step, which reads the account file and sends an updated balance back. Wherever data paths merge, split or change form, processing must happen, which helps identify the functions needed. A data dictionary is a central record of every data item: its name, valid values, storage place and the modules that use it. Building one exposes surprises early, such as a pupil number with leading zeros that must be stored as text.

Version control

Large projects keep their code in a version control system, which records every change: what changed, who made it, when and why. Git, created by Linus Torvalds in 2005 to manage Linux development, is now the most widely used. Each developer works on a local copy, records changes as commits, and merges them with other people's work. Branches let a feature be developed separately and merged when ready. If a change causes trouble, the project can be returned to an earlier version. Version control holds documentation, configuration and build instructions as well as code.

UML and use case diagrams

The Unified Modeling Language (UML) is a standard family of diagrams for object-oriented design. Grady Booch, James Rumbaugh and Ivar Jacobson created it in the mid-1990s by merging their earlier notations, and the Object Management Group adopted it as a standard in 1997. Of its many diagram types, three are met most often. A use case diagram shows a system from the outside. A rectangle marks the system boundary; ovals inside are use cases, things the system does for users, such as Borrow book; stick figures outside are actors, the roles that interact with it. An actor may be another system, such as a payment service.

Class diagrams

A class diagram shows a design's static structure. Each class is a rectangle with three compartments: name, attributes and methods. A line between classes is an association, which may carry a label such as borrows. Numbers at each end, called multiplicities, say how many objects can be linked: 1 means exactly one, 0..1 at most one, * any number including zero, and 1..* at least one. Associations are therefore one-to-one, one-to-many or many-to-many. A line ending in a hollow triangle shows generalisation: the triangle touches the more general class, and the class at the other end is a specialised version that inherits its features.

Worked example: reading a class diagram

A library diagram links Member to Copy with the label borrows, writing 0..5 at the Copy end and 0..1 at the Member end. Copy is linked to Book with 1 at the Book end and 1..* at the Copy end. 1. Each member may borrow 0 to 5 copies at a time. 2. Each copy has at most one borrower at a time. 3. Each copy belongs to exactly one book, and every book has at least one copy. 4. So Member to Copy is one-to-many. 5. With 120 copies and 30 members, the most copies on loan is the smaller of \(30 \times 5 = 150\) and 120, which is 120. With 20 members it is \(20 \times 5 = 100\).

Sequence diagrams and CRC cards

Class diagrams show structure, not events. For behaviour, UML has interaction diagrams, most often the sequence diagram. Each participating object or actor has a box at the top with a dashed lifeline running down. Arrows between lifelines are messages, usually method calls, and time runs downwards; dashed arrows show returns. A frame labelled sd holds the diagram, and boxes called combined fragments mark repetition, loop, or alternatives, alt, each guarded by a condition in square brackets. Designers also test designs with CRC cards, one index card per class listing its name, responsibilities and collaborators; team members act out a scenario, each playing a class, and gaps soon show.

Design patterns

A design pattern is a proven, reusable solution to a problem that keeps recurring in software design, described generally so it can be adapted to each case. The idea came from the architect Christopher Alexander, whose 1977 book A Pattern Language catalogued recurring solutions in buildings and towns. In 1994 Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, often called the Gang of Four, published 23 software patterns in three groups: creational, for making objects; structural, for combining them; and behavioural, for dividing responsibilities. Patterns give designers a shared vocabulary, and good ones keep coupling low and cohesion high.

Worked example: the Adapter pattern

An app expects every temperature sensor to have a method celsius(), but a ready-made class, OldSensor, only offers read_fahrenheit(), which returns 77.0. The Adapter pattern wraps the old object in a new one with the expected interface. 1. Python code: class CelsiusAdapter: with def __init__(self, sensor): self.sensor = sensor. 2. Add def celsius(self): return (self.sensor.read_fahrenheit() - 32) * 5 / 9. 3. Use it: s = CelsiusAdapter(OldSensor()), then print(s.celsius()). 4. Trace: \((77.0 - 32) \times \frac{5}{9} = 25.0\), so the output is 25.0. 5. Check the design: OldSensor was not edited, and the app talks only to celsius(), so the two stay loosely coupled.

More patterns, and a warning

The Decorator pattern adds responsibilities by wrapping an object in another with the same interface: a text stream can be wrapped by a compressing stream, then an encrypting one, giving any combination without a class for each. The Observer pattern notifies many objects automatically when one changes, as when every open view of a spreadsheet updates after a cell is edited. Patterns also teach caution about inheritance, which ties a subclass tightly to its parent. If a delivery app's Driver class inherits from Employee, hiring freelance drivers later forces a redesign. The Gang of Four advised preferring composition, one object holding another, when a relationship might change.

Common mistakes to avoid

First, reading multiplicities at the wrong end: the number next to a class says how many of that class each object at the other end is linked to. Second, expecting a class diagram to show the order of events; sequence diagrams do that. Third, drawing an actor inside the system boundary. Fourth, treating patterns as finished code to paste in rather than outlines to adapt. Fifth, using inheritance just to reuse code when the relationship may change.

Key takeaways

Standard notations let teams record, review and communicate designs. Dataflow diagrams follow data through processes; data dictionaries define every data item. Version control systems such as Git record every change. UML, standardised in 1997, includes use case diagrams for the outside view, class diagrams with associations, multiplicities and generalisation, and sequence diagrams with lifelines, messages and loop and alt fragments. CRC cards check designs by role play. Design patterns, inspired by Christopher Alexander and catalogued by the Gang of Four in 1994, include Adapter, Decorator and Observer.

Flashcards

Flashcard

Dataflow diagram

Diagram of data moving between sources, processes and stores

Follows the data, not the actions.

Test your understanding

Question 1 of 5

A class diagram links Teacher to Course with 1 at the Teacher end and 1..* at the Course end. What does it say?

Question 2 of 5

Which UML diagram best shows the order in which objects call each other's methods while a customer pays?

Question 3 of 5

A library offers a method get_tempF(), but your code expects get_temp_c(). You write a small class that holds the library object and converts. Which pattern is this?

Question 4 of 5

In a use case diagram for an online shop, where is the external payment service drawn?

Question 5 of 5

A delivery app's Driver class inherits from Employee. Why might designers prefer composition instead?

← Previous topicNext topic →