B3.1.2 Construct a design of classes, their methods and behaviour.

B3.1.2 Construct a design of classes, their methods and behaviour. 
• Classes and their methods, based on application requirements 
• The use of unified modelling language (UML) class diagrams to represent class relationships, attributes and methods, to aid effective software design and planning

The big idea

Before you write code, it helps to design how the pieces of your program will fit together. In object-oriented programming, this means deciding what classes you need, what data they store, and what actions (methods) they can perform.

A good class design captures the real-world entities or logical components of your problem, organizes them clearly, and makes it easy to change or extend the program later. One of the most common tools for planning this is the UML class diagram, which shows the classes, their attributes, methods, and how they relate to each other.

From requirements to classes

If you’re designing an application:

  1. Read the requirements carefully – Identify the “nouns” (likely to become classes) and “verbs” (likely to become methods).
  2. Think about relationships – Some classes will contain other classes (composition) or extend others (inheritance).
  3. Decide on attributes – What data does each class need to store?
  4. Decide on methods – What actions should each class be able to perform?
  5. Check for clarity – Keep classes focused; avoid mixing unrelated responsibilities.

Example: Simple Library System

Requirements:

  • The library stores books.
  • Each book has a title, author, and availability status.
  • Library members can borrow and return books.
  • The library keeps a record of its members.

 

Step 1: Identify classes

  • Book – represents a single book.
  • Member – represents a library member.
  • Library – manages collections of books and members.

 

Step 2: Decide attributes and methods

Book

  • Attributes: title, author, is_available
  • Methods: borrow(), return_book()

Member

  • Attributes: name, borrowed_books
  • Methods: borrow_book(book), return_book(book)

Library

  • Attributes: books, members
  • Methods: add_book(book), add_member(member), find_book(title)

 

Step 3: Draw a UML class diagram

Notation basics:

  • Top box – class name
  • Middle box – attributes (often with visibility indicators like + for public or - for private)
  • Bottom box – methods

 

+-----------------+       +-----------------+
|     Library     |       |     Member      |
+-----------------+       +-----------------+
| - books         |       | - name          |
| - members       |       | - borrowedBooks |
+-----------------+       +-----------------+
| + addBook()     |       | + borrowBook()  |
| + addMember()   |       | + returnBook()  |
| + findBook()    |       +-----------------+
+-----------------+
        |
        | contains
        v
+-----------------+
|      Book       |
+-----------------+
| - title         |
| - author        |
| - isAvailable   |
+-----------------+
| + borrow()      |
| + returnBook()  |
+-----------------+

 

Step 4: Implement in code (simple example)

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.is_available = True

    def borrow(self):
        if self.is_available:
            self.is_available = False
            return True
        return False

    def return_book(self):
        self.is_available = True


class Member:
    def __init__(self, name):
        self.name = name
        self.borrowed_books = []

    def borrow_book(self, book):
        if book.borrow():
            self.borrowed_books.append(book)

    def return_book(self, book):
        if book in self.borrowed_books:
            book.return_book()
            self.borrowed_books.remove(book)


class Library:
    def __init__(self):
        self.books = []
        self.members = []

    def add_book(self, book):
        self.books.append(book)

    def add_member(self, member):
        self.members.append(member)

    def find_book(self, title):
        for book in self.books:
            if book.title == title:
                return book
        return None

 

Why UML is useful

  • Clarity before coding – Everyone on the team can see the structure.
  • Spot design issues early – Easy to rearrange boxes before committing to code.
  • Documentation – Serves as a visual reference for new team members.