ASM1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

I.

Introduction
In this first assignment we will be introducing different aspects of Object-Oriented-
Programming such as their general concepts, real-life scenarios to utilise OOP (With
diagrams and explanation). And from that we will provide 3 design patterns from 3
different categories for a closer inspection.

II. OOP General Concepts:


1. OOP Introduction:
Object-oriented programming (OOP) is a programming approach that
organises software design around data, rather than functions and logic. An
object is a data field that has distinct features and behaviour.

OOP focuses on the objects that developers want to manipulate rather than the
logic required to manipulate them. This programming style is ideal for large,
sophisticated projects that are constantly updated or maintained. This
encompasses industrial and design software, as well as mobile applications;
for example, OOP can be used to create manufacturing system simulation
software.(Manish, 2022)

2. OBJECT

The software is divided into a number of small units called objects. The data
and functions are built around these objects.

The data of the objects can be accessed only by the functions associated with
that object.

The functions of one object can access the functions of another object.

Objects are the basic run-time entities of an object oriented system. They may
represent a person, a place or any item that the program must handle.

No memory will be used by a class. As a result, in order to deal with the data that the
class represents, you must construct a variable for the class, which is referred to as
an object.
When an object is created with the new operator, memory for the class is allocated
on the heap, the object is referred to as an instance, and its starting address is
stored in the object in stack memory.

When memory is not allocated to the heap when an object is formed without the new
operator, an instance is not generated and the object in the stack has the value null.

When an object contains null, it is not feasible to use that object to access the class's
members.

All the programming languages supporting Object Oriented Programming will be


supporting these three main concepts,

1. Encapsulation
2. Inheritance
3. Polymorphism

3. Abstraction

In order to reduce something to a set of fundamental qualities, something must be


abstracted from certain attributes. Abstraction is one of the three fundamental
concepts of object-oriented programming (along with encapsulation and inheritance).
A programmer reduces complexity and boosts performance by hiding all but the
pertinent information about an item through the abstraction process. The thing that is
left is a depiction of the original with extraneous detail removed, similar to how
abstraction can occasionally work in art. The finished product itself is known as an
abstraction, which is a named entity made up of certain features and behaviors that
are unique to a given application of the originating entity. Encapsulation and data
hiding are both connected to abstraction.(Ivy,2022)

Real-world Example of Abstraction

Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as in the following:

Nokia 1400 (Features: Calling, SMS)


Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)

Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording,


Reading Emails)

Abstract information (necessary and common information) for the object "Mobile
Phone" is that it makes a call to any number and can send SMS.

So that, for a mobile phone object you will have the abstract class as in the following

Abstraction means putting all the variables and methods in a class that are
necessary.

For example: Abstract class and abstract method.

4. Encapsulation

Encapsulation refers to an object's ability to hide data and behaviour that are not
necessary to its user. Encapsulation enables a group of properties, methods and
other members to be considered a single unit or object.
The following are the benefits of encapsulation:

- Protection of data from accidental corruption


- Specification of the accessibility of each of the members of a class to the
code outside the class
- Flexibility and extensibility of the code and reduction in complexity
- Lower coupling between objects and hence improvement in code
maintainability

Encapsulation is used to restrict access to the members of a class so as to prevent


the user of a given class from manipulating objects in ways that are not intended by
the designer. While encapsulation hides the internal implementation of the
functionalities of class without affecting the overall functioning of the system, it
allows the class to service a request for functionality and add or modify its internal
structure (data or methods) to suit changing requirements.(Ivy,2022)

5. Inheritance

The ability to build a class that inherits characteristics and behaviors from an
existing class is known as inheritance. The current class is the base (or parent)
class, while the newly generated class is the derived (or child) class.

One of the main components of object-oriented programming is inheritance. One of


the reasons object-oriented programming can take the role of structured
programming is due to the advantages of inheritance.

In C#, inheritance is planned at the interface and implementation levels.


Implementation inheritance refers to the reuse of code from one class in another,
whereas interface inheritance merely refers to the inheritance of function signatures
from one type to another.
For example, a child includes the properties of its parents.

