0% found this document useful (0 votes)
175 views16 pages

Unit 20 Assignment 2

The program implements the builder design pattern to construct complex objects like houses and apartments. It includes classes for the Director, Builder interface, HouseBuilder, ApartmentBuilder and House. The Director executes the construction steps by calling methods on the builder object. This allows building houses or apartments by varying the concrete builder class but using the same building steps. The builder pattern avoids telescoping constructor parameters for complex objects.

Uploaded by

kcas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views16 pages

Unit 20 Assignment 2

The program implements the builder design pattern to construct complex objects like houses and apartments. It includes classes for the Director, Builder interface, HouseBuilder, ApartmentBuilder and House. The Director executes the construction steps by calling methods on the builder object. This allows building houses or apartments by varying the concrete builder class but using the same building steps. The builder pattern avoids telescoping constructor parameters for complex objects.

Uploaded by

kcas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ASSIGNMENT 2

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 20: Advanced Programming

Submission date 07 May 2022 Date Received 1st submission 07 May 2022

Re-submission Date Date Received 2nd submission

Student Name Pham Khac Thanh Phong Student ID GCH210005

Class GCH0905 Assessor name Do Thanh Tung

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:

Grade: Assessor Signature: Date:


Lecturer Signature:
Table of Contents
I. Introduction ......................................................................................................................................................................................... 5
II. Scenario analysis.................................................................................................................................................................................. 5
1. Scenario............................................................................................................................................................................................ 5
2. Diagram ............................................................................................................................................................................................ 6
III. Implementation ............................................................................................................................................................................... 7
1. Code ................................................................................................................................................................................................. 7
2. Program screenshots ..................................................................................................................................................................... 13
IV. Discussion....................................................................................................................................................................................... 15
1. Range of similar patterns ............................................................................................................................................................... 15
2. Usage of pattern ............................................................................................................................................................................ 15
V. References ......................................................................................................................................................................................... 16

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.

II. Scenario analysis


1. Scenario
A house contains many different aspects such as types of walls, the number of floors, types of roofs, and different backyard settings
such as trees, pool, garden, and more. In the traditional approach, the House constructor will be containing these parameters, which
ends up with a large number of parameters. Additionally, any additional parameter will require further expansion of this hierarchy.
The problem can be addressed through a builder pattern which allows the parameter to be extracted out of its own class and moved
to a builder object.
2. Diagram

Figure 1 - UML Diagram

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

Figure 8 - Program running

When running the program, the menu will be shown in the terminal. User can choose the selection on the menu.

Figure 9 – Build a House

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.

Figure 11 - Exiting the program

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):

• Abstract factory: The families of product objects.


• Builder: How a composite object gets created.
• Factory method: Subclass of an object that is instantiated.
• Prototype: Class of an object that is instantiated.
• Singleton: The sole instant of a class.

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.

You might also like