0% found this document useful (0 votes)
114 views

Assigment2-Advanced Programing- Hải Đăng - 2

This document discusses an assignment to implement code applying design patterns by building an application from a UML class diagram to create a list of dogs with details like name, age, and origin. It then discusses various design patterns including creational patterns like Singleton which ensures a class only has one instance, structural patterns like Adapter which allows incompatible interfaces to work together, and behavioral patterns like Observer which defines dependency relationships so objects notify each other of changes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Assigment2-Advanced Programing- Hải Đăng - 2

This document discusses an assignment to implement code applying design patterns by building an application from a UML class diagram to create a list of dogs with details like name, age, and origin. It then discusses various design patterns including creational patterns like Singleton which ensures a class only has one instance, structural patterns like Adapter which allows incompatible interfaces to work together, and behavioral patterns like Observer which defines dependency relationships so objects notify each other of changes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

ASSIGNMENT 2 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 20: Advanced Programming

Submission date February 2nd 2020 Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name HUỲNH NGỌC HẢI ĐĂNG Student ID GBD18577

Class GCD0704 Assessor name NGUYỄN VĂN LỢI

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:

HUỲNH NGỌC HẢI ĐĂNG 1


HUỲNH NGỌC HẢI ĐĂNG 2
ASSIGMENT 2
NAME: HUỲNH NGỌC HẢI ĐĂNG
CLASS: GCD0704
ID: GBD18577
TEACHER: NGUYỄN VĂN LỢI

ADVANCED
PROGRAMING

HUỲNH NGỌC HẢI ĐĂNG 3


Table of Contents
I. LO3 Implement code applying design patterns ...............................................................................5
1. Build an application derived from UML class diagrams. .............................................................5
1.1 The scenario .........................................................................................................................5
1.2 UML Class Diagram ...............................................................................................................5
1.3 Class Diagram .......................................................................................................................5
1.4 Code of the scenario .............................................................................................................6
1.5 The result of program ...........................................................................................................8
II. LO4 Investigate scenarios with respect to design patterns.............................................................9
2. Discuss a range of design patterns with relevant examples of creational, structural and
behavioral pattern types. ...............................................................................................................9
2.1 Creational patterns ...............................................................................................................9
2.2. Structural patterns............................................................................................................12
2.3. Behavioral patterns ...........................................................................................................16
III. REFERENCE ..................................................................................................................................24

Table of Figures
Figure 1 UML Class Diagram of the scenario ........................................................................................5
Figure 2 Class Diagram from code .......................................................................................................6
Figure 3 Origin Class Code ...................................................................................................................6
Figure 4 Dogs Class Code.....................................................................................................................7
Figure 5 Program Class Code ...............................................................................................................7
Figure 6 The result of scenario ............................................................................................................8
Figure 7 The result of scenario ............................................................................................................8

List of Tables
Table 1 Creational Patterns ............................................................................................................... 12
Table 2 Structural Patterns................................................................................................................ 16
Table 3 Behavioral Patterns .............................................................................................................. 23

HUỲNH NGỌC HẢI ĐĂNG 4


I. LO3 Implement code applying design patterns

1. Build an application derived from UML class diagrams.

1.1 The scenario


PetZ needs to create a list of dogs: name, age, sex, height, weight, and origin code. Our goal is to create
a Dogs' father and subclass class list that includes country of origin, characteristics and type. Any time
we buy it, we can easily see the specifics of this breed, as well as explain its origin certified on legal
papers.

1.2 UML Class Diagram


The UML diagram we can see:
Program Class This is the key class that takes the results to the computer.
Dogs Class to collect and receive information ( name, age , sex, height , weight, ID, etc.).
Origin Class contains information such as (country, type, feature) to insert information about dogs.

Figure 1 UML Class Diagram of the scenario

1.3 Class Diagram

HUỲNH NGỌC HẢI ĐĂNG 5


Figure 2 Class Diagram from code

1.4 Code of the scenario


Class of Origin:
- The first element specifies the variables for use in the knowledge.
- The next part is for the keyboard feature to get details.
- The last part is to complement the knowledge concerning the students that Dogs class does not yet
have.

Figure 3 Origin Class Code

HUỲNH NGỌC HẢI ĐĂNG 6


