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

Assignment 1 Advanced Programming

This document contains an assignment for an advanced programming unit. It includes instructions for the student to examine characteristics of object-oriented programming like abstraction, encapsulation, inheritance and polymorphism. It also asks the student to design class diagrams using UML for these characteristics and specific design patterns. The student is to define class diagrams for creational, structural and behavioral design patterns. Feedback and grading are also included.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Assignment 1 Advanced Programming

This document contains an assignment for an advanced programming unit. It includes instructions for the student to examine characteristics of object-oriented programming like abstraction, encapsulation, inheritance and polymorphism. It also asks the student to design class diagrams using UML for these characteristics and specific design patterns. The student is to define class diagrams for creational, structural and behavioral design patterns. Feedback and grading are also included.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

ASSIGNMENT 1 ADVANCED PROGRAMMING

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 Luu Van Trung Hieu Student ID GCD17213

Class GCD0820 Assessor name Hoang Nhu Vinh

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: ❒ Resubmission Feedback:

Grade: Assessor Signature: Date:


Lecturer Signature:
Table of Contents
I. Introduction:................................................................................................................................................................................................................................................................... 4
II. Body:................................................................................................................................................................................................................................................................................. 4
P1 Examine the characteristics of the object-orientated paradigm as well as the various class relationships. P2 Design and build class diagrams using a
UML tool.............................................................................................................................................................................................................................................................................. 4
M1 Determine a design pattern from each of the creational, structural and behavioural pattern types. M2 Define class diagrams for specific design
patterns using a UML tool............................................................................................................................................................................................................................................... 19
I. Introduction:
- In this assignment I define four characteristic of Object-oriented programming and draw class diagram of them.

II. Body:
P1 Examine the characteristics of the object-orientated paradigm as well as the various class relationships.
P2 Design and build class diagrams using a UML tool.
1. Object-oriented programming:[CITATION 19101 \l 1033 ]

- Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain
data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A
feature of objects is that an object's procedures can access and often modify the data fields of the object with which they
are associated (objects have a notion of "this" or "self"). In OOP, computer programs are designed by making them out of
objects that interact with one another. There is significant diversity of OOP languages, but the most popular ones are
class-based, meaning that objects are instances of classes, which typically also determine their type.

- Characteristic: [ CITATION 19101 \l 1033 ]

o Abstraction: It is the ability of the program to ignore or disregard some aspects of the information that it is directly
working on, it is able to focus on the core needs set each object serving as a "kinetic" can accomplish tasks internally,
reporting, changing its status, and communicating with other objects without telling how. The statue conducts the
operation. This is often referred to as the abstraction of data. Abstraction is also shown by the fact that an initial
object may have some common characteristics for many others as its extension but the original object itself may not
have the means of execution. This abstraction is usually defined in concepts called abstract classes or abstract base
classes.
o Encapsulation : This property does not allow user objects to change the state of an object. Only the object's internal
methods allow to change its state. Allowing an external environment to affect an object's internal data in any way is
entirely up to the coder. This is the nature to ensure the integrity of the object.

o Polymorphism: Expressed through the sending of messages. Sending these messages is comparable to calling the
inner functions of an object. The methods used to respond to a message will depend on the object to which the
message is sent will react differently. Programmers can define a property (example, through the names of methods)
for a series of objects that are close to each other, but when executed they use the same name, which executes each
object automatically. occur according to the characteristics of each object without being confused.

o Inheritance: This property allows an object to have available properties that other objects have inherited. This allows
objects to share or extend existing features without having to redefine them. However, not all object-oriented
languages have this feature.

- Example:

a. Abstraction:
- I created an abstract class Person that contains the sayHello method. This abstract class illustration abstraction, which
means that no matter who it is (Daddy and Children), there will be have a sayHello method.
UML class diagram of Abstraction:
Figure 1:Abstract Class Diagram
b. Inheritance:
- I created a base class named Person with name, age, salary and sayHello method. I created two classes Daddy and Children
inherit class Persson, Daddy and Children will inherit the name, age, salary and sayHello method of Person class.This
illsualtion Inheritance.
- UML Class diagram of inheritance:
Figure 2: Inheritance Class Diagram
c. Encapsulation:

