Professional Documents
Culture Documents
1651 Assignment 2 gcs190844 Tran Doan Dang Khoi
1651 Assignment 2 gcs190844 Tran Doan Dang Khoi
ADVANCE
PROGRAMMING
Assignment 2
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
ASSIGNMENT 2 BRIEF
Qualification BTEC Level 5 HND Diploma in Computing
Assignment title Application development with class diagram and design patterns
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
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.
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).
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
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
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
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.
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
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.
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
Figure 14
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
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
Figure 19
Figure 20
Figure 21
Figure 22
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.
Figure 24
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
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.
Figure 28
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]