ASSIGNMENT 2 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 20: Advanced Programming
Submission date 8/21/2022 Date Received 1st submission 8/21/2022
Re-submission Date Date Received 2nd submission
Student Name Le Hong Nhat Huy Student ID BSAF200003
Class PBIT16101 Assessor name Lam Hong Thanh
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. Build an application derived from UML class diagrams....................................................................................................................................... 4
1. Describe the scenario where you may need a pattern ..................................................................................................................................... 4
2. Diagram .............................................................................................................................................................................................................................. 5
3. Implementation ............................................................................................................................................................................................................... 7
4. Screenshots of the program running..................................................................................................................................................................... 19
P4 Discuss a range of design patterns with relevant examples of creational, structural and behavioral pattern types. .................. 24
1. Creational Design Pattern .......................................................................................................................................................................................... 24
2. Structural Design Pattern .......................................................................................................................................................................................... 34
3. Behavior Pattern ........................................................................................................................................................................................................... 42
4. Range of similar patterns ........................................................................................................................................................................................... 53
5. Usage of pattern............................................................................................................................................................................................................. 53
References .................................................................................................................................................................................................................................... 54
I. Build an application derived from UML class diagrams.
1. Describe the scenario where you may need a pattern
A design pattern is a collection of optimized, reusable solutions to common design software problems we encounter every
day. This is the solution to have been thought of and has been addressed in specific situations.
using templates Makes our products more flexible, and easier to change and maintain.
One thing that always happens in software development is the change in requirements. At this point, the system swells,
new features are added and performance needs to be optimized.
Design patterns provide optimized, proven solutions to problems in software engineering. Solutions in a generic form, speed
up software development by providing tested, tried-and-tested development models.
When you have any difficulty with solving problems, prototyping is the way to help you solve that problem instead of finding
the solution by yourself.
Help programmers can understand other people's code quickly (can be understood as the relationship between modules,
for example). All team members can easily communicate with each other to build the project together without taking much
time.
2. Diagram
Use Case Diagram
Image p3.1 use Case Diagram
Diagram + explanation
Image P3.2 Observer Pattern example
a student can only own 1 account and 1 account contains only one student's information, One manager can manage
many students, and many students are managed by one manager, one student can only register one account and
one account can only be registered by one student, an account can contain only one registration, and a register
makes up only one account, one manager is granted one account and one account is only granted to one manager
3. Implementation
Code + explanation:
First, I will instantiate a class named profile and value variables to use
Image P3.3 create a class profile
Next, I will create a class called StudentManager that will display processing logic such as adding, deleting, editing
and viewing student information. Then I create a constructor to store and initialize the values
P3.4 create a class studentManager
continue to create a function GenerateID() the task of this function is that every time a student is initialized, the
student ID will automatically increase. I will initialize a variable max with the default value = 1
and check if the student has been initialized and total number of students is greater than 0 then this max variable
will be equal to an arraylist containing the value of that ID. I keep using the forEach() function to iterate through
the array, if max is greater than the existing student ID, then max will be reassigned the value of that ID and each
time max will be incremented by a final value. about the variable max
P3.5 create a Generate() function
I will create the countStudent() function to count the number of students, create a counter variable that checks
the condition if listStudent has a value, then use the count function to count the total number of students in the
array and assign the value to the counter variable
P3.6 create a CountStudent() function
create a function enterStudent() to enter the message to the user and save the values entered by the user
P3.7 create a enterStudent() function
create a function FindbyID() that searches for student IDs initializes 1 variable Default value of SearchSesult is
null Checks if ListStudent has a value continue traversing the array checking IDs in array by current Id If yes then
assign student array member for the variable searchResult
P3.8 create a FindbyID() function
create updateStudent() function to update student information first use FindbyId function get ID parameter to
delete student based on that ID continue to check condition if exist values ie student information has been
initialization assigns valuable information to students
P3.9 create a updateStudent() function
create a deleteStudent() function that takes an ID parameter to delete a student create a bool variable isdeleted =
false pass the ID parameter to the finbyid function to check the condition if the student has been initialized, then
use the remove function to delete the student member and reassign the value to the variable isdeleted
P3.10 create a deletedStudent() function
create a showStudent() function to print out the values
P3.11 create a showStudent() function
we will instantiate a class to initialize manager information and printProfile() function to print manager
information
P3.12 create a class manager
Program screenshots
I will design a program table that includes selectable categories according to the switch case
Image P3.13 design a program table
Image P3.14 design a program table
Image P3.15 design a program table
4. Screenshots of the program running
Image P3.16 Screenshots of program running
I will add student information then I will choose option 1
Image P3.17 add student information
I will try the information display function to check if the information has been entered, I will choose option 4
Image P3.18 information display
The program has successfully added student information, then I will test the function of updating student
information I will change the student's age from 18 years old to 12 years old
Image P3.19 update information student
Successfully updated student information
Image P3.20 update information student
now i will try the delete student function i will choose option 3
Image P3.21 delete information student
check if the information has been deleted
Image P3.22 check information student
the list is empty so the student has been successfully deleted
To create the manager information, we choose menu number 4
Image P3.23 create the manager information
Link source code: https://drive.google.com/drive/folders/1jm-AmD4YuA3in9q5HpzZ18uE9cgizoPP?usp=sharing
P4 Discuss a range of design patterns with relevant examples of creational, structural and behavioral pattern types.
1. Creational Design Pattern
Pattern Singleton:
Singleton only needs to build on only one class, this class is divided into 2 groups of components:
Group security is created for instance only: The variable instance is private and static to ensure there is only 1 instance.
The Instance property provides an interface for accessing a single instance.
Business group: contains the class's method business attributes and characteristics.
Image P4.1 Singleton
Example:
Image P4.2 Singleton example
Image P4.3 Singleton example
Prototype Pattern
The object that contains the initial settings is called the prototype, and this way of designing code is called the
prototype pattern.
In short: In essence, the prototype pattern is a design that allows instantiation of an object by copying from an
existing object instead of using the new operator
The new object is a copy that may have the exact same or modified properties as the original object. In particular,
you can freely change the data on the cloned object without affecting the original object. If you just copy the object
in the usual way, with a programming language like Java, for example, when you change the data in the copy object,
the original will also be changed. Because you just copied the reference address only.
Image P4.4 prototype Pattern
Example
Image P4.5 Prototype Pattern example
Image P4.6 Prototype pattern example
Image P4.7 Prototype pattern example
Image P4.8 Prototype pattern example
Builder Pattern
The Builder Pattern divides the structure of a complex object from its small details, so the same process can create
objects with different details (thanks to different builders) Frequent use
Classes and objects that participate in this pattern include:
Builder (VehicleBuilder) implements an abstract interface by creating a part of the Product object
ConcreteBuilder (MotorCycleBuilder, CarBuilder, ScooterBuilder)
structuring and concatenating parts of a product by implementing the Builder interface
redefine and document the details it creates
Provide an interface that can return the details of the generated product
Director (Shop) creates object using Builder interface
Product (Vehicle) is the complex object created. ConcreteBuilder builds the product details internally and defines
the concatenation handling, including classes that define the details, and the interfaces for piecing together the
parts that produce the final result.
Image P4.9 Builder Pattern
Example:
Image P4.10 Builder Pattern example
2. Structural Design Pattern
Proxy Pattern
A proxy is a structural design pattern that provides an operational object as an alternative to a real service object used
by the client. A proxy takes client requests, does some work (access control, caching, etc) and then passes the request to
a service object.
The proxy object has the same interface as a service, which makes it interchangeable with a real object when passed to a
client.
Image P4.11 Proxy Pattern
Example:
Image P4.12 Proxy Pattern example
Image P4.13 Proxy Pattern example
Bridge Pattern
Bridge pattern is used when we want to separate an abtraction from its implementation so that the two can change
independently of each other. Bridge Pattern is a structural pattern (Structural Pattern) => Clearly shows the hierarchy
between the interface in both the interface and the implementing components. We are demonstrating the use of Bridge
Pattern through the following example where a circle can be drawn in different colors using the same abstract class
method but different bridge implementer classes .
Image P4.14 bridge pattern
Example:
Image P4.15 bridge pattern example
Image P4.16 bridge pattern example
Image P4.17 bridge pattern example
Image P4.18 bridge pattern example
Image P4.19 bridge pattern example
3. Behavior Pattern
Observer Pattern
Observer Pattern is one of the Patterns belonging to the Behavioral Group (Behavior Pattern).
It defines one-to-many dependencies between objects so that when an object changes state, all its dependencies are
notified and updated automatically.
Image P4.20 Observer Pattern
Example:
Image P4.21 Observer Pattern example
Image P4.22 Observer Pattern example
Image P4.23 Observer Pattern example
Image P4.24 Observer Pattern example
Image P4.25 Observer Pattern example
Image P4.26 Observer Pattern example
Image P4.27 Observer Pattern example
strategy pattern
Strategy pattern: In simple terms, this is a design pattern that helps you abstract the behavior (behavior, method,
function) of an object by giving implementations to different classes.
Image P4.28 strategy pattern
Example:
Image P4.29 strategy pattern Example
Image P4.30 strategy pattern Example
Image P4.31 strategy pattern Example
Image P4.32 strategy pattern Example
Image P4.33 strategy pattern Example
Image P4.34 strategy pattern Example
Image P4.35 strategy pattern Example
4. Range of similar patterns
I feel my model fits the Singleton pattern because my model is relatively small and can create many objects without
consuming memory because the constructor is private or protected. The purpose is to prevent other objects from
creating objects of the singleton class via the new keyword so that no matter how many students I create, I can
still save a lot of memory compared to other patterns
5. Usage of pattern
Advantages
The advantage of using the Singleton pattern is to save maximum memory compared to other patterns because
using a static method to initialize the Singleton, all calls to this method will be returned to the cache and can be be
accessed
Disadvantage
The Singleton pattern can hide bad design for instances when the components in the program know each other
well.
Singleton requires special handling in a multithreaded field, so that multiple threads do not create a Singleton
object more than once.
Singleton is often considered "Evil" by longtime programmers, because Singleton creates too many dependencies,
cannot use polymorphism, and easily creates bugs.
References
duythanhcse.wordpress.com, 2021. Advanced Programming. Dai Hoc Quoc Gia TPHCM - Truong Dai Hoc Kinh Te Luat ed. TP.HCM: Dai Hoc Quoc
Gia TPHCM.
Ngo, P. C., 2016. Lap trinh C# co ban den nang cao. Pham Cong Ngo ed. Ho Chi Minh: https://cuongquach.com/.
www.howkteam.vn, 2021. CSharp Và CSharp OOP. DH Bach Khoa ed. Ha Noi: HowkTeam.