Tải bản đầy đủ (.pdf) (65 trang)

Assignment 1 Advanced Programming (1651 Distinction)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.57 MB, 65 trang )

ASSIGNMENT 1 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

Bui Quang Minh

Student ID

GCD210325

Class

GCD1104

Assessor name


Nguyen The Nghia

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
P1

P2

M1

M2

D1

D2


 Summative Feedback:

Grade:
Lecturer Signature:

 Resubmission Feedback:

Assessor Signature:

Date:



Table of Contents
INTRODUCTION ........................................................................................................................................ 9
CHAPTER 1. OOP GENERAL CONCEPTS ................................................................................................... 10
I. Object-oriented programming (OOP) ...................................................................................................................................... 10
1. Definition ..........................................................................................................................................................................................................10
2. Benefits and drawbacks ...............................................................................................................................................................................10
II. Object and class ............................................................................................................................................................................ 11
1. Class ....................................................................................................................................................................................................................11
1.1 Definition ...................................................................................................................................................................................................11
1.2 Class diagram ...........................................................................................................................................................................................12
1.3 Code.............................................................................................................................................................................................................12
2. Object..................................................................................................................................................................................................................13
2.1 Definition ...................................................................................................................................................................................................13
2.2 Code.............................................................................................................................................................................................................13
3. Class and Object Comparison .....................................................................................................................................................................13
Class....................................................................................................................................................................................................................14
Object .................................................................................................................................................................................................................14
Class and Object comparison .....................................................................................................................................................................14
III. Encapsulation .............................................................................................................................................................................. 14
1. Definition ..........................................................................................................................................................................................................14
2. Advantages and disadvantages..................................................................................................................................................................14
3. UML and code ..................................................................................................................................................................................................15
IV. Inheritance ................................................................................................................................................................................... 17
1. Definition ..........................................................................................................................................................................................................17
2. Advantages and disadvantages..................................................................................................................................................................17
3. Types of inheritance ......................................................................................................................................................................................18
3.1 Single inheritance ...................................................................................................................................................................................18
3.2 Multilevel inheritance ...........................................................................................................................................................................18

3.3 Multiple inheritance ..............................................................................................................................................................................19
3.4 Multipath inheritance............................................................................................................................................................................20
3.5 Hierarchical inheritance.......................................................................................................................................................................20
3.6 Hybrid inheritance .................................................................................................................................................................................21
4. Class diagram and code ................................................................................................................................................................................22
V. Polymorphism ............................................................................................................................................................................... 24
1. Definition ..........................................................................................................................................................................................................24
2. Advantages and disadvantages..................................................................................................................................................................25
3. Class diagram and code ................................................................................................................................................................................26


VI. Abstraction ................................................................................................................................................................................... 28
1. Definition ..........................................................................................................................................................................................................28
2. Types of abstraction ......................................................................................................................................................................................28
2.1 Data abstraction ......................................................................................................................................................................................28
2.1 Process abstraction ................................................................................................................................................................................29
3. Advantages and disadvantages..................................................................................................................................................................30
4. Class diagram and code ................................................................................................................................................................................31

CHAPTER 2. DESIGN AND BUILD CLASS DIAGRAMS USING UML TOOLS ................................................. 34
I. Theory of UML ................................................................................................................................................................................ 34
1. Definition ..........................................................................................................................................................................................................34
2. Types of UML diagram ..................................................................................................................................................................................34
II. Diagrams......................................................................................................................................................................................... 35
1. Use-Case diagram ...........................................................................................................................................................................................35
1.1 Definition ...................................................................................................................................................................................................35
1.2 Notations ...................................................................................................................................................................................................36
1.3 Advantages and disadvantages ..........................................................................................................................................................37
1.4 Example .....................................................................................................................................................................................................37
2. Class diagram...................................................................................................................................................................................................39

2.1 Definition ...................................................................................................................................................................................................39
2.2 Notations ...................................................................................................................................................................................................39
2.3 Advantages and disadvantages ..........................................................................................................................................................41
2.4 Example .....................................................................................................................................................................................................42
3. Sequence diagram ..........................................................................................................................................................................................43
3.1 Definition ...................................................................................................................................................................................................43
3.2 Notations ...................................................................................................................................................................................................43
3.3 Advantages and disadvantages ..........................................................................................................................................................44
3.4 Example .....................................................................................................................................................................................................45

