1651 - Vo Nguyen Duy Nam - GCS200888 - Assignment Brief 2
1651 - Vo Nguyen Duy Nam - GCS200888 - Assignment Brief 2
1651 - Vo Nguyen Duy Nam - GCS200888 - Assignment Brief 2
1
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
2
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).
P3 Build an application derived M3 Develop code that implements a D3 Evaluate the use of design
from UML class diagrams. design pattern for a given purpose. patterns for the given purpose
specified in M3.
P4 Discuss a range of design M4 Reconcile the most appropriate D4 Critically evaluate a range of
patterns with relevant examples design pattern from a range with a design patterns against the range
of creational, structural and series of given scenarios.
of given scenarios with justification
behavioral pattern types.
of your choices.
3
TABLE OF CONTENTS
INTRODUCTION......................................................................................................................................... 1
LO3 IMPLEMENT CODE APPLYING DESIGN PATTERNS............................................................................ 1-5
P3 BUILD AN APPLICATION DERIVED FROM UML CLASS DIAGRAMS..................................................... 1-5
LO4 INVESTIGATE SCENARIOS WITH RESPECT TO DESIGN PATTERNS.................................................... 6-
P4 DISCUSS A RANGE OF DESIGN PATTERNS WITH RELEVANT EXAMPLES OF CREATIONAL, STRUCTURAL
AND BEHAVIORAL PATTERN TYPES. ........................................................................................................ 6-
1. What Is Design Pattern.......................................................................................................................... 6
2. When Use Design Patterns.................................................................................................................... 6
3. Categorization Of Patterns ....................................................................................................................6
3.1 Creational Pattern ...............................................................................................................................7
3.1.1 Factory pattern................................................................................................................................. 7-8
3.1.2 Abstract Factory Pattern...................................................................................................................9
3.1.3 Builder Pattern..................................................................................................................................9
3.1.4 Singleton Pattern.............................................................................................................................. 9
3.1.5 Prototype Pattern ............................................................................................................................ 9
3.2. Structure Patterns...............................................................................................................................9
3.2.1 Adapter Pattern................................................................................................................................ 10
3.2.2 Bridge Pattern................................................................................................................................... 11
3.2.3 Composite Pattern............................................................................................................................ 13
3.2.4 Decorator Pattern............................................................................................................................. 13
4. Flyweight Pattern...................................................................................................................................13
4.1 Behavior Patterns.................................................................................................................................13
4.1.1 Chain-of-responsibility Pattern ........................................................................................................14
4.1.2 Command Pattern.............................................................................................................................14
4.1.3 Observer Pattern ..............................................................................................................................14
4.1.4 State Pattern..................................................................................................................................... 15
Strategy Pattern ........................................................................................................................................ 15
REFERENCES...............................................................................................................................................16
ASSIGNMENT 2 ANSWERS
Introduction
Based on the class diagram created in assignment 1, in this assignment I will build an application
derived from that UML class diagram. Then, I will cover a series of design patterns with relevant examples
of creative, structural, and behavioral patterns.
My report includes two parts:
• Build an application derived from UML class diagram.
• Design patterns.
LO3 IMPLEMENT CODE APPLYING DESIGN PATTERNS
P3 BUILD AN APPLICATION DERIVED FROM UML CLASS DIAGRAMS.
Page 1
Class IPersonManage
- This is a interface class has main functionalities (Add, update, delete), with Inheritance of class
StudentManage and LectureManage.
Class PersonMenuFactory
- This class is a Factory Method, has a realization relationship with Class IPersonManage,
StudentManage and LectureManage. This class run when user chose manage student or lecture it will
create a new list.
Page 2
Class StudentManage
- Association with Class Student it use a data type is Student(String stdID,String stdName, String
stdMajor),with Generalization of class IPersonManage and overriding all method of it.. It has main
functionalities (Add, update, delete) to manage Student
Class Student
- Association with Class StudentManage allow it use data type Student and Storage Student data.
Page 3
Class LectureManage
- Association with Class Lecture it use a data type is Lecture(String lectureID,String lectureName,
String lectureMajor),with Generalization of class IPersonManage and overriding all method of it. It has
main functionalities (Add, update, delete) to manage Lecture.
Class Lecture
- Association with LectureManage allow it use data type Lecture and Storage Lecture data.
Page 4
Class MainMenu
- This class has a Realization relationship with class PersonMenuFactory, show menu to user chose
manage student or lecture then run class PersonMenuFactory.
Class Program
-This class has a Realization relationship with class Main menu, it run class Main menu.
Page 5
LO4 INVESTIGATE SCENARIOS WITH RESPECT TO DESIGN PATTERNS
Page 6
The below table shows the list of patterns under their respective categories:
Page 7
When Test is executed, the result is:
Page 8
3.1.2. Abstract Factory Pattern
Definition: The Abstract Factory pattern is a creational pattern which is related to the Factory Method
pattern, but it adds another level of abstraction.
When to use:
The Abstract Factory is often employed when there is a need to use different sets of objects and where the
objects could be added or changed sometime during the lifetime of an application.
Benefits:
Use of this pattern makes it possible to interchange concrete classes without changing the code that uses
them, even at runtime.
3.1.3. Builder Pattern
Definition:
The Builder pattern can be used to ease the construction of a complex object from simple objects. The
Builder pattern also separates the construction of a complex object from its representation so that the
same construction process can be used to create a different composition of objects. Related patterns
include Abstract Factory and Composite.
When to use:
• When the algorithm for creating a complex object should be independent of the partsthat make up the
object and how they are assembled.
• When the construction process must allow different representations for the object that is constructed.
• When you want to insulate clients from the knowledge of the actual creation process and/or resulting
product.
Benefits:
• The built object is shielded from the details of its construction. Code for construction is isolated from
code for representation and both are easy to replace without affecting the other.
• Gives you control over the construction process.
• Gives you the possibility to reuse and/or change the process and/or product independently.
3.1.4. Singleton Pattern
Definition:
While the singleton pattern falls in with other creational patterns, it is somewhat distinctive in that it limits
creation of objects. A singleton restricts a class to one and only one instance, providing a single, global
means of accessing that instance.
When to use:
When only one instance or a specific number of instances of a class are allowed. Facade objects are often
Singletons because only one Facade object is required.
Benefits:
• Controlled access to unique instance.
• Reduced name space.
• Allows refinement of operations and representations.
3.1.5 Prototype Pattern
Definition:
The Prototype pattern is basically the creation of new instances by duplicating existing instances. By
creating a prototype, new objects are created by copying this prototype.
When to use
• When a system needs to be independent of how its objects are created, composed, and represented.
• When adding and removing objects at runtime.
• When specifying new objects by changing an existing object’s structure.
• When configuring an application with classes dynamically.
• When keeping trying to keep the number of classes in a system to a minimum.
Page 9
Benefits
• Speeds up instantiation of large, dynamically loaded classes.
• Reduced subclassing.
Page 10
• When you want clients to be able to ignore the differences between compositions of objects and
individual objects.
• When the structure can have any level of complexity and is dynamic.
Benefits
Define class hierarchies consisting of primitive objects and composite objects. Makes it easier to add new
kind of components.
Example of Structure pattern
Here, I will take the example of adapter pattern.
• First, I will create interfaces for CreditCard
Page 11
Next, I will create BankCustomer which extends BankDetails and implement CreditCard class.
Page 12
3.2.4 Decorator Pattern
Definition:
The Decorator pattern lets you attach additional responsibilities and modify an instance functionality
dynamically. Decorators provide a flexible alternative to subclassing for extending functionality, using
composition instead of inheritance.
Where to use
• When you want to add responsibilities to individual objects dynamically and transparently, without
affecting the original object or other objects.
• When you want to add responsibilities to the object that you might want to change in the future.
• When extension by static subclassing is impractical.
Benefits
• More flexibility than static inheritance.
• Avoids feature-laden classes high up in the hierarchy.
• Simplifies coding because you write a series of classes each targeted at a specific part of the functionality
rather than coding all behavior into the object.
• Enhances the object's extensibility because you make changes by coding new classes.
4. Flyweight Pattern
Definition
Flyweight pattern provides a mechanism by which you can avoid creating a large number of 'expensive'
objects and instead reuse existing instances to represent new ones.
Where to use
• When there is a very large number of objects that may not fit in memory.
• When most of an object’s state can be stored on disk or calculated at runtime.
• When there are groups of objects that share state.
• When the remaining state can be factored into a much smaller number of objects with shared state.
Benefits
Reduce the number of objects created, decrease memory footprint and increase performance.
4.1. Behavior Patterns
Whereas creational design patterns have to do with instantiating objects and structural patterns have to do
with composing objects and classes into larger, more complex structures, behavioral design patterns are
concerned with communication among a family of objects.
While there are a variety of behavioral design patterns, we will consider four here: chain of responsibility,
observer, state, strategy and command pattern.
4.1.1. Chain-of-responsibility Pattern
Definition:
The Chain-of-responsibility pattern lets more than one object handle a request without mutual knowledge.
We avoid coupling between the sender of a request and the possible receivers. We place all receivers in a
chain which lets the receiving objects pass the request along to the next receiver in the chain until one
receiver handles it, or the end of the chain is reached.
When to use
• When more than one object may handle a request, and the handler isn't known.
• When you want to issue a request to one of several objects without specifying the receiver explicitly.
• When the set of objects that can handle a request should be specified dynamically.
Benefits
• It reduces coupling.
• It increases the flexibility of handling a request.
Page 13
4.1.2 Command Pattern
Definition
The Command pattern is used to create objects that represents actions and events in an application. A
command object encapsulates an action or event and contains all information required to understand
exactly what has happened. By passing the command object as a parameter we can, anywhere needed
extract information about occurred actions and events.
When to use
• Where you want an action that can be represented in many ways, like dropdown menu, buttons and
popup menu.
• To create undo/redo functionality.
Benefits
• A command object is a possible storage for procedure parameters. It can be used while assembling the
parameters for a function call and allows the command to be set aside for later use.
• A class is a suitable place to collect code and data related to a specific action or event.
• It allows the reaction to a command to be executed sometime after it has occurred.
• Command objects enables data structures containing multiple commands.
• Command objects supports undo-able operations, provided that the command objects are stored (for
example in a linked list).
4.1.3 Observer Pattern
Definition:
An observer is a structural pattern that enables publish/subscribe functionality. This is accomplished by an
autonomous object, publisher that allows other objects to attach or detach their subscription as they like.
The pattern does not impose any limit to the number of observers that can attach, or subscribe,
themselves for notification on future changes in the publisher's state.
When to use
When an object wants to publish information and many objects will need to receive
that information.
Benefits
Makes for a loose coupling between publisher and subscriber as the publisher does
not need to know who or how many subscribers there will be.
4.1.4 State Pattern
Definition:
The State pattern allows an object to alter its behavior when its internal state changes. By using inheritance
and letting subclasses represent different states and functionality we can switch during runtime. This is a
clean way for an object to partially change its type at runtime.
When to use
• When we need to define a "context" class to present a single interface to the outside world. By defining a
State abstract base class.
• When we want to represent different "states" of a state machine as derived classes of the State base
class.
Benefits
Cleaner code when each state is a class instead. Use a class to represent a state, not a constant.
Page 14
4.1.5 Strategy Pattern
Definition: Use strategy when you need to define a family of algorithms, encapsulate each one, and make
them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Related
patterns include State, Flyweight, Decorator, Composite.
Where to use
• When you need to use one of several algorithms dynamically.
• When you want to configure a class with one of many related classes (behaviors).
• When an algorithm uses data that clients should not know about.
Benefits
• Reduces multiple conditional statements in a client.
• Hides complex, algorithmic-specific data from the client.
• Provides an alternative to subclassing.
• Can be used to hide data that an algorithm uses that client should not know about.
Example of behavior pattern
Here, I will take the example of command pattern In summary, in this assignment I have built an
application derived from the UML class diagram generated in assignment 1. Then, I explained a series of
design patterns with relevant examples of types. pattern of creation, structure and behavior.
Page 15
REFERENCES
1. Advanced programming with C#
Available at: https://www.codeproject.com/Articles/1094625/Advanced-programming-with-Csharp
Lecture-Notes-Par [Accessed December 19, 2022]
2. What is Object Oriented Programming? OOP Explained in Depth
Available at: https://www.educative.io/blog/object-oriented-programming [Accessed December 19, 2022]
3. Design a Library Management System
Available at: https://www.educative.io/courses/grokking-the-object-oriented
designinterview/RMlM3NgjAyR [Accessed December 19, 2022]
4. Object Oriented Programming Using C# .NET
Available at: https://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-
C-Sharp-net/
[Accessed December 19, 2022]
1. Lập trình hướng đối tượng (OOP) trong C#
Available at: https://comdy.vn/c-sharp/lap-trinh-huong-doi-tuong-oop-trong-c-sharp/ [Accessed December
19, 2022]
2. OOP Concepts in C#: Code Examples and How to Create a Class
Available at: https://stackify.com/oop-concepts-c-sharp/ [Accessed December 19, 2022]
3. C# OOP
Available at: https://www.w3schools.com/cs/cs_oop.php [Accessed December 19, 2022]
4. C# Inheritance with Examples
Available at: https://www.tutlane.com/tutorial/csharp/csharp-inheritance
[Accessed December 19, 2022]
5. C# Inheritance with Examples
Available at: https://www.tutlane.com/tutorial/csharp/csharp-encapsulation [Accessed December 19, 2022]
6. C# Polymorphism with Examples
Available at: https://www.tutlane.com/tutorial/csharp/csharp-polymorphism [Accessed December 19,2022]
7. C# Abstraction with Examples
Available at: https://www.tutlane.com/tutorial/csharp/csharp-abstraction
[Accessed December 19, 2022]
8. Types Of Relationships In Object Oriented Programming (OOPS)
Available at: https://www.c-sharpcorner.com/article/types-of-relationships-in-
objectorientedprogramming
oops/
[Accessed December 19, 2022]
9. Dependency, Generalization, Association, Aggregation, Composition in Object Oriented Programming
Available at: https://www.c-sharpcorner.com/UploadFile/b1df45/dependency
generalizationassociation-
aggregation-compos/ [Accessed December 19, 2022]
1. Class Diagram for Library Management System
Available at: https://www.geeksforgeeks.org/class-diagram-for-library-management-system/ [Accessed
December 19, 2022]
2. Use Case Diagram for Library Management System
Available at: https://www.geeksforgeeks.org/use-case-diagram-for-library-management-system/ [Accessed
December 19, 2022]
Page 16