Fri, May 31, 2019 4:50:54 PM

Introduction to Objects 29�<br/>
The solution to most problems in object-oriented design seems flippant: You create another <br/>
type of object. The new type of object that solves this particular problem holds references to <br/>
other objects. Of course, you can do the same thing with an array, which is available in most <br/>
languages. But this new object, generally called a container (also called a collection, but the <br/>
Java library uses that term in a different sense so this book will use �container�), will expand <br/>
itself whenever necessary to accommodate everything you place inside it. So you don�t need <br/>
to know how many objects you�re going to hold in a container. Just create a container object <br/>
and let it take care of the details. <br/>
Fortunately, a good OOP language comes with a set of containers as part of the package. In <br/>
C++, it�s part of the Standard C++ Library and is often called the Standard Template Library <br/>
(STL). Smalltalk has a very complete set of containers. Java also has numerous containers in <br/>
its standard library. In some libraries, one or two generic containers is considered good <br/>
enough for all needs, and in others (Java, for example) the library has different types of <br/>
containers for different needs: several different kinds of List classes (to hold sequences), <br/>
Maps (also known as associative arrays, to associate objects with other objects), Sets (to <br/>
hold one of each type of object), and more components such as queues, trees, stacks, etc. <br/>
From a design standpoint, all you really want is a container that can be manipulated to solve <br/>
your problem. If a single type of container satisfied all of your needs, there�d be no reason to <br/>
have different kinds. There are two reasons that you need a choice of containers. First, <br/>
containers provide different types of interfaces and external behavior. A stack has a different <br/>
interface and behavior than a queue, which is different from a set or a list. One of these might <br/>
provide a more flexible solution to your problem than the other. Second, different containers <br/>
have different efficiencies for certain operations. For example, there are two basic types of <br/>
List: ArrayList and LinkedList. Both are simple sequences that can have identical <br/>
interfaces and external behaviors. But certain operations can have significantly different <br/>
costs. Randomly accessing elements in an ArrayList is a constant-time operation; it takes <br/>
the same amount of time regardless of the element you select. However, in a LinkedList it is <br/>
expensive to move through the list to randomly select an element, and it takes longer to find <br/>
an element that is farther down the list. On the other hand, if you want to insert an element <br/>
in the middle of a sequence, it�s cheaper in a LinkedList than in an ArrayList. These and <br/>
other operations have different efficiencies depending on the underlying structure of the <br/>
sequence. You might start building your program with a LinkedList and, when tuning for <br/>
performance, change to an ArrayList. Because of the abstraction via the interface List, you <br/>
can change from one to the other with minimal impact on your code. <br/>
Parameterized types (generics) <br/>
Before Java SE5, containers held the one universal type in Java: Object. The singly rooted <br/>
hierarchy means that everything is an Object, so a container that holds Objects can hold <br/>
anything.6 This made containers easy to reuse. <br/>
To use such a container, you simply add object references to it and later ask for them back. <br/>
But, since the container held only Objects, when you added an object reference into the <br/>
container it was upcast to Object, thus losing its character. When fetching it back, you got an <br/>
Object reference, and not a reference to the type that you put in. So how do you turn it back <br/>
into something that has the specific type of the object that you put into the container? <br/>
Here, the cast is used again, but this time you�re not casting up the inheritance hierarchy to a <br/>
more general type. Instead, you cast down the hierarchy to a more specific type. This manner <br/>
of casting is called downcasting. With upcasting, you know, for example, that a Circle is a <br/>
type of Shape so it�s safe to upcast, but you don�t know that an Object is necessarily a <br/>
������������������������������������������������������������<br/>
6 They do not hold primitives, but Java SE5 autoboxing makes this restriction almost a non-issue. This is discussed in <br/>
detail later in the book. <br/>
Circle or a Shape so it�s hardly safe to downcast unless you know exactly what you�re <br/>
dealing with. <br/>
It�s not completely dangerous, however, because if you downcast to the wrong thing you�ll get <br/>
a runtime error called an exception, which will be described shortly. When you fetch object <br/>
references from a container, though, you must have some way to remember exactly what they <br/>
are so you can perform a proper downcast. <br/>
Downcasting and the runtime checks require extra time for the running program and extra <br/>
effort from the programmer. Wouldn�t it make sense to somehow create the container so that <br/>
it knows the types that it holds, eliminating the need for the downcast and a possible <br/>
mistake? The solution is called a parameterized type mechanism. A parameterized type is a <br/>
class that the compiler can automatically customize to work with particular types. For <br/>
example, with a parameterized container, the compiler could customize that container so that <br/>
it would accept only Shapes and fetch only Shapes. <br/>
One of the big changes in Java SE5 is the addition of parameterized types, called generics in <br/>
Java. You�ll recognize the use of generics by the angle brackets with types inside; for example, <br/>
an ArrayList that holds Shape can be created like this: <br/>
ArrayList<Shape> shapes = new ArrayList<Shape>(); <br/>
There have also been changes to many of the standard library components in order to take <br/>
advantage of generics. As you will see, generics have an impact on much of the code in this <br/>
book. <br/>
Object creation & lifetime <br/>
One critical issue when working with objects is the way they are created and destroyed. Each <br/>
object requires resources, most notably memory, in order to exist. When an object is no <br/>
longer needed it must be cleaned up so that these resources are released for reuse. In simple <br/>
programming situations the question of how an object is cleaned up doesn�t seem too <br/>
challenging: You create the object, use it for as long as it�s needed, and then it should be <br/>
destroyed. However, it�s not hard to encounter situations that are more complex. <br/>
Suppose, for example, you are designing a system to manage air traffic for an airport. (The <br/>
same model might also work for managing crates in a warehouse, or a video rental system, or <br/>
a kennel for boarding pets.) At first it seems simple: Make a container to hold airplanes, then <br/>
create a new airplane and place it in the container for each airplane that enters the air-traffic-<br/>
control zone. For cleanup, simply clean up the appropriate airplane object when a plane <br/>
leaves the zone. <br/>
But perhaps you have some other system to record data about the planes; perhaps data that <br/>
doesn�t require such immediate attention as the main controller function. Maybe it�s a record <br/>
of the flight plans of all the small planes that leave the airport. So you have a second <br/>
container of small planes, and whenever you create a plane object you also put it in this <br/>
second container if it�s a small plane. Then some background process performs operations on <br/>
the objects in this container during idle moments. <br/>
Now the problem is more difficult: How can you possibly know when to destroy the objects? <br/>
When you�re done with the object, some other part of the system might not be. This same <br/>
problem can arise in a number of other situations, and in programming systems (such as <br/>
C++) in which you must explicitly delete an object when you�re done with it this can become <br/>
quite complex. <br/>
30 Venkata Pilaka Venkata Pilaka <br/>

Comments

Popular posts from this blog

termux vnc viewer setup

../Settings.jpg

me.html