Class Dogs:
-- The first element specifies the variables for use in the knowledge.
-- The next part is the keyboard feature to get details.
-- The final portion will be printed with the dog's basic knowledge material.

Figure 4 Dogs Class Code

Program Class:
Their task is to add a student's information to get the results printed on the screen. The order in which
the information is entered is the same as its variables in the Origin Class.

Figure 5 Program Class Code

HUỲNH NGỌC HẢI ĐĂNG 7


1.5 The result of program
The printed content conforms to the variables ordered in the order in which they are available. Users
can only construct a new blank student and apply details to that student.

Figure 6 The result of scenario

Figure 7 The result of scenario

HUỲNH NGỌC HẢI ĐĂNG 8


II. LO4 Investigate scenarios with respect to design patterns

2. Discuss a range of design patterns with relevant examples of creational,


structural and behavioral pattern types.

2.1 Creational patterns


Pattern Class Diagrams Short Explanation / Explain
Name how the pattern works

Singleton A singleton is a class that only


allows a single instance of itself
to be created and usually gives
simple access to that instance.
Ensures a class has only one
instance and provides a global
point of access to it.

There are two forms of


singleton design pattern

• Early Instantiation: creation


of instance at load time.

• Lazy Instantiation: creation


of instance when required.

The advantages of a Singleton


Pattern Singleton pattern can
be implemented interfaces. It
can be also inherited from other
classes. It can be lazy-loaded. It
has Static Initialization. It can
be extended into a factory
pattern. It helps to hide
dependencies. It provides a
single point of access to a
particular instance, so it is easy
to maintain.

Singleton implementations

 A single constructor,
which is private and
parameterless.
 The class is sealed. This
is unnecessary, strictly

HUỲNH NGỌC HẢI ĐĂNG 9


speaking, due to the
above point
 A static variable which
holds a reference to the
single created instance,
if any.
 A public static means of
getting the reference to the
single created instance

Factory Factory pattern is one of the


most used design patterns. This
type of design pattern comes
under creational pattern as this
pattern provides one of the best
ways to create an object.
The factory is used when there
are multiple implementations of
the same interface and when the
customer really wants and the
customer doesn't, it is
determined at run time itself.
Know which deployment to
use. The factory will cover the
specifics of making a particular
object and return it as a general
understanding of the interface
required.
There are two distinct trends
related to the name of the
company, as far as practicable:
factory and process of factory.
The factory method is used to
manufacture products, and
several other similar products
have been produced before.
The advantages of a Factory
Pattern

HUỲNH NGỌC HẢI ĐĂNG 10


 You avoid tight
coupling between the
creator and the concrete
products.
 Single Responsibility
Principle. You can
move the product
creation code into one
place in the program,
making the code easier
to support.
 Open/Closed Principle.
You can introduce new
types of products into
the program without
breaking existing client
code.

Prototype Prototype is a creational design


pattern that lets you copy
existing objects without making
your code dependent on
their classes.
The prototype pattern is a
creational design pattern.
Prototype patterns is required,
when object creation is time
consuming, and costly
operation, so we create object
with existing object itself. One
of the best available way to
create object from existing
objects are clone() method.
Clone is the simplest approach
to implement prototype pattern.
However, it is your call to
decide how to copy existing
object based on your business
model.
The advantages of a
Prototype Pattern

HUỲNH NGỌC HẢI ĐĂNG 11


 You can clone objects
without coupling to their
concrete classes.
 You can get rid of
repeated initialization
code in favor of cloning
pre-built prototypes.
 You can produce
complex objects more
conveniently.
 You get an alternative to
inheritance when
dealing with
configuration presets for
complex objects.

Table 1 Creational Patterns

2.2. Structural patterns


Pattern Class Diagrams Short Explanation / Explain
Name how the pattern works
Adapter Adapter is a structural design
pattern that allows objects with
incompatible interfaces
to collaborate.
Implementing class adapters
Adapter would inherit publicly
from Target and privately from
Adaptee. Thus Adapter would
be a subtype of Target but not
of Adaptee. Adaptee interface
in the TreeDisplay class.
Subclasses must implement the
abstract operations and adapt
the hierarchically structured
object. DirectoryTreeDisplay
specializes the narrow interface
so that it can display directory
structures made up of

