Chapter 2 Concepts of Object Orientation Contents Explain

Chapter 2: Concepts of Object Orientation

Contents Explain the basic principles of object orientation. Define the basic concepts of object orientation and the associated UML notation.

Why Model? Modeling achieves four aims: Helps you to visualize a system as you want it to be. Specify the structure or behavior of a system. Gives a template that guides you in constructing a system. Documents the decisions you have made. Model helps to better understand the system being developed

Object Orientation is about viewing and modelling the world/system as a set of interacting and interrelated objects.

What is Object-Oriented Modeling? Object-Oriented Modeling is a way of modeling systems in a language natural. Object-oriented modeling aims to structurally and functionally decompose a system into smaller units with less complexity and less responsibilities

Why use Object-Oriented Modeling? Clean conceptual modeling of domains Object oriented models provide a farmework within which one can formalize clean. Reducing Realization Costs Reducing the cost of implementation is facilitated by Improved communication between role players, Complexity reduction for complex domains. . Reducing Complexity

Why use Object-Oriented Modeling? Component Based Approach: aims for increases in Productivity, Quality and Re-use Reducing Risk of Failures Reduced Maintenance Costs Facilitating Dynamic Systems Availability of Analysis and Design Tools

Four principles of OO Modeling There are four basic principles of object orientation. They are: Abstraction (trừu tượng) Encapsulation (Đóng gói) Modularity Hierarchy

Abstraction A process allowing to focus on most important aspects while ignoring less important details. Abstraction allows us to manage complexity by concentrating on essential aspects making an entity different from others.

Encapsulation separates implementation from users. The key to encapsulation is an object’s message interface

Modularity breaks up complex systems into small, self contained pieces that can be managed independently. Billing System Course Catalog System Course Registration System Student Management System

Hierarchy Ordering of abstractions into a tree-like structure. Include various kinds: Aggregation hierarchy, Class hierarchy, Containment hierarchy, Inheritance hierarchy, Partition hierarchy, Specialization hierarchy.

OO Concepts 1. Object 2. Class 3. Attribute 4. Operation 5. Interface 6. Implementation 7. Association 8. Aggregation 9. Composition 10. Generalization 11. Super-Class 12. Sub-Class 13. Abstract Class 14. Concrete Class 15. Discriminator 16. Polymorphism 17. Realization

Objects Any abstraction that models a single thing A representation of a specific entity in the real world Examples: Television, specific citizen, agency, job, location, order, etc.

Object Definition Each real world object has Information and Behaviour. Information: Has a unique identity Has a description of its structure Has a state representing its current condition Behaviour: What can an object do? What can be done to it?

Object Definition

Class A class can be defined as: A description of a set of objects that share the same attributes, operations, relationships, and semantics. An object is an instance of a class. Rules that define objects A definition or template that describes how to build an accurate representation of a specific type of objects

Representing Classes in the UML A class is represented using a rectangle with compartments. Example

Attributes Attribute is a named property of a class describing a range of values that instances of the class may hold for that property. An attribute has a type and defines the type of its instances. Only the object is able to change the values of its own attributes. The set of attribute values defines the state of the object.

Attributes

Operation is the implementation of a service that can be requested from any object of a given class. An operation could be: A question: does not change the values of the attributes A command: may change the values of the attributes

Operation Example

The Relationship Between Classes & Objects A class is an abstract definition of an object. It defines the structure and behavior of each object in the class. It serves as a template for creating objects. The objects created from a class are also called the instances of the class. The class is the static description; the object is a runtime instance of that class. Classes are not collections of objects.

Questions Which of the following statements are true? For those that are false, explain why. 1) Trainee is an example of a class. 2) Dora Cheong is an example of a class. 3) Create Job is an example of an object. 4) Delete File is an example of an operation 5) Name is an attribute

Questions Suppose we wish to model an application for Course Registration System Identify: 1) Three classes for the model 2) At least three attributes for each class

Interface Minimum information required to use an object Allows users to access the object’s knowledge No direct access to object internals

Interface An interface represents a client's view of a service provider. It encapsulates the services required by the client as well as the messages which the client will send when requesting the services. Different service providers may implement the same interface and a client can switch between these service providers without changing the way in which the service is requested.