Output

Parent Constructor.

Child Constructor.

I'm a Parent Class.

6. Polymorphism

The ability of objects of various types to offer a distinctive interface for various
method implementations is known as polymorphism. It is typically employed in the
context of late binding, in which an object's response to a call to one of its method
members is predicated at runtime on the basis of the object type. Because of
polymorphism, derived classes' methods can be redefined.

Along with encapsulation and inheritance, polymorphism is one of the essential


notions of object-oriented programming.

Differences between Abstraction and Encapsulation

Abstraction Encapsulation
1. Abstraction solves the problem 1. Encapsulation solves the problem at the
at the design level. implementation level.
2. Abstraction hides unwanted2. Encapsulation means hiding the code and data
data and provides relevant data. into a single unit to protect the data from the
outside world.
3. Abstraction lets you focus on3. Encapsulation means hiding the internal details
what the object does instead ofor mechanics of how an object does something.
how it does it
4. Abstraction: Outer layout, used
4. Encapsulation- Inner layout, used in terms of
in terms of design.
implementation.
For example:
An external of a Mobile Phone, like For example: the internal details of a Mobile
it has a display screen and keypad Phone, how the keypad button and display screen
buttons to dial a number. are connected with each other using circuits.

III. OOP Scenario

1. Scenario
We want to use the university library system as an example for OOP. This system
would contains 3 actors:
Book borrower, librarian and admin. The borrower has the ability to login, search
books, borrow books, order books, return books and pay a fine if they borrow books
overdue. The librarian has abilities to login, search books,insert data, check for
borrowed books and statistical borrowers. The admin has abilities to login, search for
all users’ information and manage them.

2. UML diagrams
UML is an abbreviation for Unified Model Language, it is an approach to modelling
documents. It works based on diagram representations of software, a type of UML
that can be used to replace diagrams. They provide a way to model work as well as a
wider range of features to improve readability and efficacy. There are many types of
UML diagrams, each of which is used for different purposes. The two most broad
categories that encompass all other types are Behavioural UML diagram and
Structural UML diagram. There are also many other types of small diagrams in these
two main UML categories. In this report, I will design some typical diagrams.
2.1. Use-case diagram

To present a brief look at what the system can do, we need to draw out a use-case
diagram - a diagram showing how users in the system can interact with the system via
the features. Thus, and according to the above information, we can have the following
use-case diagrams

Use-case 1: Management system


Use-case 2: User system
Use-case 3: Librarian system

The diagrams above include three main entities: Book Borrowers, librarians and
admin. Admin will be responsible for managing the entire system and managing all
accounts. He also has the right to check account information of librarians if necessary.
Librarians and borrowers who want to use the library will have to have an account,
log into the system to be able to search for books.

3. Class Diagram
4. Design Patterns

According to Christopher Alexander, Design Pattern is a technique in object-oriented


programming, it is quite important and every programmer who wants to be good must know.
Used frequently in OOP languages. It will provide you with "design patterns", solutions to
common problems, commonly encountered in programming. The problems you face may be
your own to come up with a solution, but it may not be optimal. Design Pattern helps you
solve problems in the most optimal way, providing you with solutions in OOP programming.
Design Patterns are not language specific at all. It can be done in most programming
languages, such as Java, C#, even Javascript or any other programming language.

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides
published a book titled Design Patterns – Elements of Reusable Object-Oriented Software,
which is the origin of the concept of design patterns in programming. software.

These four authors are widely known as the Gang of Four. From the point of view of the four
people, the design pattern is mainly based on the following rules of object-oriented design.
Program for an interface, not to implement that interface. Prefer object composition over
inheritance.

The Design pattern system currently has 23 patterns defined in the book "Design patterns
Elements of Reusable Object Oriented Software" and is divided into 3 groups:

4.1. Creational pattern

- Creational 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: Abstract Factory Pattern, Builder Pattern,
Factory Method Pattern, Prototype Pattern and Singleton Pattern.