HUỲNH NGỌC HẢI ĐĂNG 12


FileSystemEntity objects.
TreeDisplay forwards requests
for accessing the hierarchical
structure to a delegate object.
TreeDisplay can use a different
adaptation strategy by
substituting a different
delegate.
A block can adapt a request,
and the adapter can store a
block for each individual
request. In our example, this
means TreeDisplay stores one
block for converting a node
into a GraphicNode and
another block for accessing a
node's children.
The advantages of a Adapter
Pattern
One Theory of Accountability.
You may set the interface or
data conversion code separately
from the program's primary
business logic.
Principles Open / Closed. You
may add new types of adapters
into the software without
breaking existing client code,
so long as they operate through
the client interface with the
adapters.

HUỲNH NGỌC HẢI ĐĂNG 13


Bridge Bridge is a structural design
pattern that lets you split a
large class or a set of closely
related classes into two
separate hierarchies—
abstraction and
implementation—which can be
developed independently of
each other.
Bridge Implementation
Identify the orthogonal
dimensions in your classes.
These independent concepts
could be: abstraction/platform,
domain/infrastructure, front-
end/back-end, or
interface/implementation. See
what operations the client
needs and define them in the
base abstraction class.
Determine the operations
available on all platforms.
Declare the ones that the
abstraction needs in the general
implementation interface. For
all platforms in your domain
create concrete implementation
classes, but make sure they all
follow the implementation
interface. Inside the abstraction
class, add a reference field for
the implementation type. If you
have several variants of high-
level logic, create refined
abstractions for each variant by
extending the base abstraction
class. The client code should
pass an implementation object

HUỲNH NGỌC HẢI ĐĂNG 14


to the abstraction’s constructor
to associate one with the other.
The advantages of a Bridge
Pattern
 You can build classes
and apps which are
platform independent.
 High-level abstractions
function in the client
code. It isn't exposed to
information about the
site.
 Principle open / Closed.
New abstractions and
implementations can be
implemented
independently of one
another.
 Clear Concept of
Accountability. In the
abstraction, you can
concentrate on high-
level logic, and
application platform
specifics.

Composite Composite is a structural


design pattern that lets you
compose objects into tree
structures and then work with
these structures as if they were
individual objects.
Composite Implementation
Make sure that the core model
of your app can be represented
as a tree structure. Declare the

HUỲNH NGỌC HẢI ĐĂNG 15


component interface with a list
of methods that make sense for
both simple and complex
components. Create a leaf class
to represent simple elements. A
program may have multiple
different leaf classes. Create a
container class to represent
complex elements. In this class,
provide an array field for
storing references to sub-
elements. The array must be
able to store both leaves and
containers, so make sure it’s
declared with the component
interface type. Finally, define
the methods for adding and
removal of child elements in
the container.
The advantages of a
Composite Pattern
More conveniently you can
deal with complex tree
structures: use polymorphism
and recursion for your benefit.
Principles Open / Closed. You
may add new types of elements
into the app without breaking
existing code, which now
works with the tree of objects.

Table 2 Structural Patterns

2.3. Behavioral patterns


Pattern Class Diagrams Short Explanation /
Name Explain how the pattern
works

HUỲNH NGỌC HẢI ĐĂNG 16


Command Command is a pattern of
behavioral design which
turns a request into a stand-
alone object which
contains all the request
information. This
transformation allows
parameterizing methods
with different requests,
delaying or queuing
execution of a request and
supporting undoable
operations.
Command
Implementation
Declare a single execution
method on the command
interface. Start extracting
requests into specific
command classes which
implement the interface of
the command. Each class
must have a set of fields to
store the request arguments
together with a reference to
the receiver object. Identify
classes which are to act as
senders. Add the command
storage fields into those
classes. Senders should
communicate only via
command interface with
their commands. Usually
senders do not create
command objects on their
own but instead get them
from the client code.
Change the senders so they

HUỲNH NGỌC HẢI ĐĂNG 17


execute the command
rather than sending a
request directly to the
receiver.
In the following order, the
client should initialise
objects:

 Set up receivers.
 Create instructions,
and if necessary
connect them with
the receivers.
 Build senders, and
