You are on page 1of 24

lOMoARcPSD|14707295

1651 - Assignment 2 - GCS190844 - Trần Đoàn Đăng Khôi

Data Structure and algorihms (Trường Đại học FPT)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)
lOMoARcPSD|14707295

ADVANCE
PROGRAMMING
Assignment 2

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

STUDENT ID: GCS190844


STUDENT NAME: TRẦN ĐOÀN ĐĂNG
KHÔI
CLASS: GCS0903B
LECTURE: LÊ NGỌC THÀNH

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

ASSIGNMENT 2 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 20: Advanced Programming

Submission date Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name TRẦN ĐOÀN ĐĂNG KHÔI Student ID GCS190844

Class GCS09030B Assessor name LÊ NGỌC THÀNH

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
 

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Lecturer Signature:

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

ASSIGNMENT 2 BRIEF
Qualification BTEC Level 5 HND Diploma in Computing

Unit number and


Unit 2: Advanced Programming
title

Assignment title Application development with class diagram and design patterns

Academic Year 2018-2019

Unit Tutor Doan Trung Tung

Issue date 30 April 2019 Submission date 11 May 2019

Submission Format:

Format: The submission is in the form of an individual written report. This should be written in a concise,
formal business style using single spacing and font size 12. You are required to make use of
headings, paragraphs and subsections as appropriate, and all work must be supported with
research and referenced using the Harvard referencing system. Please also provide a
bibliography using the Harvard referencing system.
Submission Students are compulsory to submit the assignment in due date and in a way requested by the
Tutors. The form of submission will be a soft copy in PDF posted on corresponding course of
http://cms.greenwich.edu.vn/ together with zipped project files.
Note: The Assignment must be your own work, and not copied by or from another student or from
books etc. If you use ideas, quotes or data (such as diagrams) from books, journals or other sources, you
must reference your sources, using the Harvard style. Make sure that you know how to reference properly,
and that understand the guidelines on plagiarism. If you do not, you definitely get fail

Assignment Brief and Guidance:

Scenario: (continued from Assignment 1) Your team has shown the efficient of UML diagrams in
OOAD and introduction of some Design Patterns in usages. The next tasks are giving a demonstration
of using OOAD and DP in a small problem, as well as advanced discussion of range of design patterns.

Tasks:
Your team is now separated and perform similar tasks in parallel. You will choose one of the real
scenarios that your team introduced about DP in previous phase, then implement that scenario based
on the corresponding class diagram your team created. You may need to amend the diagram if it is
needed for your implementation. In additional, you should discuss a range of DPs related / similar to
your DP, evaluate them against your scenario and justify your choice.

In the end, you need to write a report with the following content:
 A final version of the class diagram based on chosen scenario which has potential of using DP.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

 Result of a small program implemented based on the class diagram, explain how you translate
from design diagram to code.
 Discussion of a range of DPs related / similar to your DP, evaluate them against your scenario
and justify your choice (why your DP is the most appropriate in that case).

Learning Outcomes and Assessment Criteria

Pass Merit Distinction

LO3 Implement code applying design patterns

P3 Build an application M3 Develop code that D3 Evaluate the use of


derived from UML class implements a design pattern design patterns for the given
diagrams. for a given purpose. purpose specified in M3.

LO4 Investigate scenarios with respect to design patterns

P4 Discuss a range of M4 Reconcile the most D4 Critically evaluate a


design patterns with appropriate design pattern range of design patterns
relevant examples of from a range with a series of
against the range of given
creational, structural and given scenarios.
scenarios with justification
behavioral pattern types.
of your choices.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Contents
LO3 Implement code applying design patterns....................................................................................................... 4
P3 Build an application derived from UML class diagrams.................................................................................... 4
Class Book.......................................................................................................................................................... 4
Class AptechBook............................................................................................................................................... 6
LO4 Investigate scenarios with respect to design patterns.................................................................................... 12
P4 Discuss a range of design patterns with relevant examples of creational, structural and behavioral pattern types.
................................................................................................................................................................................ 12
What is the Design Pattern?.............................................................................................................................. 12
Type of design patterns..................................................................................................................................... 13
Type 1: Creational - The Creational Design Pattern................................................................................... 13
Type 2: Structural - The Structural Design Pattern........................................................................................ 15
Type 3: Creational - The Behavioral Design Pattern.................................................................................. 16
References............................................................................................................................................................. 19

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

LO3 Implement code applying design patterns


P3 Build an application derived from UML class diagrams.
Class Book
This is a model book that includes properties such as Name, Author, Producer, YearPushlish, and Price.

Figure 1

This is the constructor used to initialize the value. When initializing the object with the "new" directive, it will
actually call this constructor. This is also the first block of code to be executed to initialize the object. This
constructor will be used to initialize a new Book object that includes the aforementioned properties.

Figure 2

This function is the Input() function, used to enter book information: book title, author name, publication name,
publication year and finally the price. I use virtual here to be able to change or add new property fields according

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

to the override method. I use Console.WriteLine(); to display text on the screen, followed by the function that
reads from the user's keyboard Console.ReadLine();

