1651 GCS0901 NguyenHoaiNam Assignment2
1651 GCS0901 NguyenHoaiNam Assignment2
udent declaration
ertify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I underst
at making a false declaration is a form of malpractice.
ding grid
P3 P4 M3 M4 D3 D4
❒ Summative Feedback: ❒ Resubmission Feedback:
Assignment title Application development with class diagram and design patterns
Academic Year
Unit Tutor
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).
P3 Build an application derived from UML M3 Develop code that implements D3 Evaluate the use of design patterns
class diagrams. a design pattern for a given for the given purpose specified in M3.
purpose.
P4 Discuss a range of design patterns with M4 Reconcile the most appropriate D4 Critically evaluate a range of design
relevant examples of creational, structural design pattern from a range with a patterns against the range of given
and behavioral pattern types. series of given scenarios.
scenarios with justification of your
choices.
Table of Contents
Table of contents........................................................................................................................4
I. Introduction............................................................................................................................5
II. Build an application derived from UML class diagrams......................................................5
III. Design Patterns..................................................................................................................12
1. What is design pattern......................................................................................................12
2. When use design patterns.................................................................................................13
3. Categorization of patterns................................................................................................13
3.1 Creational Pattern.......................................................................................................14
3.1.1 Factory pattern....................................................................................................14
3.1.2 Abstract Factory Pattern.....................................................................................16
3.1.3 Builder Pattern....................................................................................................16
3.1.4 Singleton Pattern.................................................................................................17
3.1.5 Prototype Pattern.................................................................................................17
3.2 Structure Patterns.......................................................................................................18
3.2.1 Adapter Pattern...................................................................................................18
3.2.2 Bridge Pattern.....................................................................................................19
3.2.3 Composite Pattern...............................................................................................19
3.2.4 Decorator Pattern................................................................................................19
III.1.5 Flyweight Pattern..............................................................................................20
3.3 Behavior Patterns.......................................................................................................23
3.3.1 Chain-of-responsibility Pattern...........................................................................23
3.3.2 Command Pattern................................................................................................24
3.3.3 Observer Pattern..................................................................................................24
3.3.4 State Pattern........................................................................................................25
Page |1
Class IPersonManage
- This is a interface class has main functionalities (Add, update, delete), with Inheritance of class
StudentManage and LectureManage.
Page |2
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 |3
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.
Page |4
Class Student
- Association with Class StudentManage allow it use data type Student and Storage Student data.
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.
Page |5
Class Lecture
- Association with LectureManage allow it use data type Lecture and Storage Lecture data.
Class MainMenu
- This class has a Realization relationship with class PersonMenuFactory, show menu to user chose
manage student or lecture then run class PersonMenuFactory.
Page |6
Class Program
-This class has a Realization relationship with class Main menu, it run class Main menu.
• Flexibility: Using design patterns, your code becomes flexible. It helps to provide
the correct level of abstraction so that objects are loosely linked together, which
makes your code easy to change.
• Reusability: Loosely coupled and cohesive objects and classes can make your
code more reusable. This kind of code becomes easier to test than highly
coupled code.
• Shared Vocabulary: Shared vocabulary makes it easy to share your code and
thought with other team members. It creates more understanding between the
team members related to the code.
• Design patterns make it easier to reuse successful designs and architectures.
3. Categorization of patterns
Design patterns can be categorized in following categories:
Creational patterns create objects for you. These give you some flexibility in using
different object types in different scenarios. For example, you don't need to prepare
your own value meal when you go to a fast-food restaurant. There are many
different varieties but all serve the same goal of giving you a meal (with modest
nutritional value). Instead, a staff member takes your order and prepares the meal
for you. Here is an example of a builder pattern.
Structural patterns are about composing several objects into larger structures. A
simple example is like a power adapter. The European wall socket has two round
pins and the power supply is 220V. An unfortunate North American may have an
Page |8
appliance whose plug will not fit and will expect a measly 110V. Thus, an adapter is
needed so that every traveler need not worry about how to bridge this electrical
divide herself. Here's an example of one of the simplest structural patterns, called
(not surprisingly) an adapter.
Ultimately, behavior patterns are about communication and flow control. A common
example is someone subscribing to a magazine. Subscribers cannot keep track of all
the details on topics they might be interested in, but periodically they receive a
succinct update on relevant information. This type of pattern is called an observer, in
which automatic status updates about one object are sent automatically to another
object (the subscriber).
The below table shows the list of patterns under their respective categories:
3.1Creational Pattern
As their name suggests, the family of creational patterns all deal with creating
instances of objects. The OOP principle of polymorphism allows our programs to
work flexibly with many different specific types of objects that share particular
properties. The specific, runtime object type is sometimes unimportant and depends
on the situation. We can extend this idea to the act of creating objects as well.
While there are several standard creational patterns, we will consider four: factory,
abstract factory, builder, and singleton. Let us look at each of these in more detail.
Page |9
3.1.1 Factory pattern
Definition: Factory pattern provides a way to use an instance as an object factory.
The factory may return an instance of one of several possible classes (in the subclass
hierarchy), depending on the data provided to it.
When to use:
• When a class can't anticipate which kind of class of object it must create.
• You want to localize the knowledge of which class gets created.
• When you have classes that is derived from the same subclasses, or they may
in fact be unrelated classes that just share the same interface. Either way, the
methods in these class instances are the same and can be used
interchangeably.
• When you want to insulate the client from the actual type that is being
instantiated. Benefits:
• The client does not need to know every subclass of objects it must create. It
only needs one reference to the abstract class/interface and the factory object.
• The factory encapsulates the creation of objects. This can be useful if the
creation process is very complex.
Example:
This example shows how two different concrete Products are created using the
ProductFactory. ProductA uses the superclass writeName method. ProductB
implements writeName that reverses the name.
P a g e | 10
When Test is executed, the result is:
P a g e | 11
P a g e | 12
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:
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:
Benefits
• Speeds up instantiation of large, dynamically loaded classes.
• Reduced subclassing.
• When you want to use an existing class, and its interface does not match the
one you need.
P a g e | 15
• When you want to create a reusable class that cooperates with unrelated or
unforeseen classes, that is, classes that do not necessarily have compatible
interfaces.
• When you want to increase transparency of classes. When you want to make a
pluggable kit.
Benefits:
• When you want to separate the abstract structure and its concrete
implementation.
• When you want to share an implementation among multiple objects, when
you want to reuse existing resources in an 'easy to extend' fashion.
• When you want to hide implementation details from clients. Changes in
implementation should have no impact on clients.
Benefits:
Benefits
Define class hierarchies consisting of primitive objects and composite
objects. Makes it easier to add new kind of components.
P a g e | 17
Next, I will create BankCustomer which extends BankDetails and implement CreditCard
class.
P a g e | 18
Finally, I will create AdapterPatternDemo to run the program.
P a g e | 19
When AdapterPatternDemo is executed, the result is:
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.
Flyweight pattern provides a mechanism by which you can avoid creating a large
P a g e | 20
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.
P a g e | 21
3.3.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.
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.
P a g e | 22
• 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).
Benefits
Cleaner code when each state is a class instead. Use
a class to represent a state, not a constant.
P a g e | 23
3.3.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.
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.
P a g e | 24
REFERENCES
12, 2021]
12, 2021]
3. C# OOP
Available at: https://www.w3schools.com/cs/cs_oop.php [Accessed
12, 2021]
oops/
12, 2021]
P a g e | 27