CHAPTER 3. DESIGN PATTERN ................................................................................................................ 46
I. Overview .......................................................................................................................................................................................... 46
1. Definition ..........................................................................................................................................................................................................46
2. Types of design patterns ..............................................................................................................................................................................47
3. Advantages and disadvantages of design patterns .............................................................................................................................47
II. Creational patterns ...................................................................................................................................................................... 48
1. Singleton pattern ............................................................................................................................................................................................48
1.1 Definition ...................................................................................................................................................................................................48
1.2 Advantages and disadvantages ..........................................................................................................................................................48
1.3 Class diagram ...........................................................................................................................................................................................49
1.4 Code example ...........................................................................................................................................................................................49


III. Structural patterns ..................................................................................................................................................................... 51
1. Facade pattern.................................................................................................................................................................................................51
1.1 Definition ...................................................................................................................................................................................................51
1.2 Advantages and disadvantages ..........................................................................................................................................................52
1.3 Class diagram ...........................................................................................................................................................................................52
1.4 Code example ...........................................................................................................................................................................................53
IV. Behavioral patterns.................................................................................................................................................................... 54

1. Command pattern ..........................................................................................................................................................................................54
1.1 Definition ...................................................................................................................................................................................................54
1.2 Advantages and disadvantages ..........................................................................................................................................................54
1.3 Class diagram ...........................................................................................................................................................................................55
1.4 Code example ...........................................................................................................................................................................................55

CHAPTER 4. DESIGN PATTERN AND OOP ................................................................................................ 58
I. Relationship between OOP and Design pattern .................................................................................................................... 58
II. Analyzing OOP through design patterns ................................................................................................................................ 59
III. Analyzing OOP through scenario............................................................................................................................................ 61

CONCLUSION .......................................................................................................................................... 63
REFERENCE LIST ...................................................................................................................................... 64


Table of Figures
Figure 1. OOP abbreviation ..................................................................................................................... 10
Figure 2. Class diagram of MobilePhone .................................................................................................. 12
Figure 3. MobilePhone's code implementation ....................................................................................... 12
Figure 4. Object's code implementation .................................................................................................. 13
Figure 5. MobilePhone's class diagram .................................................................................................... 15
Figure 6. MobilePhone's code implementation ....................................................................................... 16
Figure 7. Code implementation in Main................................................................................................... 16
Figure 8. Single Inheritance illustration.................................................................................................... 18
Figure 9. Multi-Level inheritance illustration ........................................................................................... 19
Figure 10. Multiple inheritance illustration .............................................................................................. 19
Figure 11. Multipath inheritance illustration ........................................................................................... 20
Figure 12. Hirachical inheritance illustration ............................................................................................ 21
Figure 13. Hybrid inheritance illustration................................................................................................. 21
Figure 14. Inheritance class diagram........................................................................................................ 22

Figure 15. Parent class code implementation .......................................................................................... 22
Figure 16. Son class code implementation ............................................................................................... 23
Figure 17. Object initialization code ........................................................................................................ 23
Figure 18. Result of code ......................................................................................................................... 24
Figure 19. Polymorphism concepts .......................................................................................................... 25
Figure 20. Polymorphism class diagram example ..................................................................................... 26
Figure 21. Code implementation ............................................................................................................. 27
Figure 22. Result of code ......................................................................................................................... 27
Figure 23. Data abstraction illustration .................................................................................................... 29
Figure 24. Process abstraction illustration ............................................................................................... 29
Figure 25. Abstraction class diagram example ......................................................................................... 31
Figure 26. Shape abstract class code implementation ............................................................................. 31
Figure 27. Circle class code implemetation .............................................................................................. 32
Figure 28. Rectangle class code implementation ..................................................................................... 32
Figure 29. Result of code ......................................................................................................................... 33
Figure 30. Types of UML diagrams ........................................................................................................... 35
Figure 31. UseCase of Inventory management system............................................................................. 38
Figure 32. Inheritance relationship .......................................................................................................... 39
Figure 33. Association relationship .......................................................................................................... 40
Figure 34. Aggregation relationship ......................................................................................................... 40