Implementation Information required to make an object work properly A combination of the behaviour and the resources required to satisfy the goal of the behaviour Ensures the integrity of the information upon which the behaviour depends

Relations and Links Relations: expresses relationships between classes Links: expresses relationships between objects

Relations and Links in UML

Three kinds of relations between classes Association The semantic relationship between two or more classifiers that specifies connections among their instances. In other words, an association is a structural relationship that specifies that objects (instances of classes) are connected to other objects Example

Three kinds of relations between classes Aggregation is a kind of association used to model wholepart relationships between things. Aggregation is used to model relationships between model elements. Example: Car needs a Wheel object to function. Meaning Car object own Wheel object but we cannot say Wheel has no significance without Car.

Three kinds of relations between classes Aggregation Example A CPU is part of a computer. CPU, devices, monitor and keyboard are assembled to create a computer.

Three kinds of relations between classes Composition It is a specialized form of Aggregation. It is a strong type of Aggregation. A composition is a stronger form of association in which the composite has sole responsibility for managing its parts Example House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house, room will automatically delete.

Three kinds of relations between classes Composition Example A word cannot exist if it is not part of a line. If a paragraph is removed, all lines of the paragraph are removed, and all words belonging to that lines are removed.

Generalization A relationship among classes where one class shares the structure and/or behavior of one or more classes The Generalization association ("is a") is the relationship between the base class that is named as “superclass” or “parent” and the specific class that is named as “subclass” or “child”.

Generalization Super-Class is a class that contains the features common to two or more classes. Sub-Class is a class that contains at least the features of its super-class.

Generalization in UML

Abstract Class A class cannot have its own (direct) instances Cannot be used to create objects Class must be declared as abstract. Use abstract classes when you need to provide some functionality but also wish to defer some implementation. In UML the method is shown in italics.

Concrete Class Has methods for all operations Can be instantiated Methods may be: Defined in the class or Inherited from a super-class

Dependency Change in structure or behaviour of a class affects the other related class A dependency relationship is a weaker form of relationship showing a relationship between a client and a supplier where the client does not have semantic knowledge of the supplier. Example

Realization relationship is specified interface and its implementation. between The realization relationship is like generalization in which only behavior specification is inherited rather than structure of attributes or implementation.

Realization Example

Questions Identify which of the following statements are true. For those that are false, explain why. There is an association between Trainee and Course There is a composition between Course and Professor There is an aggregation between Course and Venue

Questions Suppose we wish to model an e-service application for a government agency. Does this diagram reasonably model the relationship between the entities User, Employee, Front. Office Employee, Back -Office Employee and Applicant? If not, provide a more appropriate model.

Polymorphism The ability to hide many different implementations behind a single interface, or to dynamically choose the method for an operation at run-time By encapsulation and generalization: Encapsulation: separation of interface from implementation Generalization: organizing information such that the shared features reside in one class and unique features in another Operations could be defined and implemented in the superclass, but re-implemented methods Are in unique sub-classes.

Polymorphism Ford, Honda, Toyota, BMW, Benz etc. , Everything is of type Car. But each have their own advanced features and more advanced technology involved in their move behavior.

Polymorphism Example If we wanted to create a Boat and a Car class, we would certainly want to inherit from the Transport class, as all Boats can move, and all Cars can move

Review: Concepts of Object Orientation What are the four basic principles of object orientation? Provide a brief description of each. What is an object and what is a class? What is the difference between the two? What is a class relationship? Give some examples. What is polymorphism? What is an interface?

OO Analysis and Design A methodology for system modelling and design using Object-Oriented concepts. Consists of two parts: Object Oriented Analysis Object Oriented Design

Object Oriented Analysis emphasizes an investigation problem and requirements. of the Object-oriented analysis emphasizes on finding and describing the objects concepts in the problem domain. Concerned with developing an object-oriented model of the problem domain

Object Oriented Design emphasizes a conceptual solution that fulfills the requirements. Object-oriented design emphasizes on defining software objects and how they collaborate to fulfill the requirements. Provides an object-oriented model of a software system to implement the identified requirements.
- Slides: 52