Unit 20 Assignment 2
Unit 20 Assignment 2
Submission date 07 May 2022 Date Received 1st submission 07 May 2022
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Grading grid
P3 P4 M3 M4 D3 D4
Summative Feedback: Resubmission Feedback:
Table of Figures
Figure 1 - UML Diagram ............................................................................................................................................................................. 6
Figure 2 – Director ...................................................................................................................................................................................... 7
Figure 3 – House ......................................................................................................................................................................................... 8
Figure 4 – ApartmentBuilder ...................................................................................................................................................................... 9
Figure 5 – HouseBuilder ............................................................................................................................................................................ 10
Figure 6 - Builder interface ....................................................................................................................................................................... 11
Figure 7 – Program .................................................................................................................................................................................... 12
Figure 8 - Program running ....................................................................................................................................................................... 13
Figure 9 – Build a House ........................................................................................................................................................................... 13
Figure 10 - Build an apartment ................................................................................................................................................................. 14
Figure 11 - Exiting the program ................................................................................................................................................................ 14
I. Introduction
Builder is a creational design pattern that helps a user to construct complex objects step by step. The report will display builder
pattern and the scenario for the implementation of builder pattern. In the conclusion, the usage as well as the comparison with
other creational patterns are written in the report.
The figure above shows an example of a House Builder diagram. There are four classes including Director, Builder interface,
HouseBuilder, ApartmentBuilder and House. A house contains many factors; therefore, it has been extracted into a separate house
builder class instead of a House constructor with a large number of parameters.
The HouseBuilder and ApartmentBuilder classes include a set of methods for assembling various components of a house, such as
floors, roof, and backyard. The director is created for executing the construction steps in a particular order. The result will be sent to
the director through the makeHouse() operation and displayed on the screen.
III. Implementation
1. Code
Figure 2 – Director
The figure above shows the Director class. The Director is only responsible for carrying out the building steps in a specific order. It
comes in handy when making things to a certain order or configuration. For example, in the makeHouse() function the builder is
called to override several functions, the order will be the floors to the garage. The first function will be executed first and all the way
to the last.
Figure 3 – House
Figure 3 shows the House class. This class contains different parts of a complex product such as a house, it can include a pool, roof,
area, and floor. This class must include functions that have been declared in the Builder interface. At the end of the class, the Print()
function is included to display the result.
Figure 4 – ApartmentBuilder
The figure above shows the ApartmentBuilder class of this project. This class is inherited from IBuilder interface; therefore, it must
override functions in the Builder interface. However, several components of an apartment are default, for example, an apartment
only has one floor, so this component must always be one. In the end, the class will return the result of the apartment in getHouse()
function.
Figure 5 – HouseBuilder
The concrete builder class is shown in figure 4, which is called the house builder. This class is inherited from the builder interface,
which must include functions in the builder interface such as setFloors, and setArea. In the end, there must be a getHouse function
that returns the result to its inherited class. A builder instance is usually expected to be ready to start building another product after
giving the final result to the client. That is why the house is saved inside a result variable and the function will return that result, and
the previous house is deleted.
Figure 6 - Builder interface
The Builder interface defines methods for constructing the various components of Product objects. As the figure above has shown,
the Builder interface defines several functions that will be used for constructing a house.
Figure 7 – Program
In the program class, the client code builds a builder object, passes it to the director, and starts the building process. The builder
object is used to acquire the final result. In addition, the menu which separates from building a house and building an apartment is
included in this project. User can also exit the program and if they input an invalid number, the program will send an invalid choice
on screen.
2. Program screenshots
When running the program, the menu will be shown in the terminal. User can choose the selection on the menu.
The house builder is shown in figure 7. The clients will enter several components information of a house in each line. The result will
be sent to the director, and the director will show the similar information as the client inputs on the screen.
Figure 10 - Build an apartment
The build an apartment is shown on figure 10. User don’t need to enter many components as a house, and the information will show
the result with default information on the terminal.
When user exit from the program, the terminal will be shown as figure 11.
IV. Discussion
1. Range of similar patterns
The builder pattern is in the creation pattern which has several patterns such as singleton, prototype, and more. In detail, the other
patterns are included (Gamma, Helm, Johnson and Vlissides, 1994):
Since the scenario is building a house that has many different components, the builder pattern is most suitable for this project. The
pattern provides a set of subclasses to cover all combinations of the components in a house. Other creation patterns require a
constructor with many components or parameters. In most cases, several parameters are not required, therefore, the input could be
troublesome.
2. Usage of pattern
Advantages Disadvantages
Objects can be constructed step-by-step, delay construction
steps, or run steps recursively.
The same construction code can be reused when building The complexity of the code will increase since the pattern
various representations of products. requires creating multiple new classes.
Complex construction code can be isolated from the business
logic of the product.
V. References
Gamma, E., Helm, R., Johnson, R. and Vlissides, J., 1994. Design Patterns Elements of Reusable Object-Oriented Software. 1st ed. New
York: KevinZhang, p.43.