Figure 35. Composition relationship ........................................................................................................ 41
Figure 36. Implementation relationship................................................................................................... 41
Figure 37. Class diagram example ............................................................................................................ 42
Figure 38. Class diagram example ............................................................................................................ 45
Figure 39. Design pattern ........................................................................................................................ 46
Figure 40. Singleton class diagram ........................................................................................................... 49
Figure 41. Singleton class code implementation ...................................................................................... 50
Figure 42. Result of code ......................................................................................................................... 50

Figure 43. Lock code implementation ...................................................................................................... 51
Figure 44. Facade's class diagram ............................................................................................................ 52
Figure 45. Subsystems code implementation .......................................................................................... 53
Figure 46. Facade code implementation .................................................................................................. 53
Figure 47. Result of code ......................................................................................................................... 54
Figure 48. Command's class diagram ....................................................................................................... 55
Figure 49. Receiver class code implementation ....................................................................................... 55
Figure 50. ICommand and Concrete code implementation ...................................................................... 56
Figure 51. Sender class code implementation .......................................................................................... 57
Figure 52. Result of code ......................................................................................................................... 57
Figure 53. Lego bricks illustration ............................................................................................................ 58
Figure 54. Singleton pattern class diagram .............................................................................................. 59
Figure 55. Facade pattern class diagram .................................................................................................. 60
Figure 56. Command pattern class diagram ............................................................................................. 60
Figure 57. Airplane ticket system's class diagram .................................................................................... 61


Table of Tables
Table 1. Class and Object Comparison ..................................................................................................... 14
Table 2. Symbols of Use Case diagram ..................................................................................................... 36
Table 3. Relationship in Use Case diagram ............................................................................................... 36
Table 4. Notations of Sequence diagram ................................................................................................. 43


INTRODUCTION
In this report, I will explore the fundamental elements of the object-oriented programming model
(OOP) by applying object-oriented design and analysis to a hypothetical scenario. I will enlighten
various characteristics of OOP, such as encapsulation, inheritance, polymorphism, and abstraction. To
gain a deeper understanding of them, I will provide diagrams and code for each aspect.
The second part of the assignment involves introducing some types of UML diagrams for objectoriented analysis and design. These diagrams include use case and class diagrams. For each type of

diagram, I will offer examples and explanations.
The final objective is to present various design patterns, which encompass creational, structural,
and behavioral patterns. These patterns will be illustrated with diagrams, and I will explain how to
implement them using class diagrams and the C# language.


CHAPTER 1. OOP General Concepts
I. Object-oriented programming (OOP)
1. Definition
Object-oriented programming (OOP) is a way of designing computer programs. Instead of focusing on
functions and rules, it revolves around data and objects. An object is like a data container with special
traits and actions.
OOP is useful for big and complex programs that change often. It's great for things like making
software for manufacturing or mobile apps, even simulating manufacturing systems.
In OOP, programs are organized in a way that makes it easy for a group of people to work together.
This approach also promotes reusing code, adapting to different sizes, and being efficient.

Figure 1. OOP abbreviation

2. Benefits and drawbacks
There are several advantages of OOP including:




Modularity: Objects are like self-contained units, making it easier to find and fix problems, and
to work together on projects.
Reusability: You can reuse code, so you don't have to write the same stuff over and over.
Productivity: You can create new programs faster using pre-made code and libraries.







Upgradability and Scalability: You can update different parts of a program without messing up
the whole thing.
Security: Complex stuff is hidden, making it easier to keep software safe and protect online
data.
Flexibility: Objects can change and work in different ways, making them adaptable, and
different objects can use the same methods.

On the other hand, OOP also contains a certain number of disadvantages, they are:




Steep Learning Curve: OOP is tough to learn, especially for beginners. Concepts like
inheritance, polymorphism, and encapsulation can be tricky to grasp.
Overhead: OOP code can be wordy and might run slower. It often needs more memory and
processing power compared to other coding styles.
Complexity: OOP can make your code complex, especially in big systems with lots of linked
objects. This makes debugging and maintaining the code harder.

