It provides an overview of the phases involved in object-oriented analysis and design.. The object-oriented methodology in software development revolves around a single concept called th
Trang 1This chapter introduces object-oriented methodology and discusses the concepts of objects, classes, messages, and methods It provides an overview of the phases involved in object-oriented analysis and design This chapter also explains how to define classes in C#
In this chapter, you will learn to:
Explain features of the object-oriented
Trang 3Object-orientation is a software development methodology that is based on modeling a real-world system An object is the core concept involved in object orientation An object
is the representation of a real-word entity or concept For example, an employee, a
window, a car, or a bird can be modeled as objects You can think of an object-oriented model as a collection of objects and their inter-relationships
Object-orientation is a type of methodology used for building software applications An object-oriented program consists of classes, object, and methods
If you were asked to create a classroom, you will start by creating a room and defining its boundaries and features Next, you will put together all the components, such as chairs, tables, and books Similarly, if you want to create an aircraft or a high-rise building, you can do so by putting together several components or parts These components or parts are analogous to objects in the object-oriented methodology
The object-oriented methodology in software development revolves around a single concept called the object Software is developed by breaking the application into
component objects These objects interact with each other when the whole application is put together
An object is a combination of messages and data Objects can receive and send messages
and use messages to interact with each other The messages contain information that is to
be passed to the recipient object
Objects
An object literally means a ‘material thing’ that is capable of being presented to the senses For our purpose, an object is a tangible entity that may exhibit some well-defined behavior For example, let us consider a tennis ball:
A tennis ball is a tangible entity, with a visible boundary
A tennis ball has a specific defined purpose (such as bouncing)
You can direct a specific action towards a tennis ball by hitting it with a racquet or
by tossing it
But the definition of an object is not limited to merely something that can be seen, held, and touched, such as a tennis ball or a car For the purpose of software development, the definition of an object needs refinement For example, consider the Acme Nut and Bolt Company An organization does not have a visible boundary, unlike a tennis ball While it
Object-Oriented Methodology
The Foundation of Object-Orientation
Trang 4does not possess a physical boundary, it does have a conceptual boundary Like all organizations, it has a specific defined purpose, and one can direct a specific action towards it Thus, the Acme Nut and Bolt Company is an object
State, Behavior, and Identity of an Object
According to Grady Booch, a renowned software architect, an object has the following characteristics:
It has a state
It may display behavior
It has a unique identity
The state of an object is indicated by a set of attributes and their values For example, a chemical can be characterized by its temperature, color, and density The behavior of a chemical refers to the change of its attributes over a period of time A chemical also has state, such as solid, liquid, or gas
The preceding paragraph explains the first two points of the definition of an object by Grady Booch “An object has state, exhibits some well-defined behavior…” Now, you will examine the next part of the definition - “…and has a unique identity.”
Each object has a unique identity, and the identity of an object distinguishes the object from all other objects
For example, you can take a car as an object It can have states such as moving and stationary It can accelerate, decelerate, turn right, or turn left, which is its behavior The car also has an identity, such as a unique registration number The following figure depicts the characteristics of a car
An Object Has State, Behavior, and Identity
Two objects may have the same behavior and state, but they will never have the same identity The identity of an object does not change in its lifetime For example, two tennis balls may have the same color, be made of the same material, have the same weight and the same circumference, and display the same behavior However, they will have distinct
Trang 51 States of a cell phone: Off, Ring, Vibrate, and Call
2 States of a stereo: Play, Pause, Rewind, and Forward
All animals and birds, which are essentially objects, can be classified on the basis of their common attributes For example, the peacock, the sparrow, and the kingfisher are all birds All of them share characteristics that are common to the family of birds All of them lay eggs, are covered with feathers, have hollow bone structures, and have the ability to fly Therefore, they share structural and behavioral similarities and belong to the class called Birds
Trang 6This is shown in the following figure
The Class - Birds
From the preceding example, you may define a class as a declaration, a template, or a blueprint that can be used to classify objects Similarly, Book is a class and ‘Gone with the Wind’ and ‘Farewell to Arms’ are the objects of this class
Consider a video game that involves two players, Tom and Mark, who fight with each other Tom chooses a shotgun and Mark chooses a pistol as their weapons in the game If you need to create an object model for this game, you can identify a total of four objects They are Tom, Mark, the shotgun, and the pistol
The object shotgun belongs to the class Firearms It has certain attributes that are common with the pistol However a shotgun is a unique object Both the shotgun and the pistol are examples or instances of Firearms Therefore, the shotgun and pistol are objects of the same class, as shown in the following figure
Classes and Objects
Messages and Methods
Objects do not exist in isolation They interact with other objects These interactions take place through messages Grady Booch has defined behavior as follows:
“Behavior is how an object acts and reacts, in terms of its state changes and message passing.”
In the example of a video game, every time Tom attacks, Mark either attacks or defends himself
Trang 7Just a minute:
When Tom attacks Mark, Mark receives a message and reacts to that message The reaction can be exhibiting a particular behavior such as running, hiding, dodging, or fighting back For example, when Tom attacks, Mark receives a message “Being
Attacked” Mark responds to this message by hiding In this situation, hiding is a behavior exhibited by Mark Therefore, behavior (also called method in object-oriented
methodology) is simply a set of actions taken by the receiving object in response to a message
Dr James and Mr Hyde went to the railway station to book tickets for 3rd December
At the railway station, they requested the clerk at the ticket counter to book two tickets for the Flying Express in the first class Identify the following:
1 The possible receiver of the message in this situation
2 The possible method that the receiver can use
Answer:
1 The receiver of the message in this case will be the clerk at the ticket counter
2 The clerk will check if two tickets are available on the requested train in the
desired class and for the desired date If the tickets are available, the clerk will enter the details (name, age, departure date, and seat), confirm the reservation, and collect the required fare
Trang 8As discussed earlier, an object has a state, behavior, and identity An object is reusable Therefore, an object exists as a stand-alone entity that can be used in any relevant context For example, if you have a chair, you can use the same chair in an office, in a classroom,
or in a garden
An existing object can be used to create a similar object For example, you have a simple wooden chair and you need to add a headrest to it You do not need to create a new chair because you can easily add a headrest to the existing chair This is known as resilience to change
Realistic Modeling
Because you live in a world of objects, it logically follows that the object-oriented
approach models the real world more accurately The object-oriented approach allows you
to identify entities as objects having attributes and behavior Attributes and behavior typically depict how the object acts and reacts For example, the car is an object belonging
to the class Vehicle The car has attributes such as speed, color, and power It displays behavior such as being stationary, moving slowly, or accelerating
Reusability
In the software industry, using existing classes or objects from other applications saves resources spent in recreating the classes from scratch Consider the following analogy: Carcare is a leading car manufacturing company Carcare manufactures two-seater cars that are very popular among the consumers aged between 20 and 24 The company has decided to enter the market of family segment market and has decided to manufacture a four-seater model
Carcare can either design a new car for the family segment or convert the two-seater model to a four-seater model The cost involved in designing a new model is very high as compared to the cost involved in converting the two-seater model to a four-seater model
Characteristics of the Object-Oriented Approach
Trang 9Just a minute:
Therefore, the management and the design team may decide to convert the two-seater model into a family segment model, as shown in the following figure
Example of Reusability
The preceding scenario depicts the concept of reusability that is supported by the
object-oriented approach The process of creating a new class by adding some features to
an existing class is known as inheritance The benefit of reusability translates to savings
in time and effort, which in turn results in cost benefits
State whether the following situations demonstrate reusability:
2 It represents reusability because a pump can be used for suction of water as well
as petrol It is not necessary to use the same pump in both the cases Two separate machines can be used because both belong to the ‘Pump’ class
Two-seater Car
Attributes Behavior
Four-seater Car Two-seater Car: Attributes
Two-seater Car: Behavior New Attributes New Behavior
Trang 10In the object-oriented system, this requirement does not mean that the new car needs to be built from scratch The new features can be easily incorporated in the old toy car without modifying the color, shape, and sound of the old toy car, as shown in the following figure
Illustration of Resilience to Change
Resilience to change also results in easier maintenance This feature of object-oriented methodology is known as extensibility The ability of a class to inherit features from another class also makes object-oriented programs more extensible For the same reason, even during construction, parts of the system under development can be refined without any major changes to other parts
Existence as Different Forms
Using the object-oriented approach, objects can be made to respond differently to the same message The response is decided based on the information or parameters provided with the message
Old Toy Car
Color Shape Sound
New Toy Car
Color Shape Sound
Remote Control Flash Lights
Trang 11Note
Information is passed as parameters in a function For a detailed study of functions and
parameters, refer to the subsequent chapters
If a car collides against an object, the behavior of the car after the collision will depend on the speed of the car and nature of the object that hit the car For example, if the car
collides with another car at high speed, both the cars will be smashed and the drivers might get injured On the other hand, if a car collides with a street light at a slow speed, the impact would be less This ability to react differently based on the information
associated with the message is known as polymorphism.
Trang 12Before the actual development of any product, important steps such as analysis and design need to be performed The following phases are involved in the object-oriented
methodology:
The Analysis phase
The Design phase
The Implementation phase
Consider an aircraft manufacturing factory where a new jetliner is being assembled and several competent engineers and workmen are working with specialized tools Can you visualize them working on pure intuition, grabbing the tools, and sitting down to work immediately? Or, do you see them working according to a detailed design on which they have spent hundreds of hours, and finally constructing the aircraft based on the specified design
For the safety of the millions of people who fly every day, it is recommended to use a detailed design After all, it is comforting to know that a lot of thought has gone into first making a model on paper and then translating the design into the physical jetliner The same thing holds true for almost any item that is constructed Similarly designers put pen
to paper before attacking the fabric with scissors Architects spend hours drawing layouts
of buildings on blueprints before the bricks are laid
Therefore the construction of software follows the same approach The software industry still relies mainly on the informal paper-and-pencil approach in the upstream development phases
It is this paper-and-pencil approach that is termed analysis and design To analyze and design a system, you need to build a model of the system This model is simpler than the system that is finally constructed All the practical aspects of building a system for the real world cannot be reflected in the design However, this does not undermine the
importance of design
The Analysis Phase
“The purpose of analysis is to provide a description of a problem The description must
be complete, consistent, readable, and reviewable by diverse interested parties, and testable against reality.”- Mellor
The analysis or the Object-Oriented Analysis (OOA) phase considers the system as a solution to a problem in its environment or domain Broadly, analysis is the phase where users and developers of the system get together and arrive at a common understanding of
Phases of Object-Oriented Methodology
Trang 13the system One of the end products of the analysis phase is the specification on how a system functions
In the analysis phase, the developer concentrates on obtaining as much information as a possible about the problem The developer has to identify the critical requirements Analysis, therefore, involves meeting and interviewing the concerned persons to
understand systems that are currently in operation, and identifying all the causes of problems (if any) in the current system
The Design Phase
In the design phase, the developers of the system document their understanding of the system Design generates the blueprint of the system that is to be implemented
The first step in creating an Object-Oriented Design (OOD) is the identification of classes and their inter-relationships
Work in the design phase of software development is comparable to the work of an architect The architect will need to create a blueprint or a model of the building before the construction of the building starts
The Implementation Phase
The design phase is followed by OOP, which is the implementation phase OOP provides specifications for writing programs in a programming language One of the most popular languages used to write object-oriented programs is C#
During the implementation phase, programming is done as per the requirements gathered during the analysis and design phases
Many of the modern applications are built by using OOP Developing complex,
large-scale business systems can be simplified by using OOP techniques Some of the applications that can be built by using OOP techniques are Computer Aided Design (CAD), Computer Aided Manufacturing (CAM), and Object-Oriented Databases