1 Software Engineering Lecture 15 Object Oriented Software Design in Java.
-
Upload
victor-chapman -
Category
Documents
-
view
213 -
download
0
Transcript of 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.
![Page 1: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/1.jpg)
1
Software Engineering
Lecture 15
Object Oriented Software Designin Java
![Page 2: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/2.jpg)
2
Analysis leads to Design
goals of the system scenarios properties of interest
Analysis
Design
identify the main events, actions and interactions
identify and define the main processes
identify and define the properties of interest
structure the processes into a design architecture
check properties of interest
![Page 3: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/3.jpg)
3
Design leads to the Design Document
Design
Design Document
identify the main data design (structures)
identify component-level design
identity software interface design
identity user interface design
![Page 4: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/4.jpg)
4
Design leads to Source Code
Design
Java
identify the main active entities
identify the main (shared) passive entities
structure the classes as a class diagram
![Page 5: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/5.jpg)
5
Software Design Strategies
FUNCTIONAL OBJECT-ORIENTED DECOMPOSITION DESIGN
The solution is expressed
in terms of objects
(self-contained entities
composed of data and
operations on that data) that
interact by sending messages
to one another.
The problem is divided into
more easily handled
subproblems, the solutions
of which together create a
solution to the overall
problem.
![Page 6: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/6.jpg)
6
Object-Oriented Design
A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.
TextField
setText. ..
getText
Private dataand
methods
setLayout
add
pack. ..
show
Frame
Private dataand
methods
TextField Frame
![Page 7: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/7.jpg)
7
Objects to Classes
• A class defines the pattern used when instantiating
an object of that type.
• A class generally contains private data and public
operations (called methods).
![Page 8: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/8.jpg)
8
Superclass and Subclass
• Inheritance enables us to define a new class (called a subclass) that inherits the properties of an already existing class.
• The newly derived class is then specialized by adding properties specific to it.
• The class being inherited from is the superclass.• The class that inherits properties is the subclass.
![Page 9: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/9.jpg)
9
Defining Objects
• An object-oriented program consists of many objects.
• An object is composed of identity, state (attributes, data, and their current values) and behavior (operations) .
![Page 10: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/10.jpg)
10
Identity, State, Behavior
• Identity is the property of an object that distinguishes it from all other objects.
• The failure to recognize the difference between the name of the object and the object itself is the source of many errors in object-oriented (OO) programming.
![Page 11: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/11.jpg)
11
Identity, State, Behavior
• The state of an object encompasses all of the (static) properties of the object plus the current (dynamic) values of each of these properties
• A property is an inherent or distinctive characteristic, trait, quality, or feature that contribute to making an object uniquely that object
• We will use the word attribute, or data member, to refer to the state of an object
![Page 12: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/12.jpg)
12
Examples of State
• Properties• Elevators travel up or down• Vending machines accept coins• Clocks indicate the current time
• Values• Current floor• Number of coins deposited• The number of minutes since the last hour
![Page 13: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/13.jpg)
13
Identity, State, Behavior
• Behavior is how an object acts and reacts, in terms of state changes and interactions with other objects.
• An operation is some action that one object performs upon another in order to elicit a reaction.
• We will use the word method to describe object behavior in java.
• Invoking a method causes the behavior to take place.
![Page 14: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/14.jpg)
14
Classes
• Classes are the definitions (or blueprints) used to create objects. I’d say: descriptions of objects.
• To make a car the manufacturer must first have a design from which to build the first car. Then, once all the problems are worked out, the design is used to build all the cars of that model.
![Page 15: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/15.jpg)
15
Objects
• An object is an instance of a class.
• If we have a class definition called Car, then we can think of Audi, BMW, and Corvette as each being an instance (object) of the class Car, i.e., they are each a type of car.
![Page 16: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/16.jpg)
16
Object example
Audi 6 BMW Z3 Corvette
• Notice that all objects are of the same type. All objects are cars!
Car Car Car
![Page 17: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/17.jpg)
17
Classes and Objects
• An object is an instance of exactly one class• Corvette can not be an instance of a car class
and an instance of a plane class at the same time.
• An instance of a class, an object, belongs to that particular class.• A Corvette is a car Corvette belongs to the
class Car.
![Page 18: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/18.jpg)
18
Classes
• Once a class is defined you can create as many instances of the class (objects from the class) as you would like.
• Once a blue print is completed for the 2003 Porsche 911, Porsche will use an assembly line to build as many instances of the 2003 Porsche 911 as they wish.
![Page 19: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/19.jpg)
19
Defining a class
• Properties are variables which describe the essential characteristics of an object.• Properties of a car: color, model, make, how
many doors, transmission type, direction of movement, etc.
• Behaviors are methods that describe how the object behaves and how the properties may be modified. • Behavior of a car: braking, changing gears,
opening doors, moving forwards or backwards, etc.
![Page 20: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/20.jpg)
20
Defining a class
Behavior or Method 1
Behavior Behavior or or
Method 4 Method 2
Behavior or Method 3
Properties
or
Instance Variables
![Page 21: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/21.jpg)
21
Instance variables
• The class definition will include parameter definitions (properties) that represent data about a particular object, instance variables.
• Example, Joe's car may have 4 gallons of gas in it while John's car has 10 gallons.
• The amount of gas in each car may change without affecting the amount of gas in the any other cars.
• All instances (objects) of a class will have a set of instance variables that are specific to that individual object.
• The combination of the values of these instance variables is known as the object’s state.
![Page 22: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/22.jpg)
22
Instance variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
![Page 23: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/23.jpg)
23
Class variables
• The class definitions may also include parameter definitions that represent data that is shared by all class instances (objects), called class variables.
• In the case of the car class, we will define a maximum allowed speed, by the law (variable MaxSpeed). This will be the same for each individual car.
![Page 24: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/24.jpg)
24
Class variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
MaxSpeed=155
![Page 25: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/25.jpg)
25
Class variables
• Class variables may also be used to keep track of things such as how many instances of a class exist.
• Example: let’s create a counter the records how many cars are in the garage.
![Page 26: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/26.jpg)
26
Class variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
MaxSpeed=155
NumCars = 3
![Page 27: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/27.jpg)
27
Messages
• For Objects• The object to whom the
message is being sent.• The name of the method
that object is to execute.• Any parameters
(variables) needed by that method.
For Humans• Who the message is for.• What we want the
person to do.• What information is
needed to do it.
Audi 6 • turnOnHazard()
![Page 28: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/28.jpg)
28
Messages and Methods
• In order to process a message, an object needs to have a method defined for the requested task.
• A method is a small, well-defined piece of code that completes a specific task.
• For our previous example, we need to define a method to turn on the car's hazard lights.
![Page 29: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/29.jpg)
29
Messages and Methods
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155
turnOnHazard()
MaxSpeed = 165
turnOnHazard()
MaxSpeed = 145
turnOnHazard()
MaxSpeed
MaxSpeed=155
NumCars = 3
turnOnHazard()
![Page 30: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/30.jpg)
30
Instance methods
• Each class can have methods that are specific to each object, called instance methods.
• These can only affect that object's parameters, i.e., it’s instance variables.
• Example: If BMW has 4 gallons of gas and someone puts 6 more gallons of gas in his/her car, the car now has 10 gallons. The amount of gas in Audi and Corvette is unchanged.
![Page 31: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/31.jpg)
31
Messages and Methods
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155
turnOnHazard()addGass(amount)
MaxSpeed = 165
turnOnHazard()addGass(amount)
MaxSpeed = 145
turnOnHazard()addGass(amount)
MaxSpeedMaxSpeed=155NumCars = 3turnOnHazard()addGass(amount)
![Page 32: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/32.jpg)
32
Methods
• It is also possible that you want information from an object; in this case, you would define a method that sends (returns) a message back to the requester containing that information.
• We need to know how much gas is in our cars, so we will create a new method that returns the value of GasLevel variable for our car.
![Page 33: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/33.jpg)
33
Messages and Methods
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155GasLevel = 4
turnOnHazard()addGass(amount)getGasLevel():GasLevel
MaxSpeed = 165GasLevel = 10
MaxSpeed = 145GasLevel = 6
turnOnHazard()addGass(amount)getGasLevel():GasLevel
MaxSpeedGasLevelMaxSpeed=155NumCars = 3
addGass(amount) getGasLevel():GasLevel
turnOnHazard()
addGass(amount) getGasLevel():GasLevel
turnOnHazard()
![Page 34: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/34.jpg)
34
Class methods
• Class methods are used to get or manipulate information about all objects created from the class.
• Typically, class methods are changing class variables. For example:• Each time we move the car in or out of the
garage, we need to add/subtract one to the number of cars: carIn( ) & carOut( )
• Also, we may want to know how many cars are actually in the garage: getNumCars( )
![Page 35: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/35.jpg)
35
Messages and Methods
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155GasLevel = 4
turnOnHazard()addGass(amount)getGasLevel():GasLevel
MaxSpeed = 165GasLevel = 10
turnOnHazard()addGass(amount)getGasLevel():GasLevel
MaxSpeed = 145GasLevel = 6
turnOnHazard()addGass(amount)getGasLevel():GasLevel
MaxSpeedGasLevelMaxSpeed=155NumCars = 3
addGass(amount) getGasLevel():GasLevel
turnOnHazard()
carIn()
carOut()
getNumCars():NumCars
![Page 36: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/36.jpg)
36
Object Oriented Programming
• When writing object-oriented programs, first one must define the classes (like Car).
• Then, while the program is running, the instances of the classes (objects) (such as Audi, BMW, Corvette in our example) are created.
![Page 37: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/37.jpg)
37
Object Oriented Programming - Benefits
• An object can be written and maintained separately from the rest of the program, modularity.
• An object has a “public face” that it uses to communicate with other objects, but other objects can not directly access its instance variables, information hiding.
![Page 38: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/38.jpg)
38
Inheritance
• All classes in Java are organized into a class hierarchy.
• The highest level classes are very general and the lower level classes are more specific.
• The lower level classes are based upon the higher level classes and inherit instance variables and methods from those higher level class. They also may contain their own (new) instance variables and methods beyond the higher level class definition.
![Page 39: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/39.jpg)
39
Inheritance
• A higher level class is called a superclass; a lower level class is called a subclass.
• A subclass may also be a superclass
• Inheritance allows you to define certain behaviors once and then to reuse those behaviors over and over again in the subclasses. This is called reusability.
![Page 40: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/40.jpg)
40
Inheritance
• Our Car class is very general.
• Let's define a new class called BMW that contains the parameters: model, color, engine size.
![Page 41: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/41.jpg)
41
Inheritance
CarMaxSpeedGasLevelMaxSpeed=155
turnOnHazard()addGass(amount)getGasLevel():GasLevel
BMWModelColorEngineSize
MaxSpeedGasLevel
turnOnHazard()addGass(amount)getGasLevel():GasLevel
![Page 42: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/42.jpg)
42
Inheritance
• Now let's define two new classes. One for the Z3 and another for the 3 Series Sedan.
• What might be some of the differences between the two classes?
• Number of doors (3, 5)• Roof (soft or hardtop)• Therefore, we add variables NumDoors and
Roof
![Page 43: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/43.jpg)
43
Inheritance
CarMaxSpeedGasLevelMaxSpeed=155
turnOnHazard()addGass(amount)getGasLevel():GasLevel
BMWModelColorEngineSize
MaxSpeedGasLevel
turnOnHazard()addGass(amount)getGasLevel():GasLevel
Z3ModelColorEngineSizeRoof
MaxSpeedGasLevel
turnOnHazard()addGass(amount)getGasLevel():GasLevel
3 seriesModelColorEngineSizeNumDoors
MaxSpeedGasLevel
turnOnHazard()addGass(amount)getGasLevel():GasLevel
![Page 44: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/44.jpg)
44
Views of the class
• A class can be viewed as a sort of contract that specifies what instances of the class can, and cannot do
• It is possible to distinguish between the outside and inside view of a class
• The interface of a class provides its outside view and emphasizes the abstraction
• The implementation of a class is its inside view
![Page 45: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/45.jpg)
45
Access
• Most classes provide three levels of access to their members (state and behavior):• Public
• The part of the class that is visible to all clients of the class
• Protected• The part of the class that is only visible to
subclasses of the class
• Private• A part of the class that is not visible to any other
classes
![Page 46: 1 Software Engineering Lecture 15 Object Oriented Software Design in Java.](https://reader036.fdocuments.us/reader036/viewer/2022062806/5697c02c1a28abf838cd90be/html5/thumbnails/46.jpg)
46
Project Work
• Next project deliverable – Design Specification Document
• Last deliverable - Prototype