Lecture recording (Tuesday 3 September, 2024) here.
Lab recording (Thursday 5 September, 2024) here.
To start the course we will look at software design principles in general. These principles will be adhered to during our study of standard design patterns and design patterns for machine learning. Students are encouraged to take the tutorial on UML. We will look at two design patterns that we are already familiar with - the singleton design pattern and the factory design pattern. The singleton pattern ensures that a class has only one instance and provides a global point of access to it. The factory pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Assignment 1 - The Retail Store
Software design principles represent a set of guidelines that helps us to avoid having a bad design. there are 3 important characteristics of a bad design that should be avoided:
See also 10 Design Principles in Software Engineering:
Design patterns are used by experts to make their designs more flexible and reusable, so in studying them you tend to see:
Design patterns can be categorized into three main types: creational patterns, structural patterns, and behavioral patterns.
Creational patterns focus on object creation mechanisms, providing flexible ways to create objects while hiding the
complexities involved. They help ensure that objects are created in a manner that is suitable for the given situation.
Structural patterns deal with object composition and relationships between objects, focusing on class and object composition
to form larger structures. They help define how objects and classes can be combined to form flexible and efficient structures.
Behavioral Patterns focus on communication between objects and the distribution of responsibilities, describing patterns
of communication between objects to achieve specific behaviors. They help define how objects interact and fulfill their roles in
a flexible and maintainable way.
The Rationale
The singleton pattern is a design pattern that is used to ensure that a class has only one instance, and that this instance can be easily accessed throughout an application. The rationale for using the singleton pattern is to provide a single point of access to a shared resource, such as a database connection, a file system, or a network socket.
The UML
Here is the UML diagram for the singleton pattern:
Here are the components of the singleton design pattern:
Code Example - Do Something
The following code attempts to create or access one instance of the singleton class:
C++: Singleton.cpp.
C#: Program.cs.
Java: Factory.java.
Python: Python.py.
Common Usage
The following are some common usages of the singleton pattern:
Code Problem - The Database
We wish to write code that access a database. There can only be one database. The code must either
create the database, or use it if it has already been created. The following code allows only one instance of a database:
Database.h,
Database.cpp and
DatabaseMain.cpp.
The Rationale
The Factory pattern is a creational design pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the type of objects that will be created. The rationale for using the Factory pattern is to separate the creation of objects from their use in the code, which can simplify the code and make it more maintainable.
The UML
Here is the UML diagram for the factory pattern:
Here are the components of the Factory design pattern shown in the above UML diagram:
Code Example - Factory Pattern
The following is an example of a factory pattern to create one of three concrete objects:
C++: Factory.cpp.
C#: Program.cs.
Java: Factory.java.
Python: Python.py.
Common Usage
The following are some common usages of the factory pattern:
Code Problem - Toolbox
We wish to build a toolbox of many types of tools. The types of tools will be entered by the user, and are not known at compile time.
The following code fills a toolbox with screwdrivers and wrenches with the factory pattern:
ToolIf.h, the tool abstract class,
Hammer.h, the hammer concrete class,
Screwdriver.h, the screwdriver concrete class,
Toolbox.h, the toolbox,
ToolIf.cpp, the toolbox factory function.
ToolMain.cpp, the main function.
The following is a tutorial showing sample code written in C++ from SEP101 and SEP200 and their equivalent code in Java and C#.
SEP101 Week 1 - Hello World
The following is a simple hello world program.
C++: Hello.cpp
C#: Hello.cs
Java: Hello.java
SEP101 Week 2 - Student Scholarship
The following program demonstrates if-else and switch, input/output, and simple mathematical operations.
C++: StudentScholarship.cpp
C#: StudentScholarship.cs
Java: StudentScholarship.java
SEP101 Week 3 - Average Mark
The following program demonstrates loops.
C++: AverageMark.cpp
C#: AverageMark.cs
Java: AverageMark.java
SEP101 Week 5 - Pointers
The following program demonstrates pass by value and pass by address. Note that the Java program
does not handle pointers and does not work properly.
C++: Swap.cpp
C#: Swap.cs
Java: Swap.java
SEP101 Week 7 - Structures and Functions
The following program demonstrates structures (or classes) and functions.
C++: StudentMarks.cpp
C#: StudentMarks.cs
Java: StudentMarks.java
SEP101 Week 10 - Structures and Functions, Multiple Files
The following program demonstrates how C# and Java handle multiple file includes (they don't).
C++: Transaction.h,
Transaction.cpp,
TransactionMain.cpp.
C#: Transaction.cs,
TransactionMain.cs
Java: Transaction.java
TransactionMain.java
SEP200 Week 4 - Calculate Area
The following program demonstrates inheritance.
C++: CalculateArea.cpp
C#: CalculateArea.cs
Java: StudentScholarship.java
SEP200 Week 5 - Math Library
The following program demonstrates polymorphism.
C++: MathLib.cpp
C#: MathLib.cs
Java: MathLib.java
SEP200 Week 9 - Employee Information
The following program demostrates some of the container classes seen in C++'s' STL, in multiple files.
C++: EmployeeInfoSTL.h,
EmployeeInfoSTL.cpp,
EmployeeInfoSTLMain.cpp
C#: EmployeeInfoSTL.cs,
EmployeeInfoSTLMain.cs
Java: EmployeeInfoSTL.java,
EmployeeInfoSTLMain.java