- In the image above, I created the Person class that contains name and age variables will be private and the salary variable
will be protected.
- Encapsulation have :
o Private: method properties cannot be accessed outside the class, it is only called within the class.
o Protected: method properties cannot be accessed outside the class, it is only called in the derived class and class.
- The external elements cannot access the elements inside the Person class (here are name, age and salary), in order to be
accessible, I have created public methods to access the components Private of Person class.

- The getName and getAge methods are public so that external elements can access name and age.
- Protected is a protected method that is not accessible from outside the context of the object, but accessible from within the
context of the same object. Here salary can only be accessed from the Daddy class inheriting from Person class via
getSalary method.
- Class Children cannot be accessed because the class children class does not contain the getSalary method, although it
inherits the salary element of Person class. When adding the getSalary method to the Children class, an error will occur.
- UML Class Diagram :

Figure 3: Encapsulation Class Diagram


d. Polymorphism:

- I created a Family class to manage multiple people, there is an add method to add the objects of the Person inheritance class, the
showFamily() method to call sayHello() of all the objects it manages.The class Family only calls a sayHello method, but Person will
sayHello different.
- Override: Say this is an override method for the method inherited from the parent class.
- Overloading : Method overloading has several methods with the same name but different arguments in the same class.
- UML Class Diagram :
o Override:
Figure 4: Override class diagram

o Overloading:
Figure 5: Overloading

Access Modifier:

Access Modifiers Inside Assembly Outside Assembly


With Inheritance With Type With Inheritance With Type
Public x x x x
Private   x x
Protected  x  x
M1 Determine a design pattern from each of the creational, structural and behavioural pattern
types. M2 Define class diagrams for specific design patterns using a UML tool.
1. Definition Design Pattern:
- A design design is a total solution to common problems in software design. A design pattern is not a complete
design so that it can be directly converted into code; It is only a model describing how to solve problems that can
be used in many different situations.

- Design Patterns are not specific languages at all. It can be implemented in most programming languages, such as
Java, C #, even Javascript or any other programming language.

- Design patterns can help make our designs more flexible, easy to change and maintain.

2. Creational Pattern:

- Createtional Pattern are designs pattern that deal with object creation mechanisms, trying to create objects in a way
that fits the situation. The basic form of creating objects can lead to design problems or add design complexity. The
creative design solves this problem in some way controlling the creation of this object.

- Try to create objects in a way that fits the situation: Instead of "new someClass ()", use "template.Create ()"

- Creative designs include two key ideas. One is to encapsulate knowledge about the specific classes the system
uses. Another way is to hide the way the instances of these specific classes are created and combined.

- Have five type of Creational Pattern : Abstarct Factory Pattern, Builder Pattern, Factory Method Pattern, Prototype
Pattern and Singleton Pattern.
- I will use Abstract Factory Pattern to give an example:

2.1. Definition: Abstract Factory Pattern is an object-oriented pattern design in computer software design,
providing a class interface that creates a set of related or interdependent objects without specifying them as
specific classes. Abstract Factory design pattern encapsulates a group of classes that play a "production" role
in the application, which are the classes used to create objects. These production classes share a
programming interface inherited from a pure virtual parent class called "virtual production layer".

2.2. Why use it :

o Initializing Objects that hides the logical handling of creation.

o It alleviates the exchange of objects families.

o It promotes consistency between objects.

2.3. Scenario:

o I use a smartphone's information description program to illustrate the structure of Abstract Factory Pattern.

o The example here is implementing an Abstract Factory in the form of the IMobilePhone Interface, which has
methods that can create Smartphone object and Normal Phone objects. The client code against
IMobilePhone and gets ISmartPhone and INormalPhone interfaces.

