Download Object-Oriented Programming (OOP) Concepts and Design Patterns and more Assignments Advanced Education in PDF only on Docsity!
ASSIGNMENT 1
Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 20: Advanced Programming Submission date 29/05/2023 Date Received 1st submission 29/05/ Re-submission Date Date Received 2nd submission Student Name Thai Van Chien Student ID GCH 210162 Class GCH1103 Assessor name Nguyen Duc Giang 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 D
Summative Feedback: Resubmission Feedback:
Grade: Assessor Signature: Date: Lecturer Signature:
- I. Introduction
- II. OOP General Concepts
- OOP..............................................................................................................................................................................................................................................
- Class
- Object
- Constructor
- Abstraction............................................................................................................................................................................................................................
- Encapsulation.......................................................................................................................................................................................................................
- Inheritance
- Polymorphism
- Interface
- The various class relationships
- a) Inheritance
- b) Realization / Implementation
- c) Composition relationship
- d) Aggregation Relationship
- e) Association Relationships
- f) Dependencies
- III. OOP scenario
- Scenario..................................................................................................................................................................................................................................
- Usecase Diagram
- Class Diagram.......................................................................................................................................................................................................................
- IV. Design pattern
- Introduce Design Pattern
- 2 Structural Pattern...............................................................................................................................................................................................................
- V. Design pattern vs OOP
- VI. Conclusion
- Bibliography
- Figure 1 Example Class ______________________________________________________________________________________
- Figure 2 Example object ______________________________________________________________________________________
- Figure 3 Example constructors _________________________________________________________________________________
- Figure 4 Example Abstraction ________________________________________________________________________________
- Figure 5 Encapsulation ______________________________________________________________________________________
- Figure 6 Inheritance ________________________________________________________________________________________
- Figure 7 Single Inheritance ___________________________________________________________________________________
- Figure 8 Mutiple Inheritance _________________________________________________________________________________
- Figure 9 Hybrid Inheritance __________________________________________________________________________________
- Figure 10 Polymorphism_____________________________________________________________________________________
- Figure 11 Example Interface __________________________________________________________________________________
- Figure 12 Class Relationships _________________________________________________________________________________
- Figure 13 Example Inheritance ________________________________________________________________________________
- Figure 14 Example Realization / Implementation _________________________________________________________________
- Figure 15 Example Composition relationship ____________________________________________________________________
- Figure 16 Example Aggregation Relationship ____________________________________________________________________
- Figure 17 Example Association Relationship _____________________________________________________________________
- Figure 18 Example Dependencies _____________________________________________________________________________
- Figure 19 Use-case diagram __________________________________________________________________________________
- Figure 20 Class diagram _____________________________________________________________________________________
- Figure 21 Decorator Pattern __________________________________________________________________________________
- Figure 22 Decorator Pattern for Project _________________________________________________________________________
- Figure 23 Activity Diagram for User ___________________________________________________________________________
- Table 1: Usecase for Add Product _____________________________________________________________________________
- Table 2: Usecase for View List Product _________________________________________________________________________
- Table 3: Usecase for Login ___________________________________________________________________________________
- Table 4: Usecase for Edit Product _____________________________________________________________________________
- Table 5: Usecase for Search Product ___________________________________________________________________________
- Table 6: Usecase for Delete Product ____________________________________________________________________________
- Table 7: Usecase for View Order ______________________________________________________________________________
- Class In object-oriented programming, a class serves as a custom-designed structure or template for an object, specifying its characteristics and defining its appearance. A class description consists of two essential components: Member variables or attributes, and Member functions or behavior implementations (Nishad, 2023). In object-oriented terminology, a class acts as a blueprint that establishes the common variables and methods shared by all objects belonging to a specific category. It facilitates the integration of data and methods, promoting code reusability unlike in procedural languages. When declaring a class, the elements typically appear in the following sequence:
- Modifiers: These determine the accessibility of the class, such as whether it is public or has default permissions.
- The "class" keyword: This keyword is used to define and construct a class.
- Class name: The name of the class, with the convention of capitalizing the first letter.
- Superclass (if applicable): If the class has a parent class (superclass), its name is specified followed by the keyword "extends". A class can only inherit from a single parent at a time.
- Interfaces (if applicable): If the class implements any interfaces, they are listed in a comma-separated manner after the keyword "implements". It is possible for a class to implement multiple interfaces.
- Body: The body of the class is enclosed within curly braces, defining the members, variables, and methods that constitute the class. For example, consider a car. It possesses attributes such as gasoline and speed, as well as functions such as steering, speed adjustment, and refueling. Thus, cars represent a class that includes many different types of cars, which are individual objects.
Figure 1 Example Class
- Object Objects are the fundamental building blocks of Object-Oriented Programming and represent real-life entities. In a typical Java application, a large number of objects are created, and they interact with each other through methods (Logicmojo, 2023). The following components constitute an object:
- State: It refers to the characteristics or qualities of an object, also known as its attributes. The state of an object defines its current condition or values.
- Behavior: Behavior is expressed through the methods of an object. It encompasses the actions and operations that an object can perform, as well as its interaction with other objects.
- Identity: Each object possesses a unique identity that distinguishes it from other objects. It provides a distinct name or reference to the object and enables connections and relationships with other objects.
For example, the code in Figure 3 is the car class which contains the model, color, and year properties. In addition, the car constructor The constructor for the Car class encapsulates the behavior of creating a new Car object and setting its properties. There are different types of constructors used in programming, including the following:
- Default Constructor: A default constructor is a constructor that does not take any parameters. It is automatically generated if no other constructor is defined in the class. The default constructor initializes the object with default values.
- Parameterized Constructor: A parameterized constructor is a constructor that accepts parameters. It allows you to initialize the object with specific values passed as arguments during object creation.
- Copy Constructor: A copy constructor is a constructor that creates a new object by copying the values of an existing object. It is used to create a copy of an object, preserving the state of the original object.
- Private Constructor: A private constructor is a constructor that is only accessible within the class itself. It is used to restrict the creation of objects from outside the class. Private constructors are often used in singleton design patterns.
- Static Constructor: A static constructor is a special constructor that is called only once when the class is first accessed. It is used to initialize the static members of the class. Static constructors are automatically invoked and cannot be called explicitly.
- Abstraction Figure 4 Example Abstraction
Abstraction is a key principle in object-oriented programming that involves selectively revealing essential attributes while concealing irrelevant information. The primary objective of abstraction is to hide unnecessary details from users. It entails extracting relevant data from a larger collection and presenting only the essential aspects of an object to the user. This approach aids in reducing programming complexity and minimizing effort. Abstraction stands as one of the fundamental and crucial concepts in object-oriented programming (Hartman, 2023).
- Encapsulation In object-oriented programming (OOP), encapsulation is the practice of bundling related data into a structured unit, along with the methods used to work with that data. Most OOP languages implement encapsulation primarily through classes and the objects instantiated through those classes. A class defines a set of attributes for the data and the methods used to carry out operations related to the data (Sheldon, 2023). Figure 5 Encapsulation Through encapsulation, a class hides the implementation details of the programmed elements, while restricting direct access to those elements. It also provides a public interface for interacting with the class through its instantiated objects. In this sense, a class acts as a template for creating objects that can potentially access the same set of related methods and attributes.
In object-oriented programming (OOP), there exist five types of inheritance: single inheritance, multilevel inheritance, hierarchical inheritance, multiple inheritance (through interfaces), and hybrid inheritance (through interfaces). Here are the details of each type of inheritance: Single Inheritance: In single inheritance, subclasses inherit the features of one superclass. In image below, the class A serves as a base class for the derived class B. Figure 7 Single Inheritance Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In below image, class A serves as a base class for the derived class B, C, and D. Multiple Inheritance(Through Interfaces): In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes.
Figure 8 Mutiple Inheritance Hybrid Inheritance(Through Interfaces): It is a mix of two or more of the above types of inheritance. Figure 9 Hybrid Inheritance
- Polymorphism Polymorphism, derived from the Greek word meaning "having many forms," is a fundamental concept in object-oriented programming. It is commonly represented by the principle of "one interface, multiple functions." (Tutorialspoint, 2023).
- Interface An interface in programming is similar to a class in that it can have methods, properties, events, and indexers as its members. However, interfaces only contain the declaration of these members, without providing their implementation. The implementation of interface members is done by the class that explicitly or implicitly implements the interface (Kumar, 2020).
- Interfaces specify what a class must do and not how.
- Interfaces can’t have private members.
- By default all the members of Interface are public and abstract.
- The interface will always defined with the help of keyword ‘interface‘.
- Interface cannot contain fields because they represent a particular implementation of data.
- Multiple inheritance is possible with the help of Interfaces but not with classes. Figure 11 Example Interface
For example, an interface named "Shape" is defined with an abstract method called "draw()". The class "Circle" implements the Shape interface by providing an implementation for the draw() method. An instance of the Circle class is created and assigned to a variable of type Shape, and the draw() method is invoked, resulting in the message "Drawing a circle" being printed to the console.
- The various class relationships There are six primary types of relationships between classes in object-oriented programming: inheritance, realization/implementation, composition, aggregation, association, and dependency. These relationships are represented by specific arrows to indicate their nature (Visual Paradigm, 2022). Figure 12 Class Relationships Among the six types of relationships, the code structure for composition, aggregation, and association is similar as they all involve using attributes to store references to another class. Therefore, it is crucial to differentiate them based on the nature of their relationship and content.
a) Inheritance
Inheritance, also known as generalization, represents the relationship between a parent class and its child class. The parent class is often referred to as the base class, while the child class is known as the derived class.
Figure 14 Example Realization / Implementation
c) Composition relationship
Composition refers to the relationship between a whole and its parts, where the parts cannot exist independently without the whole. The composition relationship depicts the association between a class and its constituent parts, with both the whole and the parts having a shared lifespan. If the overall object ceases to exist, the associated parts will also cease to exist, as they are interdependent and have a unified lifecycle. For instance, a person consists of a head and a body. The head and body are inseparable components, existing together as a cohesive unit. Figure 15 Example Composition relationship
d) Aggregation Relationship
Aggregation pertains to the relationship between a whole and its parts, where the parts can exist independently without the whole. Aggregation represents the connection between a class and its member objects, where the member objects are part of the overall object but have the ability to exist independently. For instance, in the context of bus drivers, their work clothes and hats are associated with them as parts of their overall identity. However, these work clothes and hats can be separated from the specific driver and used by other drivers. Similarly, a bus driver can wear different work clothes and hats that are not specifically linked to them. Figure 16 Example Aggregation Relationship
e) Association Relationships
Association refers to the relationship where a property of a class holds a reference to an instance or instances of another class. Association is the most commonly used relationship between classes, signifying a connection between objects of different types. Compositions and aggregations also fall under the umbrella of association, but association relationships between classes are generally weaker than the other two. There are four types of associations: two-way associations, one-way associations, self-associations, and multiple-end associations. Figure 17 Example Association Relationship