II. Object and class
1. Class
1.1 Definition
A class is like a custom-made plan or design for an object. It tells you what a particular type of object
will be like. When you describe a class, you include two things:




Attributes, which are like characteristics or properties, and
Methods, which are like actions or functions.

In object-oriented language, a class is like a template that defines what all objects of a certain type will
have in common. It brings together data and actions, which makes your code reusable, unlike in
procedural programming.
For instance, think of a mobile phone. It has attributes like a brand name and RAM, and it can do
things like texting and calling. So, a mobile phone is a class that represents all different phones (the
objects).


1.2 Class diagram
This MobilePhone class would include 2 attributes named “name” and “ram”, and 2 public methods
which are Texting and Calling.

Figure 2. Class diagram of MobilePhone

1.3 Code
The class diagram of MobilePhone has been applied to C# programing language as below. Base on the
diagram, type of attribute “name” is string and “ram” is int. Texting() and Calling() are methods which
types are void which do not return anything.

Figure 3. MobilePhone's code implementation


2. Object
2.1 Definition
An object is a single instance of a class, and it holds both data and the actions that work with that

data. So, an object has three parts:




Name: This is a variable name or identifier for the object.
Member data: These are the details that define the object, like its properties.
Methods: These are the functions that tell you what the object can do, its behaviors.

For instance, take an iPhone. It's an object with the name "Iphone X” and “2” GB of RAM as its
properties, and the behaviors of making calls and sending texts.

2.2 Code
Base on the class that was created at 1.3. Now, when class has initialized, an object is created from
that class with name “Iphone X” and ram “2” as below. When I set MobilePhone mobilePhone, at this
point this still a variable but after setting = new MobilePhone, a new object has been created.

Figure 4. Object's code implementation

3. Class and Object Comparison
A class is like a blueprint that describes what properties (characteristics) and behaviors (actions)
objects of the same kind will have. Every object is a specific example of a class and has those
properties and behaviors.
When we define classes, they're just ideas, not using any memory. They're like custom data types we
create. But when we actually create objects based on these classes, memory is set aside in the
computer's memory heap for these objects. This is where the data and actions associated with the
objects are stored.


Table 1. Class and Object Comparison


Class

Object

A class is a blueprint for declaring and creating
objects.

An object is a class instance that allows
programmers to use variables and methods from
inside the class.

Memory is not allocated to classes. Classes have
no physical existence.

When objects are created, memory is allocated to
them in the heap memory.

You can declare a class only once.

A class can be used to create many objects.

Class is a logical entity.

An object is a physical entity.

Classes can have attributes and methods defined.

Objects can have specific values assigned to their
attributes.


III. Encapsulation
1. Definition
Encapsulation is a method to limit direct access to certain parts of an object. It prevents users from
accessing all the internal details of an object, like its variables. With encapsulation, you can hide both
the data stored in an object and the functions that work with that data. This helps keep the inner
workings of an object private and controlled.

2. Advantages and disadvantages
The advantages of encapsulation are:




Data Protection: Encapsulation prevents the program from altering variables or data,
enhancing security and stability.
Flexibility: Encapsulated code is cleaner and adaptable. It can be made read-only or write-only
using getter and setter methods, which aids in code maintenance and debugging.
Reusability: Encapsulated methods can be easily modified and reused.

However, there are some disadvantages:


Code Size: Encapsulation can make code longer because you need to provide access methods
with specific specifiers.






More Instructions: With increased code size, you'll need to provide additional instructions for
each method.
Increased Code Execution Time: Encapsulation can lead to longer program execution times as
more instructions are added, requiring more time to execute.

3. UML and code
Aftern learning the encapsulation in OOP so that I will encapsulate two properties “name” and “ram”
with private keyword and access them via public properties Name and Ram.

Figure 5. MobilePhone's class diagram


As you can see, there are two private properties which are name and ram but they can be accessed
via two public fileds Name and Ram. This is a good example to illustrate for encapsulation in OOP

Figure 6. MobilePhone's code implementation

When I encapsulate the properties so they cannot be accessed outside its class and I set their
properties via Name and Ram.

Figure 7. Code implementation in Main