Figure 3

This is the Display() function, used to display book information, print data entered from the keyboard: book title,
author name, producer name, publication year and price. Similar to the Input() function above, I use virtual to be
able to change or add new property fields according to the override method. I use Console.WriteLine() to print
the value to the screen + data

Figure 4

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Class AptechBook
In this class, there are two new attribute fields: Language and Semeter; Besides, this class inherits from the Book
class which carries both the functionality and properties of the Book class.

Figure 5

The constructor of the AptechBook class, similar to the Book class, is also the first block of code to be executed
to initialize the object. This constructor will be used to instantiate a new Book object that includes the
aforementioned properties and the new properties of AptechBook: Language and Semester classes. I use the base
function to map two values of two classes together.

Figure 6

This is an Input() override function, based on the Book class and adding two new input fields, language and
semester.

Figure 7

Similar to the Display() override function, this function will print the Book's data and the new data field
displayed on the screen.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 8

This is the ShowMenu() function, in which there are options for the user to choose including: enter book data,
display book data, search for books by name, search for books by author and finally exit function .

Figure 9

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

In the Main() function, I instantiate a new list from the AptechBook() class. Call the ShowMenu() function above
then have the user enter the selection shown in the menu.

Figure 10

Case 1: this is the case to import book data into the library, first print to the screen asking the user the number of
book data they want to import, then conduct a for loop with i<sl. Initialize new book and call Input() function
inside AptechBook class.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 11

Case 2: use a for loop and count in the previously initialized list. Finally, print out the data value with the
Display() function.

Figure 12

Case 3: this is the function used to search for books by the name of the book. Similar to the Display() function, I
use the if() function to compare the input value and the value in the for loop, if the value is equal, I will use the
Display() function to display it.

Figure 13

Case 4: this function is similar to the function to find books by name, this function helps users to search books by
author's name

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 14

Initial user interface

Figure 15

Choose case 1: display and allow the user to enter data. Previously, the system would ask the user how many
books he wanted to import, if 2, he would enter 2 books continuously

Figure 16

Case 2: display book data on the screen

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 17

Case 3: Search for books by title. I searched for a book with the name “Advance Programming” and the system
returned correct.

Figure 18

Case 4: Search books by author name. Search for the author "Nguyen Van A" and the results returned

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 19

Case 5: Finally, exit the system

Figure 20

LO4 Investigate scenarios with respect to design patterns


P4 Discuss a range of design patterns with relevant examples of creational,
structural and behavioral pattern types.
What is the Design Pattern?
Design patterns are solutions at the design level to reoccurring issues that we software engineers face frequently.
This isn't code. It functions as an explanation of how to approach these issues and create a solution. Utilizing
these patterns is thought to be best practice because the final code is more readable as a result of the solution's
well- proven design. The majority of the examples from here on will be written in Java, an OOP language that is
frequently utilized in the creation and use of design patterns.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 21

Type of design patterns


These 26 fall within one of three categories:
Creational: These patterns are intended for the creation of classes. They can be patterns for creating classes or
patterns for creating objects.
Structural: These patterns are created with consideration for the composition and structure of a class. Most of
these patterns aim to improve the functionality of the class(es) in question without significantly altering its
makeup.
Behavioral: Depending on how one class interacts with others, certain patterns are created.
We will go through one fundamental design pattern for each categorized type in this post.

Figure 22

Type 1: Creational - The Creational Design Pattern


Singleton
One of the simplest design patterns is the singleton pattern. The creation of a separate DB connection for each
object could be expensive, thus there are occasions when we only need to have one instance of our class, for
instance, a single DB connection shared by several objects. Similar to this, an application can have a single
configuration manager or error manager that takes care of all issues rather than creating several managers.
The singleton pattern is a design pattern that limits the number of objects that can be instantiated by a class to one.
Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)
lOMoARcPSD|14707295

Let's examine some design approaches for putting such a class into practice. This work shouldn't be too
challenging if you understand static class variables and access modifiers well.
A single instance of a class and a single global access point to that object are the goals of the Singleton Design
Design, a Creational pattern. Calendar is a well-known instance of a class like this in Java, but you cannot create
an instance of it. In order to obtain the desired object, it also employs its own getInstance() method.
 A single instance of the class stored in a private static variable.
 It has a private constructor, making it exclusive to that location.
 A public static method that returns the class's sole instance.

Figure 23

Prototype
The cloning procedure is delegated by the Prototype pattern to the real objects that are being copied. All objects
that support cloning will have a common interface according to the pattern. This interface enables object cloning
without tying your code to the object's class. Such an interface typically just has one clone method.
All classes implement the clone method in a relatively similar way. The method generates a new object of the
current class while transferring all of the previous object's field values to the new one. Because most
programming languages allow objects to access the private fields of other objects that are members of the same
class, you can even copy private fields.
A prototype is a thing that allows for cloning. Cloning your objects may be a better option than subclassing if
your objects have dozens of fields and hundreds of possible configurations.
Creating a collection of things and configuring them in various ways is how it works. Instead of creating a new
object from beginning when you need one that is similar to the one you configured, you may just copy a
prototype.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 24

