Algorithmics 3 Wrapping up Object-Oriented Design October 4th, 2006

advertisement
Algorithmics 3
Algorithmics 3
Wrapping up Object-Oriented Design
October 4th, 2006
Algorithmics 3
What is this class all about
What is this class about ?
In first year, you wrote algorithms.
This year, you’re expected to write applications.
Algorithmics 3
What is this class all about
What is this class about ?
In first year, you wrote algorithms.
This year, you’re expected to write applications.
Q What’s the difference ?
Algorithmics 3
What is this class all about
What is this class about ?
In first year, you wrote algorithms.
This year, you’re expected to write applications.
Q What’s the difference ?
A Algorithms are for learning and testing. Applications are supposed to
interact with the rest of the world. That is,
I with other programmers ⇒ your code must be readable
I
I
with users out of this classroom ⇒ you must present a useable
interface
with the code already written by other programmers ⇒ your code
must be able to use standard methods – and you must be able to
read documentations
Algorithmics 3
What is this class all about
Webography
On-line Java Tutorials To learn quickly some aspect of Java http:
//java.sun.com/docs/books/tutorial/index.html.
Java’s Libraries To check in details how some aspect of Java works
http://java.sun.com/j2se/1.5.0/docs/index.html.
Java Language Specifications To look smart and know what most people
don’t know about Java
http://java.sun.com/docs/books/jls/.
Passeport Informatique A good website for whoever wishes to work in
technologies of information and communication
http://www.passinformatique.com/
Algorithmics 3
Object-oriented design
What is this class all about
Object-oriented design
Java implementation
Classes / interfaces
Properties
Actions
Constructors
Algorithmics 3
Object-oriented design
What is object-oriented design ?
1. Problem description.
2. Object model.
3. Object relations.
Algorithmics 3
Object-oriented design
1. Problem description
An problem description is a set of sentences in English or in French,
describing a problem.
Often, this description involves either real-world objects or how the user
perceives what is happening, rather than what should take place for this
to happen.
Algorithmics 3
Object-oriented design
1. Problem description
An problem description is a set of sentences in English or in French,
describing a problem.
Often, this description involves either real-world objects or how the user
perceives what is happening, rather than what should take place for this
to happen.
This description is usually the same no matter what programming
language you are using.
Algorithmics 3
Object-oriented design
2. Object model
An object model recapitulates the problem as a set of objects. In the
model, each object consists in
I
I
a name
messages it can receive
Algorithmics 3
Object-oriented design
2. Object model
An object model recapitulates the problem as a set of objects. In the
model, each object consists in
I
I
I
a name
messages it can receive – messages are sent by some object to this
object, they have no return result but they usually have effects
actions it can undertake
Algorithmics 3
Object-oriented design
2. Object model
An object model recapitulates the problem as a set of objects. In the
model, each object consists in
I
I
I
I
a name
messages it can receive – messages are sent by some object to this
object, they have no return result but they usually have effects
actions it can undertake – actions are undertaken by this object,
usually as a reaction to some message or to some other action. The
have no result but they usually have effects (changeing the value of
fields, sending messages to other objects, etc.)
properties defining its state
Algorithmics 3
Object-oriented design
2. Object model
An object model recapitulates the problem as a set of objects. In the
model, each object consists in
I
I
I
I
a name
messages it can receive – messages are sent by some object to this
object, they have no return result but they usually have effects
actions it can undertake – actions are undertaken by this object,
usually as a reaction to some message or to some other action. The
have no result but they usually have effects (changeing the value of
fields, sending messages to other objects, etc.)
properties defining its state – some properties are always known,
some must be computed when they are requested, some can be
changed, others can’t, some must be accessible by everyone, others
only by this object
Algorithmics 3
Object-oriented design
2. Object model
An object model recapitulates the problem as a set of objects. In the
model, each object consists in
I
I
I
I
a name
messages it can receive – messages are sent by some object to this
object, they have no return result but they usually have effects
actions it can undertake – actions are undertaken by this object,
usually as a reaction to some message or to some other action. The
have no result but they usually have effects (changeing the value of
fields, sending messages to other objects, etc.)
properties defining its state – some properties are always known,
some must be computed when they are requested, some can be
changed, others can’t, some must be accessible by everyone, others
only by this object
At this level, there is still no Java code. Only informal descriptions of
what each message/actions/properties should do. This object model is
valid for most programming languages, including non-Object-Oriented
languages.
Algorithmics 3
Object-oriented design
3. Object relations
Object relations recapitulate the relations between objects as a class
diagram. In the class diagram, objects can be related by
I
A is a type of B – if every object of type A is also of type B
I
A is part of the definition of B – if every object of type B contains
at least one object of type A as a property.
A is an instance of B – if A is exactly one object and its type is B
I
Algorithmics 3
Object-oriented design
3. Object relations
Object relations recapitulate the relations between objects as a class
diagram. In the class diagram, objects can be related by
I
A is a type of B – if every object of type A is also of type B
I
A is part of the definition of B – if every object of type B contains
at least one object of type A as a property.
A is an instance of B – if A is exactly one object and its type is B
I
At this level, there is still no Java code. This set of relations is valid for
most object-oriented programming languages.
Algorithmics 3
Object-oriented design
Now what ?
The design is mostly complete. At this point, you can start thinking
about
I
I
I
what programming language you are going to use for your work
dividing the work between programmers
what tools you are going to use to provide the features you need
(user interface, multimedia, etc.)
Algorithmics 3
Java implementation
What is this class all about
Object-oriented design
Java implementation
Classes / interfaces
Properties
Actions
Constructors
Algorithmics 3
Java implementation
Implementation
1. Classes and interfaces.
2. Subclasses and implementations.
3. Properties.
4. Messages.
5. Actions.
6. Constructors.
7. Documentation.
Algorithmics 3
Java implementation
Classes / interfaces
Class or interface ?
Each object will be implemented either as a class or as an interface.
Algorithmics 3
Java implementation
Classes / interfaces
Class or interface ?
Each object will be implemented either as a class or as an interface.
Deciding when to use an interface and when to use a class is not always
simple.
I
Interfaces are only contracts. Classes also contain the features. So,
if you’re providing the features, you will need to write the class.
I
On the other hand, if you’re asking another programmer to write a
class for you, it’s generally a good idea to start by defining an
interface. This will let you both continue working on the project
without having to wait for each other.
It’s also useful to remove circular dependencies or to prioritize your
development process.
I
Algorithmics 3
Java implementation
Classes / interfaces
Subclasses or implementations
If the object A you define is a type of another object B, then
I
if both A and B are classes, then A extends B
I
if both A and B are interfaces, then A extends B
if B is an interface and A is a class, then A implements B
I
I
if A is an interface and B is a class, then something is wrong with
your design.
Algorithmics 3
Java implementation
Properties
Types of properties
The first question you will need to answer when implementing a property
A of object B is that of its type. Sometimes, you can be helped by
relations telling you that A “is part of the definition of” B and A “is an
instance of” C .
In this case, the type of A is C .
Otherwise, you’ll have to use your intelligence to work out the type of A.
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ?
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ? ⇒ then make it a public field of the
correct type.
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ? ⇒ then make it a public field of the
correct type.
I
is it known once and for all when the object is created ?
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ? ⇒ then make it a public field of the
correct type.
I
is it known once and for all when the object is created ? ⇒ then
make it a private final field of the correct type.
I
is it something that changes but that you always know ?
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ? ⇒ then make it a public field of the
correct type.
I
is it known once and for all when the object is created ? ⇒ then
make it a private final field of the correct type.
I
is it something that changes but that you always know ? ⇒ then
make it a private field of the correct type.
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ? ⇒ then make it a public field of the
correct type.
I
is it known once and for all when the object is created ? ⇒ then
make it a private final field of the correct type.
I
is it something that changes but that you always know ? ⇒ then
make it a private field of the correct type.
I
is it something that changes but that you always need to recompute
?
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ? ⇒ then make it a public field of the
correct type.
I
is it known once and for all when the object is created ? ⇒ then
make it a private final field of the correct type.
I
is it something that changes but that you always know ? ⇒ then
make it a private field of the correct type.
I
is it something that changes but that you always need to recompute
? ⇒ then make it a private method of the correct type.
Algorithmics 3
Java implementation
Properties
Knowing the value of properties
The second thing you need to decide when implementing a property is
how the object is going to know the value of that property:
I
is this object an interface ? ⇒ then make it a public field of the
correct type.
I
is it known once and for all when the object is created ? ⇒ then
make it a private final field of the correct type.
I
is it something that changes but that you always know ? ⇒ then
make it a private field of the correct type.
I
is it something that changes but that you always need to recompute
? ⇒ then make it a private method of the correct type.
Note that interfaces cannot have fields.
Algorithmics 3
Java implementation
Properties
Exposing properties
If you know that will need to use the value of the property from outside
the object, you need to add an public method for reading that
property, with the correct type.
If the property is already implemented as a method, just make that
method public.
The method may accept arguments.
Algorithmics 3
Java implementation
Properties
Naming conventions
For a property named XXXX ,
I
I
if you have a field containing the value of the property, call it XXXX
if you have a method for reading/computing the value of the
property, call it
I
I
I
hasXXXX if the question is “does the object contain [something] ?”
isXXXX otherwise and if the answer is a boolean
getXXXX otherwise.
Algorithmics 3
Java implementation
Properties
Naming conventions
For a property named XXXX ,
I
I
if you have a field containing the value of the property, call it XXXX
if you have a method for reading/computing the value of the
property, call it
I
I
I
hasXXXX if the question is “does the object contain [something] ?”
isXXXX otherwise and if the answer is a boolean
getXXXX otherwise.
Don’t forget to document your properties !
Algorithmics 3
Java implementation
Actions
Implementing actions
Actions are implemented as protected void methods. They often take
arguments.
By convention, the name of a method implementing an action is always a
verb.
Algorithmics 3
Java implementation
Actions
Implementing actions
Actions are implemented as protected void methods. They often take
arguments.
By convention, the name of a method implementing an action is always a
verb.
Don’t forget to document your actions !
Algorithmics 3
Java implementation
Actions
Implementing messages
Messages are implemented as public void methods. They often take
arguments.
By convention, the name of a method implementing a message is always
a verb.
Algorithmics 3
Java implementation
Actions
Naming conventions
I
I
I
If your message sets the value of property XXXX , then call the
method setXXXX.
If your message toggles the value of property XXXX , then call the
method toggleXXXX.
Otherwise, it’s often a good idea to call your method doXXXX.
Algorithmics 3
Java implementation
Constructors
Almost done
Q What are we missing ?
Algorithmics 3
Java implementation
Constructors
Almost done
Q What are we missing ?
A Constructors.
Algorithmics 3
Java implementation
Constructors
Almost done
Q What are we missing ?
A Constructors.
And probably some more documentation.
Algorithmics 3
Java implementation
Constructors
Constructors
Each class must have a constructor.
The constructor must make sure that all fields are initialized – in
particular, final fields.
The constructor must also make sure that it calls a constructor of the
parent class.
Algorithmics 3
Java implementation
Constructors
Naming conventions
You have no choice of name for constructors: they must have the exact
same name as the class.
Algorithmics 3
Java implementation
Summary
Summary
The process detailed today should help you come up with designs and
implementations in Java and other programming languages.
It won’t replace
I
intelligence
work
I
experience.
I
You’ll have to get these by yourselves.
Algorithmics 3
Java implementation
Summary
Any questions ?
Now is the right time to ask.
Algorithmics 3
Homework
Homework for you
For everyone
I
I
I
I
complete last week’s exercise 9 – including subclasses CaseMinee
and CaseNonMinee
complete last week’s exercise 10
last week’s exercise 8 – the calculator must work
last week’s exercise 11
Download