IV. Inheritance
1. Definition
Inheritance is a fundamental concept in object-oriented programming that enables code reuse by
inheriting the behaviors and data of an object. It means that a class can take on the characteristics and
methods of another class. In simpler terms, when one class inherits from another, it gains all the
attributes and methods of the original class.
The class that inherits from another is known as the subclass, child class or derived class, and the class

being inherited from is called the parent class, superclass, or base class.
It's important to note that some languages, like Go, use composition instead of inheritance.
Composition is an alternative approach where complex objects are created by combining or
aggregating other objects. Many object-oriented programming languages offer both composition and
inheritance for code organization.

2. Advantages and disadvantages
Advantages of Inheritance:





Code Reuse: Inheritance allows you to reuse common code among different classes.
Code Design: Inheritance is essential for designing well-structured code, especially when
multiple developers work on the same project.
Polymorphism: Inheritance enables different classes to be treated as objects of the same class,
promoting flexibility.
Encapsulation: It allows for hiding implementation details from external users, providing
privacy and structure through multiple levels of inheritance and private classes.

Disadvantages of Inheritance:






Tight Coupling: Changes in the parent class can impact the child classes, potentially causing
unintended issues. Proper class design is crucial to avoid this problem.

Fragile Base Class Problem: Incomplete base classes can lead to significant issues in derived
classes when changes are made to the base class.
Diamond Inheritance Problem: When multiple classes inherit from a common class and
another class inherits from two of them, conflicts can arise in deciding which function to
override. Some languages, like Java, don't support multiple inheritance due to this problem.
Inflexibility: Derived classes need to follow the structure of the base class, limiting their
independence compared to stand-alone classes.




Increase in Code Complexity: Excessive use of inheritance can make code collaboration difficult
and result in unnecessarily complex code structures.

3. Types of inheritance
OOPs support the six different types of inheritance, including:
1.
2.
3.
4.
5.
6.

Single inheritance
Multi-level inheritance
Multiple inheritance
Multipath inheritance
Hierarchical Inheritance
Hybrid Inheritance


3.1 Single inheritance
In this type of inheritance, a child class is derived from a single parent class.
In the provided example, Class A serves as the parent class, and Class B is the child class. Class B
inherits the attributes and behaviors of the parent class A, making Class A the superclass and Class B
the subclass.

Figure 8. Single Inheritance illustration

3.2 Multilevel inheritance
In this type of inheritance, a child class is created from another child class.
Class C inherits the properties and behavior of Class B, and Class B inherits the properties and
behavior of Class A. This creates a chain of inheritance. In this scenario, Class A is the top-level or
parent class, Class B is the child class of Class A, and Class C is the child class of Class B. This results in a
multilevel inheritance, and Class C implicitly inherits the properties and behavior of both Class A and
Class B.


Figure 9. Multi-Level inheritance illustration

3.3 Multiple inheritance
In this type of inheritance, a child class is created from more than one base class. It's commonly
known as multiple inheritance.
However, it's important to note that not all programming languages support multiple inheritance.
Languages like C#, F#, and Java do not support this type of inheritance.
In the example, Class C inherits the properties and behavior of both Class B and Class A, meaning both
Class A and Class B are considered parent classes for Class C, resulting in multiple inheritance.

Figure 10. Multiple inheritance illustration



3.4 Multipath inheritance
This type of inheritance, where a derived class is created from other derived classes sharing the same
base class, is referred to as multipath inheritance. However, it's important to note that certain
programming languages, such as C#, F#, and others in the .NET framework, do not support multipath
inheritance.
In the example, Class D inherits the properties and behavior of Class C and Class B, and both Class C
and Class B inherit from Class A. This results in Class A serving as the common base class for Class B,
Class C, and Class D, creating a multipath inheritance structure.

Figure 11. Multipath inheritance illustration

3.5 Hierarchical inheritance
In this type of inheritance, multiple derived classes are created from a single base class, and further
child classes can serve as parent classes for more than one child class. This results in a hierarchical or
tree-like structure of inheritance.
In the example:




Class A is the base class with two child classes, Class B and Class D.
Class B has two child classes, Class D and Class E.
Class C has two child classes, Class F and Class G.

This hierarchy demonstrates a complex form of inheritance, where multiple levels of parent-child
relationships are established.




×