Type 2: Structural - The Structural Design Pattern


Adapter
The adapter pattern functions as a link between two disparate interfaces. Given that this design pattern combines
the functionality of two different interfaces, it falls within the structural design pattern category.
In this design, a single class is in charge of connecting the capabilities of separate or incompatible interfaces. An
actual example would be a card reader that serves as an adaptor between a laptop and a memory card. To read a
memory card using a laptop, you must put the memory card into a card reader and the reader into the laptop.
In the example below, an audio player device that can only play MP3 files wishes to utilize a more sophisticated
audio player capable of playing VLC and MP4 files. This is how we are using the Adapter pattern.
We have an AudioPlayer concrete class that implements the MediaPlayer interface, as well as a MediaPlayer
interface. By default, AudioPlayer can play audio files in the mp3 format.
We also have concrete classes that implement the AdvancedMediaPlayer interface and another interface called
AdvancedMediaPlayer. VLC and MP4 files can be played by these classes.
We intend to provide support for other file types in AudioPlayer. To achieve this, we developed a media adapter
class called MediaAdapter that implements the MediaPlayer interface and makes use of AdvancedMediaPlayer
instances to play the necessary formats.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 25

Composite
A group of items that are considered the same as a single instance of the same type of object is described by the
partitioning design pattern known as the composite pattern. To express part-whole hierarchies, a composite is
intended to "assemble" things into tree structures. It enables you to have a tree structure and assign tasks to each
node within the tree structure.
 Compose things into a tree structure to depict part-to-whole hierarchies, as stated by Gof. Individual
objects and object combinations can be treated consistently by the client using composite.
 Programmers frequently need to distinguish between a leaf-node and a branch when working with data
that has been organized into a tree. This increases the complexity and error potential of the code. An
interface that can handle both sophisticated and basic problems is the solution.
 A composite object is one that was created using object-oriented programming as a composition of one
or more functionally equivalent objects. A "has-a" relationship between the objects is what this is.
The key concept is that you can manipulate a single instance of the object just as you would manipulate a group
of them. The operations you can perform on all the composite objects often have a least common denominator
relationship.

Figure 26

Type 3: Creational - The Behavioral Design Pattern


Strategy

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

A class's behavior or algorithm can be altered at runtime under the strategy pattern. The category of behavior
pattern includes this kind of design pattern.
In the strategy pattern, we design objects that represent different strategies and a context object whose behavior
changes depending on the strategy object. The context object's execution algorithm is modified by the strategy
object.
A concrete strategy class that implements the Strategy interface will be created along with a Strategy interface
that defines an action. A class that employs a strategy is context.
Our demo class, StrategyPatternDemo, will use Context and strategy objects to show how Context behavior
changes depending on the strategy it deploys or employs.

Figure 27

Visitor
One of the behavioral design patterns is the visitor pattern. It is utilized when we need to carry out an operation
on a collection of objects that share a common type. We can relocate the operational functionality from the
objects to another class with the aid of the visitor pattern.
The visitation pattern is divided into two sections:
 Every element in the data structure that is visitable has a method named Visit() that is implemented by
the visitor and is called by classes that provide Accept() methods that accept visitors.
 Client: The Client class is a consumer of the visitor design pattern's class members. It has access to the
data structure objects and can provide them instructions to accept a Visitor so that the necessary
processing can be carried out.
 An interface or abstract class called "Visitor" is used to declare the visit procedures for all varieties of
visitable classes.
 ConcreteVisitor: All visit methods described in the abstract visitor must be implemented for each type of
visitor. There will be several operations that each Visitor will be in charge of.
 This interface, which declares the accept operation, is accessible. This is the point of entry that allows a
visitor object to "visit" an object.
 ConcreteVisitable: These classes define the accept action and implement the Visitable interface or class.
This object receives the visitor object via the accept operation.

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

Figure 28

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)


lOMoARcPSD|14707295

References
freecodecamp (2022). Design Pattern. [Online]. Freecodecamp. Available at:
https://www.freecodecamp.org/news/the-basic-design-patterns-all-developers-need-to-know/ [Accessed October
25, 2022]
GeeksforGeeks (2022). Singleton Design Pattern. [Online]. GeeksforGeeks. Available at:
https://www.geeksforgeeks.org/singleton-design-pattern/ [Accessed October 25, 2022]
Clone (2022). Prototype. [Online]. Refactoring. Available at: https://refactoring.guru/design-patterns/prototype
[Accessed October 28, 2022]
Tutorial (2022). Adapter. [Online]. Tutorialspoint. Available at:
https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm [Accessed October 28, 2022]
GeeksforGeeks (2022). Composite Design Pattern. [Online]. GeeksforGeeks. Available at:
https://www.geeksforgeeks.org/composite-design-pattern/ [Accessed October 28, 2022]
Tutorial (2022). Strategy. [Online]. Tutorialspoint. Available at:
https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm [Accessed October 28, 2022]

Downloaded by Vo Huynh Dang Khoa (FGW HCM) (khoavhdgbs210751@fpt.edu.vn)

You might also like