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 Ha Tuan Anh GCD17720 Student Name Student ID Nguyen Ngoc Khanh GCD18376 Gcd0602 Class Hoang Nhu Vinh Assessor name 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: Assignment 1 Advanced Programing Table of Contents 1. Introduction ..................................................................................................................................................................................................... 1 1.1. The purpose of Assignment ....................................................................................................................................................................... 2 2. Assigment ........................................................................................................................................................................................................ 4 2.1. Examine the characteristics of the object-orientated paradigm as well as the various class relationships. ............................................... 5 2.2. Design and build class diagrams using a UML tool. ................................................................................................................................ 6 2.3. Determine a design pattern from each of the creational, structural and behavioural pattern types. ......................................................... 6 2.4. Define class diagrams for specific design patterns using a UML tool. .................................................................................................... 6 3. Conclusion ....................................................................................................................................................................................................... 4 4. References ........................................................................................................................................................................................................ 4 1. Introduction 1.1. The purpose of Assigment Our group will explain characteristics of Object-oriented programming paradigm by applying Object-oriented analysis and design on a given (assumed) scenario. The scenario can be small but should be able to presents various characteristics of OOP (such as: encapsulation, inheritance, polymorphism, override, overload, etc.). The second task is to introduce some design patterns (including 3 types: creational, structural and behavioral) to audience by giving real case scenarios, corresponding patterns illustrated by UML class diagrams. To summarize, we analyze the relationship between the object-orientated paradigm and design patterns. 2. Assignment 2.1. Examine the characteristics of the object-orientated paradigm as well as the various class relationships Object-oriented programming (OOP) is a programming language model in which programs are organized around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior. Examples of an object can range from physical entities, such as a human being that is described by properties like name and address, down to small computer programs, such as widgets. This opposes the historical approach to programming where emphasis was placed on how the logic was written rather than how to define the data within the logic. - Characteristics of OOP: Abstraction: Abstraction is one of fours characteristics of object oriented programming languges (OOP), which is used to hide the implementation details and display only essential features of the object. We can hide the required details of object and expose only necessary methods and properties through the reference of object. In the real-words, laptop is the perfect sample for abstraction characteristic. A laptop includes many things such as processor, RAM, motherboard, LCD screen, camera, USB ports, battery, speaker, etc. To use it, we need to know how to operate the laptop by switching it on, we donot need to know how internally all the parts are working. By hiding its implementation details, the latop is an object which is designed to expose only required features. The perfect example for abstraction is Class. In C#, we can create a class with required methods, properties and we can expose only necessary methods and properties using access modifiers rely on our requirements. For example, we defined a class with required method, properties and exposing it by using access modifiers to reach abstraction functionality. using System; using System.Collections.Generic; using System.Text; namespace DemoAbstraction { class Laptop { private string model; private string brand; public string Model { get { return model; } set { model = value; } } public string Brand { get { return brand; } set { brand = value; } } public void LaptopDetails() { Console.WriteLine("The brand of laptop: " + Brand); Console.WriteLine("The model of laptop: " + Model); } public void LaptopKeyboard() { Console.WriteLine("Type using Keyword"); } private void MotherBoardInfo() { Console.WriteLine("MotheBoard Information"); } private void InternalProcessor() { Console.WriteLine("Processor Information"); } } } Figure 1 Laptop class. In Figure 1, we defined Laptop class with required fields, properties such as brand, model with public/ private access modifiers to display abstraction functionality by hiding and exposing some of methods and properties rely on our requirements. In these code, the public modifier allow a defined fields, properties and methods to access outside of the class and the private modifier is used to hide or restrict an access of required fields, properties and methods from the outside of class. using System; namespace DemoAbstraction { class Program { static void Main(string[] args) { Laptop laptop = new Laptop(); laptop.Brand = "Asus"; laptop.Model = "GTX"; laptop.LaptopDetails(); Console.WriteLine("\nPress Enter Key to Exit.."); Console.ReadLine(); } } } Figure 2 Abstraction Example representation. When we execute the Demo Abstraction in C#, we will get the result shown as the following figure. Figure 3 The result of Abstraction Example Representation. - Inheritance In simply definition, inheritance is about distributing responsibilities between two classes: parent and child. The child class inherits all the qualities of the parent class, so inheritance characteristic supports reusability. The inheritance allow us to create new class by inheriting the properties from other classes to reuse, extend and modify the behavior of class members based on our requirements. The class members are inherited is called a base (parent) class and the class that inherits the members of base (parent) class is called a derived (child) class. Next, the example of implementing an inheritance by defining two class in C# programming language. using System; namespace DemoInheritance { public class User { public string Name; public string Location; public User() { Console.WriteLine("Base Class Constructor"); } public void GetUserInfo() { Console.WriteLine("Name: {0}", Name); Console.WriteLine("Location: {0}", Location); } } public class Details : User { public int Age; public Details() { Console.WriteLine("Child Class Constructor"); } public void GetAge() { Console.WriteLine("Age: {0}", Age); } } class Program { static void Main(string[] args) { Details p = new Details(); p.Name = "Nguyen Ngoc Khanh"; p.Age = 21; p.Location = "Quang Binh"; p.GetUserInfo(); p.GetAge(); Console.WriteLine("\nPress Any Key to Exit.."); Console.ReadLine(); } } } Figure 4 The Example Inheritance Representation. From the Figure 4, we defined a base class called User and it inherited all the properties of the User class into derived class named Details and we are accessing all the member of User class with an instance of Details class. When executing above C# code, we give the result shown as the following figure. Figure 5 The result of example Inheritance representation. - Encapsulation Encapsulation is the process of combining elements to create a new entity for the purpose of hiding or protecting information. In object-oriented programming, packaging is an attribute of object design. That means all the object's data is contained and hidden in the object and access to it is restricted to members of that class. In C#, due to the class will combine a various type of data members and member functions into a single unit, so it is the real time example for encapsulation. The encapsulation is used to prevent an alteration of code (data) accidentally from the outside of function. We can protect the data from accidental corruption by defining the class fields with properties. When we defined a class fields with properties, we need to use getter and setter functions to read or write the data based on our requirements. For example encapsulation representation by defining an encapsulation class using properties with get and set accessors. using System; namespace DemoEncapsulation { class Person { private string location; private string name; private int age; public string Location { get { return location; } set { location = value; } } public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } } class Program { static void Main(string[] args) { Person person = new Person(); person.Name = "Duong Chieu Anh"; person.Location = "Quang Ngai"; person.Age = 19; Console.WriteLine("Name: " + person.Name+"\nAge: "+person.Age+"\nLocation: "+person.Location); Console.WriteLine("\nPress Enter Key to Exit.."); Console.ReadLine(); } } } Figure 6 The Example Encapsulation Representation. From the Figure 6, we defined a variables with private access modifiers and exposing those variables in public way by using properties get and set accessor. When executing the above C# code, we receive the result like the following figure. Figure 7 The result of Example Encapsulation Representation. - Polymorphism In simply definition, polymorphism is the ability of a parent to hold references to any of its children. A parent can be an interface, an abstract class or a complete class. Therefore, when calling from the parent, the desired method will be called for the appropriate child. In C#, there are two types of polymorphism available: o Compile Time Polymorphism o Run Time Polymorphism Compile Time Polymorphism Compile Time polymorphism is multiple methods with the same name but with different parameters. By using compile-time polymorphism, we can perform a different task with the same method name by passing different parameters. The compile time polymorphism can be run by using method overloading- called early binding or static binding using System; namespace DemoPolymorphism { class Program { public class Calculating { public void AddNumbers(int x, int y) { Console.WriteLine("x + y = {0}", x + y); } public void AddNumbers(int x, int y, int z) { Console.WriteLine("x + y + z = {0}", x + y + z); } } static void Main(string[] args) { Calculating subtend = new Calculating(); subtend.AddNumbers(12, 11); subtend.AddNumbers(14, 11, 99); Console.WriteLine("\nPress Enter Key to Exit.."); Console.ReadLine(); } } } Figure 8 The Example Compile Time Polymorphism Representation. In Calculating class, we defined two methods with the same name, but with different input parameters to reach method overloading. It is compile time polymorphism. Executing the code in Figure 8, we give the result like Figure 9 Figure 9 The result of Example Compile Time Polymorphism. Run Time Polymorphism Run time polymorphism overrides a base class method in the derived class by creating a similar function and this can be achieved by using overrides and virtual keywords along with inheritance rules. We can override a base class method in derived class by creating a method with same name and parameters to perform a different task. In c#, the run time polymorphism can be run by using method overriding and it is also called as late binding or dynamic binding. For example of Run Time Polymorphism using System; namespace DemoPolymorphism { // Base Class public class Person { public string name; public int age; public string location; public virtual void GetInfo() { Console.WriteLine("Name: "+name+"\nAge: "+age+"\nLocation: "+location); } } // Derived Class public class Details : Person { public string classname; public int numberphone; public override void GetInfo() { Console.WriteLine( "\nClass name: " + classname + "\nNumber phone: " + numberphone); } } class Program { static void Main(string[] args) { Person p = new Person(); p.name = "Khanh"; p.age = 21; p.location = "Quang Binh"; p.GetInfo(); Details s = new Details(); s.numberphone = 12563; s.classname = "154"; s.GetInfo(); Console.WriteLine("\nPress Enter Key to Exit.."); Console.ReadLine(); } } } Figure 10 The Example Run Time Polymorphism Representation We created a two classes (“Persons”, “Details”) and the derived class (Details) is inheriting the properties from base class (Person) and we are overriding the base class method GetInfo in derived class by creating a same function to achieve method overriding, this is called a run time polymorphism in c#. Here, we defined a GetInfo() method with virtual keyword in base class to allow derived class to override that method using override keyword. While executing the code, we give the result shown as the following figure. Figure 11 The result of example Run Time Polymorphism. 2.2. Design and build class diagrams using a UML tool. 2.3. Determine a design pattern from each of the creational, structural and behavioural pattern types. Creational pattern In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation. Design patterns of creational patterns are the Simple Factory: In object-oriented programming (OOP), a factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be "new". Factory Method: In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor. Abstract Factory: The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes Builder: The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern. Prototype: The prototype pattern is a creational design pattern in software development. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. Singleton: In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. Structural In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities. Design patterns of structural patterns are the Adapter: In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code. Bridge: The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently" Composite: The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently" Decorator: In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern. Facade: A facade is an object that provides a simplified interface to a larger body of code, such as a class library. Flyweight: In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. Proxy: A proxy, in its most general form, is a class functioning as an interface to something else. A proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. Behavioral In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication. Design patterns of behavioral patterns are the Chain of Responsibility In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. Command: In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters. Iterator In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled. Mediator In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior. Memento The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback). Observer The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. Visitor In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to follow the open/closed principle. Strategy In computer programming, the strategy pattern (also known as the policy pattern) is a behavioural software design pattern that enables an algorithm's behavior to be selected at runtime. State The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern's superclass. The state pattern can be interpreted as a strategy pattern which is able to switch the current strategy through invocations of methods defined in the pattern's interface. Template Method In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure. 2.4. Define class diagrams for specific design patterns using a UML tool. 3. Conclusion 4. References https://searchapparchitecture.techtarget.com/definition/object-oriented-programming-OOP Creational pattern - Wikipedia Structural pattern - Wikipedia Behavioral pattern - Wikipedia Factory (object-oriented programming) - Wikipedia Factory method pattern - Wikipedia Abstract factory pattern - Wikipedia Builder pattern - Wikipedia Prototype pattern - Wikipedia Singleton pattern - Wikipedia Adapter pattern - Wikipedia Bridge pattern - Wikipedia Composite pattern - Wikipedia Decorator pattern - Wikipedia Facade pattern - Wikipedia Flyweight pattern - Wikipedia Proxy pattern - Wikipedia Chain-of-responsibility pattern - Wikipedia Command pattern - Wikipedia Iterator pattern - Wikipedia Mediator pattern - Wikipedia Memento pattern - Wikipedia Observer pattern - Wikipedia Visitor pattern - Wikipedia Strategy pattern - Wikipedia State pattern - Wikipedia Template method pattern - Wikipedia