ASM1
ASM1
ASM1
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.
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.
1. Encapsulation
2. Inheritance
3. Polymorphism
3. Abstraction
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.
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:
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.
Output
Parent Constructor.
Child Constructor.
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.
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.
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
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
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:
- 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.
Definition:
Scenario:
- 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.
- Structure designs are designs that make it easy to design by simply identifying a
relationship between entities.
- Have 6 popular type of Structural Pattern: Adapter Pattern, Bridge Pattern, Composite
Pattern, Decorator Pattern, Façade Pattern, Flyweight 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.
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.
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.
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.
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 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/