software.nju.edu.cn

Download Report

Transcript software.nju.edu.cn

Introduction to Objects
2015/7/21
1
Abstraction:The object-oriented
approach
• Everything is an object.
• A program is a bunch of objects telling
each other what to do by sending
messages.
• Each object has its own memory made
up of other objects.
• Every object has a type.
• All objects of a particular type can
receive the same messages.
• An object has state, behavior and
identity.
2015/7/21
2
An object has an interface
• Objects that are identical except for their state
during a program’s execution are grouped
together into “classes of objects,” and that’s
where the keyword class came from.
• Once a class is established, you can make as
many objects of that class as you like. There
needs to be a way to make a request of the
object so that it will do something.
• The requests you can make of an object are
defined by its interface, and the type is what
determines the interface.
• There must be code somewhere to satisfy that
request. This, along with the hidden data,
comprises the implementation.
2015/7/21
3
Example: The Light
The name of the type/class is Light, the name
of this particular Light object is lt, and
the requests that you can make of a Light
object are to turn it on, turn it off, make it
brighter, or make it dimmer.
2015/7/21
4
An object provides services
• While you’re trying to develop or understand a program
design, one of the best ways to think about objects is as
“service providers.”
• Your program itself will provide services to the user,
and it will accomplish this by using the services offered
by other objects. Your goal is to produce (or even better,
locate in existing code libraries) a set of objects that
provide the ideal services to solve your problem.
• Thinking of an object as a service provider has an
additional benefit: It helps to improve the cohesiveness
of the object. High cohesion is a fundamental quality of
software design: It means that the various aspects of a
software component “fit together” well.
2015/7/21
5
The hidden implementation
• Class creators: those who create new
data types.
• Client programmers: the class consumers
who use the data types in their
applications.
• The goal of the class creator is to build a
class that exposes only what’s necessary
to the client programmer and keeps
everything else hidden.
• Access Control: To have boundaries that
are respected by all parties involved.
2015/7/21
6
Access Control in Java
• Java uses three explicit keywords to set the boundaries
in a class: public, private, and protected.
• public means the following element is available to
everyone.
• private means that no one can access that element
except the creator of the type.
• protected acts like private, with the exception that an
inheriting class has access to protected members, but
not private members.
• Java also has a “default” access, which comes into play
if you don’t use one of the aforementioned specifiers.
This is usually called package access because classes can
access the members of other classes in the same
package (library component), but outside of the
package those same members appear to be private.
2015/7/21
7
Reusing the implementation
• Composition: place an object of a
class inside a new class
• Inheritance: create a new class based
on the original one
2015/7/21
8
Inheritance
• Inheritance: take the existing class,
clone it, and then make additions and
modifications to the clone.
2015/7/21
9
Example: Shape
2015/7/21
10
Is-a vs. is-like-a relationships
• The derived class is exactly the same type as
the base class since it has exactly the same
interface. This can be thought of as pure
substitution, often referred to as the
substitution principle.
• There are times when you must add new
interface elements to a derived type, thus
extending the interface. The new type can
still be substituted for the base type, but the
substitution isn’t perfect because your new
methods are not accessible from the base
type. This can be described as an is-like-a
relationship .
2015/7/21
11
Example: Is-a VS Is-like-a
2015/7/21
12
Interchangeable objects with
polymorphism
• When dealing with type hierarchies, you
often want to treat an object not as the
specific type that it is, but instead as its
base type. This allows you to write code
that doesn’t depend on specific types.
Such code is unaffected by the addition
of new types, and adding new types is
the most common way to extend an
object-oriented program to handle new
situations.
2015/7/21
13
Example: Interchangeable
objects
• The BirdController object just works with generic
Bird objects and does not know what exact type
they are. This is convenient from BirdController’s
perspective because it doesn’t have to write
special code to determine the exact type of Bird
it’s working with or that Bird’s behavior.
2015/7/21
14
Early binding VS Late binding
• early binding: The compiler generates a
call to a specific function name, and the
runtime system resolves this call to the
absolute address of the code to be
executed.
• late binding: The compiler does ensure
that the method exists and performs
type checking on the arguments and
return value, but it doesn’t know the
exact code to execute.
2015/7/21
15
Example: Late binding
• The calls to doSomething( )
automatically work correctly,
regardless of the exact type of the
object.
2015/7/21
16
Upcasting
• We call this process of treating a
derived type as though it were its
base type upcasting, that is, moving
up the inheritance diagram.
2015/7/21
17
The singly rooted hierarchy
• In Java, all classes should ultimately be
inherited from a single base class.
• The name of this ultimate base class is
simply Object.
• All objects in a singly rooted hierarchy
have an interface in common, so they
are all ultimately the same fundamental
type. All objects in a singly rooted
hierarchy can be guaranteed to have
certain functionality.
2015/7/21
18
Containers
• Problem: You don’t know how many
objects you’re going to need to solve a
particular problem, or how long they will
last. You also don’t know how to store
those objects.
• The solution to most problems in objectoriented design seems flippant: You
create another type of object. The new
type of object that solves this particular
problem holds references to other
objects. The special object is called a
container.
2015/7/21
19
Some containers provided by
Java
• List
• Map
• Set
2015/7/21
20
Parameterized types (generics)
• Downcasting: cast down the hierarchy
to a more specific type.
• Not safe and often error prone.
• The solution is called a parameterized
type mechanism. A parameterized
type is a class that the compiler can
automatically customize to work with
particular types.
2015/7/21
21
Object creation & lifetime
• Every time you want to create an
object, you use the new operator to
build a dynamic instance of that
object.
• Java provides a feature called a
garbage collector that automatically
discovers when an object is no longer
in use and destroys it.
2015/7/21
22
Exception handling: dealing with
errors
• Error handling has been a particularly
difficult issue.
• Exception handling wires error handling
directly into the programming language
and sometimes even the operating
system. An exception is an object that is
“thrown” from the site of the error and
can be “caught” by an appropriate
exception handler designed to handle
that particular type of error.
2015/7/21
23
Concurrent programming
• A fundamental concept in computer
programming is the idea of handling m
• There’s a large class of problems in which
you’re simply trying to partition the problem
into separately running pieces (tasks) so that
the whole program can be more responsive.
Within a program, these separately running
pieces are called threads, and the general
concept is called concurrency. ore than one
task at a time.
• Java’s concurrency is built into the language.
2015/7/21
24
Java and the Internet
• What is the Web?
• The Web can seem a bit of a mystery
at first, with all this talk of “surfing,”
“presence,” and “home pages.”
• Client/server computing
• The Web as a giant server
2015/7/21
25
Client/server computing
• The primary idea of a client/server system is that you
have a central repository of information— some kind of
data, usually in a database—that you want to distribute
on demand to some set of people or machines.
• A key to the client/server concept is that the repository
of information is centrally located so that it can be
changed and so that those changes will propagate out
to the information consumers.
• The information repository, the software that
distributes the information, and the machine(s) where
the information and software reside are called the
server.
• The software that resides on the consumer machine,
communicates with the server, fetches the information,
processes it, and then displays it on the consumer
machine is called the client.
2015/7/21
26
The Web as a giant server
• The Web is actually one giant
client/server system.
• All the servers and clients coexisting
on a single network at once.
• The Web browser: a big step forward
2015/7/21
27
Client-side programming
• Basic HyperText Markup Language
(HTML) contains simple mechanisms for
data gathering: text-entry boxes, check
boxes, radio boxes, lists and dropdown
lists, as well as a button that could only
be programmed to reset the data on the
form or “submit” the data on the form
back to the server.
• The text within the submission tells CGI
what to do with it.
• Step forward: Plug-ins, Scripting
languages and Java
2015/7/21
28
Plug-ins
• This is a way for a programmer to add
new functionality to the browser by
downloading a piece of code that plugs
itself into the appropriate spot in the
browser.
• The value of the plug-in for client-side
programming is that it allows an expert
programmer to develop extensions and
add those extensions to a browser
without the permission of the browser
manufacturer.
2015/7/21
29
Scripting languages
• With a scripting language, you embed
the source code for your client-side
program directly into the HTML page,
and the plug-in that interprets that
language is automatically activated
while the HTML page is being
displayed.
• One scripting language that you can
expect a Web browser to support
without a plug-in is JavaScript.
2015/7/21
30
Java in client side programming
• Java allows client-side programming
via the applet and with Java Web Start.
• An applet is a mini-program that will
run only under a Web browser.
2015/7/21
31
Server-side programming
• A more complicated request to a server
generally involves a database transaction.
A common scenario involves a request
for a complex database search, which
the server then formats into an HTML
page and sends to you as the result.
• These database requests must be
processed via some code on the server
side, which is generally referred to as
server-side programming.
2015/7/21
32
Summary
• A well-written Java program is generally far
simpler and much easier to understand than a
procedural program. What you’ll see are the
definitions of the objects that represent concepts
in your problem space (rather than the issues of
the computer representation) and messages sent
to those objects to represent the activities in that
space.
• One of the delights of object-oriented
programming is that, with a well-designed
program, it’s easy to understand the code by
reading it. Usually, there’s a lot less code as well,
because many of your problems will be solved by
reusing existing library code.
2015/7/21
33