ASM1 AdvancedProgramming
ASM1 AdvancedProgramming
Unit number and title Unit 19: Data Structures & Algorithms
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 P3 M1 M2 M3 D1 D2
r Summative Feedback: r Resubmission Feedback:
Bibliography ....................................................................................................................................... 33
Table of Figures.
Figure 1: Abstraction .................................................................................................................................................... 6
Figure 2: Abstraction .................................................................................................................................................... 7
Figure 3: Abstraction Result.......................................................................................................................................... 7
Figure 4: Abstraction class diagram .............................................................................................................................. 8
Figure 5: Encapsulation source code. ........................................................................................................................... 9
Figure 6: Encapsulation source code. ......................................................................................................................... 10
Figure 7: Encapsulation class diagram. ....................................................................................................................... 10
Figure 8: Inheritance source code. ............................................................................................................................. 11
Figure 9: Inheritance class diagram. ........................................................................................................................... 11
Figure 10: Polymorphism source code. ...................................................................................................................... 12
Figure 11: Polymorphism Result. ................................................................................................................................ 12
Figure 12: Polymorphism source code. ...................................................................................................................... 13
Figure 13: Polymorphism result. ................................................................................................................................. 13
Figure 14: Polymorphism class diagram. .................................................................................................................... 14
Figure 15: buidler pattern diagram............................................................................................................................. 16
Figure 16: buidler pattern class diagram. ................................................................................................................... 18
Figure 17: Result. ........................................................................................................................................................ 21
Figure 18: Flywiegh Class diagram. ............................................................................................................................. 22
Figure 19: Flywiegh class diagram. ............................................................................................................................. 24
Figure 20: Result. ........................................................................................................................................................ 27
Figure 21: Stragery class diagram. .............................................................................................................................. 29
Figure 22: Stragery class diagram. .............................................................................................................................. 30
Figure 23: Stragery result........................................................................................................................................... 32
I. Introduction.
- You and your team need to explain the characteristics of an object-oriented programming model by
applying object-oriented analysis and design on a (hypothetical) scenario. The script may be small but
can show the various characteristics of OOP (such as: encapsulation, inheritance, polymorphism,
override, overload, etc.).
- The second task is to introduce a number of designs (including 3 types: creativity, structure and
behavior) to the audience by giving real case scenarios, the corresponding patterns are illustrated by
UML class diagram.
- To summarize, you should analyze the relationship between object-oriented models and design
patterns.
II. P1&M1 Examine the characteristics of the object-orientated paradigm as well as the various
class relationships.
Object-oriented is a process of using class to showcase each different functional areas or objects of data in
a software application. The data objects contained in its data fields (attributes), and the method used to
define the manipulation with data. (Hieu, 2017)
There are some characteristics :
1. Abstraction
Abstraction is the concealment of internal details and displays functions such as attributes, methods of an
object for other classes can use. In other words, abstraction is giving out the underlying information of
objects in the real world and hides the whole details of an object.
Example :
Figure 1: Abstraction
In the above example, I created 3 object classes which are Animal, Dog and Cat, where Animal class is
abstract class. The animal class has an abstract method called Action () and Introduction () which is used to
display the action and introduce each corresponding animal. Because it is abstract, every method in the
Animal class is required to appear in the inherited classes of Dog and Cat classes, and we can change the
value in that method by overriding it. In addition, in the two classes Dog and Cat, although using the same
method from the Animal class, the declaration inside their methods is different, so it clearly shows the
abstract in each class. In particular, Class Animal is an abstract class, so it can not initialize objects like
normal classes. It is just a rib, so that we can create subclasses based on the binding from this rib, which is
Class Dog and Class Cat. This makes maintenance easier. The image below is the Main function we built
from the image above.
Figure 2: Abstraction
2.Encapsulation
Demonstrates the scope of use of classes, methods, and properties. These ranges are represented by access
modifiers: puplic, protected, private. (which ensures the integrity and security of the object).
The image below is the access level of each Access Modifier included in OOP
Example :
In the Dog class, we wanted the Age field to be accessible within this class so we used private as the access
modifer for this Age field. This makes it inaccessible outside the scope of the Dog class and if you want to
access Dog outside the program, there must be an intermediary wall that is the public Age variable. In the
Animal class, we want the name field to be accessed only within the scope of that class and also to the
derived class, so we use protected as the access modifer for this name field. Therefore, in the Dog class that
inherits the Animal class, it is still possible to use this field to declare the name for this object. In addition,
the Age field is used privately as an access modifer, so even though Dog class inherits Animal class, it still
cannot access it.
In the Dog class, the GetAge () and GetName () methods use the public as an access modifer, so its access
scope may be outside the scope of the class. The image below illustrates an object via the main function.
Figure 6: Encapsulation source code.
In the main function, when initializing a Dog object, we can access methods using public easily and private
and protected cannot be accessed because it has been limited in scope.
UML for encapsulation:
In the image above, the Cat class inherits the Animal class so all properties and fields in the Animal class
will be included in the Dog class. Therefore, in the Act () method, the age value in the Animal class will be
given and the Dog class to declare, so when the Main function is launched, the Act () method declaration
will give the value of the age wall in the Animal class even though it is printed in Dog class.
UML for Inheritance
In the Dog class, we use the address() method to declare the address for the object, but we want each object
to be declared with a different number of input variables. Therefore, for each address method, we declare
2-3 input variables for them and when giving the main function to run, they have the following result.
In the Animal class, we use the Eat () method, and also in the Dog class, we don't want to record the content
of the Eat () method in the Animal class so we used the override method to override it. The content contained
in this method in the Animal class is different from the content in the Dog class. The program after executing
the override is executed as follows.
- The problems you encounter may come up with a solution yourself, but it may not be optimal. Design
Pattern helps you solve problems in the most optimal way, providing you with solutions in OOP
programming. It is not a specific language at all. Design patterns are possible in most programming
languages. We often encounter it in OOP programming. (Giang, 2019)
- It is a collection of more optimized, proven solutions to solve problems in software engineering. So when
you encounter any difficulties, design patterns are a guide to help you solve a problem instead of finding a
solution for a proven problem yourself.
- The design pattern provides a solution in general form, which helps speed up software development by
introducing test and development models that have been tested.
- Reusing design patterns helps avoid potential problems that can cause major errors, and are easy to upgrade
and maintain later.
- Help programmers understand the code of others quickly (can be interpreted as communicating). All team
members can easily exchange with each other to build projects without taking too much time.
• Creational Pattern (creation group) includes: Abstract Factory, Factory Method, Singleton,
Builder, Prototype. It will help you in object creation, as you know to create you must use the new
keyword, the Creational Pattern team will use some tricks to initialize the object that you will not
see this keyword.
• Structural Pattern (structure group) includes: Adapter, Bridge, Composite, Decorator, Facade,
Proxy and Flyweight. It is used to establish and define relationships between objects.
• Behavioral Pattern includes: Interpreter, Template Method, Chain of Responsibility, Command,
Iterator, Mediator, Memento, Observer, State, Strategy and Visitor. This group is used in the
implementation of object behaviors.
3.1 Creational Pattern :
a ) Definition :
- Creational pattern aim to separate a system from the way its objects are created, composed and performed.
They increase the flexibility of the system about what, who, how and when to create objects.
- There are many design patterns in the creational pattern, but we have chosen one to explain and draw
diagrams to illustrate them and explain why they are used.
• Definition: Separate the construction of a complex object from its representation so that the same
construction process can create different representations. (Khanh, 2019)
+ Builder : specify the abstract interface to create parts of the Product object
+ ConcreteBuilder
+ Product :
- The Builder design pattern helps us cut the operations of building an object. It focuses on building a
complex object step by step. It also implements a process to create an object as a finished product. That
means an object must be massaged by a number of guided steps before it is ready and can be used by
others.
- In general, it allows you to encapsulate complex creation logic. Suppose we are developing a solution to
build a large soccer field in different seasons according to the application user's choice. We will create a
constructor class and pass season information object in the constructor class. After that, the construction
class is responsible for building a seasonal school.
- The construction model is very similar to the factory model. The main difference between builders and
factories is that builders are useful when you need to do a lot of work to build an object.
There is a laptop manufacturing company that specializes in producing two types of laptops as follows:
• Gaming laptop
• Normal laptop
Because the features of the two laptops are different, the built-in accessories in both laptops are different,
so the way it is built will be required to produce these laptops.
Here, we will create the objects of normal gaming and laptop computers using the build designs in C #.
Class Diagram
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Builder
{
// 'Product' class
class Laptop
{
public string ModelNumber { get; set; }
public string Display { get; set; }
public string RAM { get; set; }
public string GraphicsCard { get; set; }
public string TouchScreen { get; set; }
Laptop getLaptop();
}
// 'ConcreteBuilder' class - implements 'Builder' interface
class GamingLaptopBuilder : ILaptopBuilder
{
Laptop laptop = new Laptop();
public void addModelNumber()
{
laptop.ModelNumber = "Gaming1001";
}
public void addDisplay()
{
laptop.Display = "Full HD display";
}
public void addRAM()
{
laptop.RAM = "16 GB";
}
public void addGraphicsCard()
{
laptop.GraphicsCard = "Nvidia GeForce";
}
public void addTouchScreen()
{
laptop.TouchScreen = "Yes";
}
public Laptop getLaptop()
{
return laptop;
}
}
// 'ConcreteBuilder' class - implements 'Builder' interface
class NormalLaptopBuilder : ILaptopBuilder
{
Laptop laptop = new Laptop();
public void addModelNumber()
{
laptop.ModelNumber = "Normal1001";
}
public void addDisplay()
{
laptop.Display = "SD Display";
}
public void addRAM()
{
laptop.RAM = "2GB";
}
public void addGraphicsCard()
{
laptop.GraphicsCard = "No graphics card";
}
public void addTouchScreen()
{
laptop.TouchScreen = "NO";
}
public Laptop getLaptop()
{
return laptop;
}
}
// 'Director' class
class LaptopManufacturer
{
public void BuildLaptop(ILaptopBuilder laptopBuilder)
{
laptopBuilder.addModelNumber();
laptopBuilder.addDisplay();
laptopBuilder.addRAM();
laptopBuilder.addGraphicsCard();
laptopBuilder.addTouchScreen();
}
}
class Program
{
static void Main(string[] args)
{
// create object of manufacturer
LaptopManufacturer laptopManufacturer = new LaptopManufacturer();
// build Gaming Laptop
ILaptopBuilder gamingLaptopBuilder = new GamingLaptopBuilder();
laptopManufacturer.BuildLaptop(gamingLaptopBuilder);
Laptop GamingLaptop = gamingLaptopBuilder.getLaptop();
// print details
Console.WriteLine("Gaming Laptop Object:");
GamingLaptop.PrintDetails();
Console.ReadLine();
}
}
}
After executing with the c # code is complete. We run the program and the results are printed as the image below
- There are many design patterns in the Structural pattern, but we have chosen one to explain and draw
diagrams to illustrate them and explain why they are used.
• Definition: In computer programming, fly weight is a software design pattern. A weight is an object
that minimizes memory usage by sharing as much data as possible with other similar objects; It's a
way to use objects in large numbers when a simple repetitive representation will use an unacceptable
amount of memory. (NONE, không ngày tháng)
+ Flyweight : declares an interface through which flyweights can receive and act on extrinsic state.
+ ConcreteFlyweight : implements the Flyweight interface and adds storage for intrinsic state, if any. A
ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic, that is, it must be
independent of the ConcreteFlyweight object's context.
+ UnsharedConcreteFlyweight : not all Flyweight subclasses need to be shared. The Flyweight interface
enables sharing, but it doesn't enforce it. It is common for UnsharedConcreteFlyweight objects to have
ConcreteFlyweight objects as children at some level in the flyweight object structure
+ FlyweightFactory :
+ Client :
- Class diagram:
}
public class Context
{
private SoldierFactory()
{
throw new ArgumentException ;
}
After executing with the c # code is complete. We run the program and the results are printed as the image
below
-There are many design patterns in the Behavioral pattern, but we have chosen one to explain and draw
diagrams to illustrate them and explain why they are used.
• Definition: Define a set of algorithms, package them one by one and make it interchangeable.
Strategy makes the algorithm independent of its use in client computers. Enables abstraction in an
interface, detailed implementation in the root class. (NONE, 2019)
Figure 21: Stragery class diagram.
+ Strategy : declares an interface common to all supported algorithms. Context uses this interface to call
the algorithm defined by a ConcreteStrategy
+ Context :
- Strategy Pattern is to help separate the handling of a specific function from the object. Then create a set of
algorithms to handle that function and choose which algorithm we find the best fit when executing the
program. This design pattern is often used as a replacement for inheritance, when it wants to end tracking
and editing a function across multiple subclasses.
- When desired, the algorithms used within an object can be changed at run-time.
- When there is a piece of code that is easy to change, and wants to separate it from the main program for
easy maintenance.
- Avoid the hassle of having to implement a certain function through so many subclasses.
Class Diagram
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StrategyPatternDemo
{
//'IStrategy' interface
public interface IEncodingStrategy
{
void EncodeValue(string value);
}
//'ConcreteStrategyA' class
public class RSAEncodingStrategy : IEncodingStrategy
{
public void EncodeValue(string value)
{
// Implement Encoding strategy
Console.WriteLine("Value {0} is Encoded using RSA Algorithm", value);
}
}
//'ConcreteStrategyB' class
public class DESncodingStrategy : IEncodingStrategy
{
public void EncodeValue(string value)
{
// Implement Encoding strategy
Console.WriteLine("Value {0} is Encoded using DES Algorithm", value);
}
}
//'ConcreteStrategyC' class
public class BlowFishEncodingStrategy : IEncodingStrategy
{
public void EncodeValue(string value)
{
// Implement Encoding strategy
Console.WriteLine("Value {0} is Encoded using BlowFish Algorithm", value);
}
}
//'Context' class
public class Encoding
{
private IEncodingStrategy _encodeStrategy;
class Program
{
static void Main(string[] args)
{
IEncodingStrategy encodingStrategy = new RSAEncodingStrategy();
Encoding encoding = new Encoding(encodingStrategy);
encoding.Encode("10000011110");
Console.ReadLine();
}
}
}
After executing with the c # code is complete. We run the program and the results are printed as the image
below