This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: Refactor your robot!

Summary

face Josiah Wang

You have now defined a RobotFactory class. This class is used to create multiple new Robots with randomly initialised names, position and location.

In this style of programming, you are delegating the responsibility of creating and initialising class instances to a Factory class. This follows the Single Responsibility Principle, which basically says that each class should only be responsible for one aspect of the program, rather than overloading them with too many tasks. This is a good way of thinking when designing your classes. It also helps to imagine each class focussing on doing its own thing, and at the same time collaborating with other classes to achieve a larger goal.

There are potentially more objects that can be created for the robot project, each with its own responsibility. We will come back to do this later!