- I will use Abstract Factory Pattern to give an example:

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".

Scenario:

- I use a smartphone's information description program to illustrate the structure of Abstract


Factory Pattern.

- 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.

4.2. 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.
Structure class patterns use inheritance to edit interfaces or settings. 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:

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).
Scenario: 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

4.3. Behavioral pattern

These design patterns are all about Class's objects communication. Behavioral patterns are
those patterns that are most specifically concerned with communication between objects.

- Chain of responsibility: A way of passing a request between a chain of objects


- Command: Encapsulate a command request as an object
- Interpreter: A way to include language elements in a program
- Iterator: Sequentially access the elements of a collection
- Mediator: Defines simplified communication between classes
- Memento: Capture and restore an object's internal state
- Null Object: Designed to act as a default value of an object
- Observer: A way of notifying change to a number of classes
- State: Alter an object's behavior when its state changes
- Strategy: Encapsulates an algorithm inside a class
- Template method: Defer the exact steps of an algorithm to a subclass
- Visitor: Defines a new operation to a class without change

Observer Pattern is one of the Patterns belonging to the Behavioral Group (Behavior Pattern).
It defines one-to-many dependencies between objects so that when an object changes state,
all its dependencies are notified and updated automatically.(Ivy, 2022)

Observers can register with the system. When the system has a change, the system will notify
the Observer. When no longer needed, the Observer pattern will be removed from the system.

Observer Pattern is also known as Dependents, Publish/Subscribe or Source/Listener.


Components participating in the Observer Pattern:

Subject : contains a list of observers, providing methods to add and remove observers.

Observer : defines an update() method for objects that will be notified by the subject until a
state change occurs.

ConcreteSubject : implements Subject methods, stores the state of a list of


ConcreateObservers, and sends notifications to its observers when there is a change in state.

ConcreteObserver : implements Observer methods, stores the subject's state, and performs
updates to keep the state consistent with the subject sending the notification.

The interaction between the subject and the observers is as follows: whenever the subject has
a state change, it traverses its list of observers and calls a method that updates the state on
each observer, possibly passing itself to the method. mode so that observers can retrieve its
state and process it.

SCENARIO

An enterprise that exports food, fuel, and goods needs to manage their prices, which are
related to tax increases and decreases. We have designed a system so that businesses can
know the value of the products they manage.
Tax will act as subject and foodpricenoti, fuelpricenoti, shippingnoti will act as obsevers.
When the tax changes, the other 3 observers will receive information that the tax has changed
and the notify function will be executed.

5. Design Pattern vs OOP

Observer pattern

Often used in 1-n relationships between objects. Where an object changes and wants to notify
all related objects about that change.

When changing one object, it is required to change another object and we do not know how
many objects need to be changed, who these objects are.

Use in broadcast-type communication applications.

Used for event management.

Use in MVC (Model View Controller Pattern): in MVC, this pattern is used to separate
Model from View. View represents Observer and Model is Observable object.

References:
Vrat Agarwal, V., 2021. Object Oriented Programming Using C# .NET. [online] C-sharpcorner.com.
Available at: https://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-
C-Sharp-net/

[Accessed 5 September 2022].

Gillis, A., 2021. What is Object-Oriented Programming (OOP)?. [online] SearchAppArchitecture.


Available at: https://www.techtarget.com/searchapparchitecture/definition/object-oriented-
programming-OOP

[Accessed 5 September 2022].

Introduction to Object Oriented Programming Concepts in C# (2022). Available at: https://www.c-


sharpcorner.com/UploadFile/mkagrahari/introduction-to-object-oriented-programming-concepts-
in-C-Sharp/ (Accessed: 6 September 2022).
What is Abstraction? - Definition from WhatIs.com (2022). Available at:
https://www.techtarget.com/whatis/definition/abstraction#:~:text=Abstraction%20(from%20the
%20Latin%20abs,a%20set%20of%20essential%20characteristics. (Accessed: 6 September
2022).

You might also like