o In the case of "Nokia", it creates a family of Nokia objects (SmartPhone and NormalPhone) and in the
"Samsung" case, creates a family of Samsung objects (SmartPhone and NormalPhone).

o IMobilePhone interface returns as it codes against ISmartPhone and INormalPhone interface.

2.4. Class Diagram:


Figure 6: Class Diagram
2.5. Source Code in C#:
namespace ConsoleApp4
{
interface INormalPhone
{
string GetModelDetails();
}
interface ISmartPhone
{
string GetModelDetails();
}
interface IMobilePhone
{
ISmartPhone GetSmartPhone();
INormalPhone GetNormalPhone();
}
class Nokia1080 : INormalPhone
{
public string GetModelDetails()
{
return "Model: Nokia 1080\nRAM: NA\nCamera: NA\n";
}
}
class NokiaPixel : ISmartPhone
{
public string GetModelDetails()
{
return "Model: Nokia Pixel\nRAM: 3GB\nCamera: 8MP\n";
}
}
class Nokia : IMobilePhone
{
public ISmartPhone GetSmartPhone()
{
return new NokiaPixel();
}

public INormalPhone GetNormalPhone()


{
return new Nokia1080();
}
}
class SamsungGalaxy : ISmartPhone
{
public string GetModelDetails()
{
return "Model: Samsung Galaxy\nRAM: 2GB\nCamera: 13MP\n";
}
}
class SamsungGalaxyS10 : INormalPhone
{
public string GetModelDetails()
{
return "Model: Samsung GalaxyS10\nRAM: NA\nCamera: NA\n";
}
}
class Samsung : IMobilePhone
{
public ISmartPhone GetSmartPhone()
{
return new SamsungGalaxy();
}

public INormalPhone GetNormalPhone()


{
return new SamsungGalaxyS10();
}
}
class MobileClient
{
ISmartPhone smartPhone;
INormalPhone normalPhone;

public MobileClient(IMobilePhone factory)


{
smartPhone = factory.GetSmartPhone();
normalPhone = factory.GetNormalPhone();
}

public string GetSmartPhoneModelDetails()


{
return smartPhone.GetModelDetails();
}

public string GetNormalPhoneModelDetails()


{
return normalPhone.GetModelDetails();
}
}
class Program
{
static void Main(string[] args)
{
IMobilePhone nokiaMobilePhone = new Nokia();
MobileClient nokiaClient = new MobileClient(nokiaMobilePhone);
Console.WriteLine("********* NOKIA **********");
Console.WriteLine(nokiaClient.GetSmartPhoneModelDetails());
Console.WriteLine(nokiaClient.GetNormalPhoneModelDetails());

IMobilePhone samsungMobilePhone = new Samsung();


MobileClient samsungClient = new MobileClient(samsungMobilePhone);

Console.WriteLine("******* SAMSUNG **********");


Console.WriteLine(samsungClient.GetSmartPhoneModelDetails());
Console.WriteLine(samsungClient.GetNormalPhoneModelDetails());

Console.ReadKey();
}
}
}

Figure 7: Result
3. Structural Pattern:
- Structure designs are designs that make it easy to design by simply identifying a relationship between entities.

- Structure patterns describe ways to assemble objects to perform a new function

o Identify how different classes and objects are combined to form larger structures.

o Structure class patterns use inheritance to edit interfaces or settings.

o The structured object templates compose objects for new functions.


- Have 6 popular type of Structural Pattern: Adapter Pattern, , Bridge Pattern, Composite Pattern, Decorator
Pattern, Façade Pattern, Flyweight Pattern.

- I will use Decorator Pattern to give an example:

3.1. Definition: is a design pattern that allows adding behavior to an individual object, without affecting the
behavior of other objects in the same class. This type of design has a structure that acts as a wrap for the
existing layer. Whenever a new feature is needed, the existing object is wrapped in a new object
(decorator class).

3.2. Why use it:

o It provides greater flexibility than static inheritance.

o It enhances the scalability of the object, because changes are made by coding new layers.

o It simplifies coding by allowing you to develop a range of functions from targeted classes instead of
coding all the behavior into the object.

3.3. Scenario:

o In this program I will illustrate the process of enchanting an item in a game such as Sword and Iron
armor, the program will return the name and value of the item.

3.4. Class Diagram:


Figure 8: Class Diagram

3.5. Source code in C#:

4. namespace ConsoleApp5
5. {
6. class Program
7. {
8. public interface IItem
9. {
10. string GetName();
11. int GetValue();
12. }
13. class Sword : IItem
14. {
15. public string GetName()
16. {
17. return "Iron Sword";
18. }
19.
20. public int GetValue()
21. {
22. return 20;
23. }
24. }
25. class Armor : IItem
26. {
27. public string GetName()
28. {
29. return "Iron Armor";
30. }
31.
32. public int GetValue()
33. {
34. return 50;
35. }
36. }
37. abstract class Enchantment : IItem
38. {
39. IItem _item = null;
40.
41. protected int _Value = 0;
42.
43. protected Enchantment(IItem baseItem)
44. {
45. _item = baseItem;
46. }
47.
48. public string GetName()
49. {
50. return (_item.GetName() + " +1 ");
51. }
52.
53. public int GetValue()
54. {
55. return (_item.GetValue() + _Value);
56. }
57. }
58. class Magic : Enchantment
59. {
60. public Magic(IItem baseComponent) : base(baseComponent)
61. {
62. this._Value = 30;
63. }
64. }
65. static void Main(string[] args)
66. {
67. IItem newItem = new Sword();
68. Console.WriteLine(newItem.GetName() + " Value: " + newItem.GetValue().ToString());
69.
70. newItem = new Magic(newItem);
71. Console.WriteLine(newItem.GetName() + " Value: " + newItem.GetValue().ToString());
72.
73. Console.ReadLine();
74. }
75. }
76. }

Figure 9: Result

4. Behavior Pattern:
- Behavioral design patterns are designs that identify common communication patterns between objects and
identify patterns. By doing so, these patterns increase the flexibility in making this communication.
- Behavioral patterns are concerned with communication (interaction) between the objects ,either with the
assignment of responsibilities between objects.Or encapsulating behavior in an object and delegating requests to
it.

- Have 10 popular type of Behavior Pattern: Chain of responsibility Pattern, Command Pattern, Iterator Patter,
Mediator Pattern,Memento Pattern, Observer Pattern, State Pattern, Strategy Pattern , Template Method Pattern
and Visitor Pattern.

- In this part I will use Strategy Pattern to give an example:


4.1. Definition:

- The strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables
selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time
instructions as to which in a family of algorithms to use. Strategy lets the algorithm vary independently from
clients that use it.Deferring the decision about which algorithm to use until runtime allows the calling code to be
more flexible and reusable.

4.2. Why use it:

o It provides an alternative to subclasses.

o It defines each behavior in its own class, eliminating the need for conditional statements.

o It makes it easy to extend and incorporate new behaviors without changing applications.

4.3. Scenario:

o I will use the program to sum and difference of two integers to illustrate the Strategy Pattern structure

4.4. Class Diagram:


Figure 10: Class Diagram

4.5. Source Code in C#:


namespace ConsoleApp6
{
public interface ICalculate
{
int Calculate(int A, int B);
}
class Minus : ICalculate
{
public int Calculate(int A, int B)
{
return A - B;
}
}
class Plus : ICalculate
{
public int Calculate(int A, int B)
{

return A + B;
}
}
class CalculateClient
{
private ICalculate calculateInterface;

public CalculateClient(ICalculate strategy)


{
calculateInterface = strategy;
}
public int Calculate(int A, int B)
{
return calculateInterface.Calculate(A, B);
}
}
class Program
{
static void Main(string[] args)
{
Figure 11:Result

You might also like