associate basic
commands with
them.
The advantages of a
Command Pattern
 Clear Concept of
Accountability.
You can decouple
classes that trigger
class operations
that execute certain
operations.
 Principle open /
Closed. You can
insert new
commands into the
program without
breaking existing
client code.
 You will have undo
/ redo activated.
 You can perform
deferred operations.

HUỲNH NGỌC HẢI ĐĂNG 18


 You may assemble
a set of simple
commands into one
which is complex.

Iterator Iterator is a pattern of


behavioral design allowing
you to traverse elements of
a set without revealing
their underlying
representation (list, stack,
tree, etc.).
Iterator Implementation
Declare interface with the
iterator. At the very least it
needs a method to get the
next element from a list.
But you can add a few
other methods for
convenience, such as
hunting the previous part,
tracking the current
location, and checking the
end of the iteration.
Declare the design of the
set and define a method of
obtaining iterators. The
form of return should be
the same as for the iterator
interface. If you intend to
have many distinct groups
of iterators you can declare
similar methods.
Implement concrete
iterator classes for the
collections you want the
iterators to be traversable.
An iterator object must be

HUỲNH NGỌC HẢI ĐĂNG 19


assigned to a single
instance of the set.
Typically this connection
is formed through the
constructor of the iterator.
Implement the interface for
collection in your
collection groups. The key
concept is to give the client
a shortcut to build iterators,
customized to a specific
class of collections. To
create a connection
between them, the
collection object has to
transfer itself to the
constructor of the iterator.
Go over the client code to
replace all traversal code of
the array with iterators.
The client can retrieve a
new iterator object anytime
it wants to iterate over the
collection items.
The advantages of a
Iterator Pattern
 Clear Concept of
Accountability. By
separating
cumbersome
traverse algorithms
into different
groups you can
clean up the client
code and the sets.
 Principle open /
Closed. You can
add new types of

HUỲNH NGỌC HẢI ĐĂNG 20


collections and
iterators without
breaking anything,
and transfer them to
existing code.
 You can iterate in
parallel over the
same set, since
each iterator object
has its own
iteration state.
 You can pause an
iteration for the
same purpose, and
finish it when
needed.

Observer Observer is a pattern of


behavioral design that
allows you to define a
subscription mechanism to
notify multiple objects of
any events that occur with
the object that they are
observing.
Observer
Implementation
Look at the business logic
and try to break it down
into two parts: the core
functionality will serve as
the publisher, independent
of other code; the rest will
become a collection of
subscriber classes.
Declare an app for
subscribers. Single update

HUỲNH NGỌC HẢI ĐĂNG 21


method should be defined
at a bare minimum.
State the publisher
interface and define a pair
of methods for adding and
removing a subscriber
object from the list.
Decide where to position
the actual subscription list
and how to enforce
subscription methods.
Typically for all types of
publishers this code looks
the same, so the logical
place to put it is in an
abstract class derived
directly from the publisher
interface. Concrete
publishers expand the
class, inheriting the actions
of subscribers.
Creating specific divisions
for publishers. Any time
anything significant
happens within a publisher,
all its subscribers must be
informed by it.
Implement the methods of
updating notification in
specific user groups. Most
subscribers will need some
background information
about the case. It can be
passed by as a notification
process statement.
The advantages of a
Observer Pattern

HUỲNH NGỌC HẢI ĐĂNG 22


Principles Open / Closed.
You may add new user
groups without having to
modify the code for the
publisher (and vice versa if
there is an interface to the
publisher).
At runtime you can create
relationships among
objects.

Table 3 Behavioral Patterns

HUỲNH NGỌC HẢI ĐĂNG 23


III. REFERENCE

(n.d.). Retrieved from https://refactoring.guru/design-patterns

(n.d.). Retrieved from


https://www.ibm.com/support/knowledgecenter/en/SS8PJ7_9.7.0/com.ibm.xtools.viz.dotne
t.doc/topics/t_devcsapp.html

(n.d.). Retrieved from https://www.c-sharpcorner.com/UploadFile/bd5be5/design-patterns-in-net/

(n.d.). Retrieved from http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf

HUỲNH NGỌC HẢI ĐĂNG 24

You might also like