Fortunately, a good OOP language comes with a set of containers as part of the package。 In C++, it’s part of the Standard C++ Library and is sometimes called the Standard Template Library (STL)。 Object Pascal has containers in its Visual Component Library (VCL)。 Smalltalk has a very complete set of containers。 Java also has containers in its standard library。 In some libraries, a generic container is considered good enough for all needs, and in others (Java, for example) the library has different types of containers for different needs: a vector (called an ArrayList in Java) for consistent access to all elements, and a linked list for consistent insertion at all elements, for example, so you can choose the particular type that fits your needs。 Container libraries may also include sets, queues, hash tables, trees, stacks, etc。
All containers have some way to put things in and get things out; there are usually functions to add elements to a container, and others to fetch those elements back out。 But fetching elements can be more problematic, because a single-selection function is restrictive。 What if you want to manipulate or compare a set of elements in the container instead of just one?
The solution is an iterator, which is an object whose job is to select the elements within a container and present them to the user of the iterator。 As a class, it also provides a level of abstraction。 This abstraction can be used to separate the details of the container from the code that’s accessing that container。 The container, via the iterator, is abstracted to be simply a sequence。 The iterator allows you to traverse that sequence without worrying about the underlying structure—that is, whether it’s an ArrayList, a LinkedList, a Stack, or something else。 This gives you the flexibility to easily change the underlying data structure without disturbing the code in your program。 Java began (in version 1。0 and 1。1) with a standard iterator, called Enumeration。
For all of its container classes。 Java 2 has added a much more complete container library that contains an iterator called Iterator that does more than the older Enumeration。 From a design standpoint, all you really want is a sequence that can be manipulated to solve your problem。 If a single type of sequence satisfied all of your needs, there’d be no reason to have different kinds。 There are two reasons that you need a choice of containers。 First, containers provide different types of interfaces and external behavior。 A stack has a different interface and behavior than that of a queue, which is different from that of a set or a list。 One of these might provide a more flexible solution to your problem than the other。 Second, different containers have different efficiencies for certain operations。 The best example is an ArrayList and a LinkedList。 Both are simple sequences that can have identical interfaces and external behaviors。 But certain operations can have radically different costs。 Randomly accessing elements in an ArrayList is a constant-time operation; it takes the same amount of time regardless of the element you select。 However, in a LinkedList it is expensive to move through the list to randomly select an element, and it takes longer to find an element that is further down the list。 On the other hand, if you want to insert an element in the middle of a sequence, it’s much cheaper in a LinkedList than in an ArrayList。 These and other operations have different efficiencies depending on the underlying structure of the sequence。 In the design phase, you might start with a LinkedList and, when tuning for performance, change to an ArrayList。 Because of the abstraction via iterators, you can change from one to the other with minimal impact on your code。
In the end, remember that a container is only a storage cabinet to put objects in。 If that cabinet solves all of your needs, it doesn’t really matter how it is implemented (a basic concept with most types of objects)。 If you’re working in a programming environment that has built-in overhead due to other factors, then the cost difference between an ArrayList and a LinkedList might not matter。 You might need only one type of sequence。 You can even imagine the “perfect” container abstraction, which can automatically change its underlying implementation according to the way it is used。 面向对象程序设计英文文献和中文翻译(2):http://www.youerw.com/fanyi/lunwen_87408.html