[/ Answer one]
One day, some people may run this code and notice that Stack is not running as fast as imagination, and can be used under heavy load. You can rewrite Stack, so that it does not need arraylist and continues to increase the efficiency of Stack. This is a new tendency and meaningful version:
[/ Code]
Class Stack
{
Private int stack_point = -1;
Private Object [] stack = new object [1000];
Public void push (Object Article)
{
Assert Stack_Pointer Stack [ stack_pointer] = article; } Public Object Pop () { askERT Stack_Pointer> = 0; Return stack [stack_pointer--]; } Public void push_many (Object [] articles) { ASSERT (stack_point articles.length) System.ArrayCopy (Articles, 0, Stack, Stack_Pointer 1, Articles.Length); Stack_pointer = articles.length; } } [/ Code] Note that push_many no longer calls push () - it makes block transfer. The new Stack runs normally; in fact, it is better than the previous version. Unfortunately, the derived class monTORABLE_STACK is no longer run because if push_many () is called, its incorrect tracking stack is used (Push () derived class version no longer calls by inherited push_many () method, so push_many () No longer update high_water_mark. Stack is a fragile class. Like closing it, it is in fact impossible to eliminate these types of errors by careful. Note If you inherit with an interface, you don't have this problem because you don't inherit a function of harmful to you. If the stack is an interface, it is implemented by Simple_stack and MonitOrable_stack, then the code is more robust. I provide an interface-based approach in Listing 0.1. This solution and the same flexibility in inheritance: You can write code with Stack abstract terms without worrying about the specific stack you actually operate. Because two implementations must provide all things in the public interface, it is difficult to make things worse. I still have the same code as the code to write the base class, because I am using the package instead of inheritance. At the bottom, I have to access the default implementation through the trivial accessor method in the package class. (For example, Monitorable_stack.push (...) (in 41 line) has to be called in Simple_stack equivalents). Programmer complained to write all of these lines, but writing this special line of code with the elimination of important potential bugs is very small cost . [/ Code] Listing 0.1. Eliminate vulnerable groups with interfaces 1 | Import java.util. *; 2 | 3 | Interface Stack 4 | { 5 | Void Push (Object O); 6 | Object Pop (); 7 | void push_many (object [] source); 8 |} 9 | 10 | Class Simple_Stack Implements Stack 11 | {private int stock_pointer = -1; 12 | private object [] stack = new object [1000]; 13 | 14 | Public Void Push (Object O) 15 | {Assert Stack_Pointer 16 | 17 | Stack [ stack_pointer] = O; 18 |} 19 | 20 | Public Object Pop () 21 | {Assert Stack_pointer> = 0; 22 | 23 | Return Stack [stack_pointr -]; 24 |} 25 | 26 | Public Void Push_MANY (Object [] SOURCE) 27 | {Assert (stack_pointer source.length) 28 | 29 | System.Arraycopy (Source, 0, Stack, Stack_Pointer 1, Source.length); 30 | stack_point = source.Length; 31 |} 32 |} 33 | 34 | 35 | Class MonitorAble_stack imports stack 36 | { 37 | private int high_water_mark = 0; 38 | Private int current_size; 39 | Simple_stack stack = new simple_stack (); 40 | 41 | Public Void Push (Object O) 42 | {IF ( current_size> high_water_mark) 43 | High_water_mark = current_size; 44 | stack.push (o); 45 |} 46 | 47 | Public Object Pop () 48 | {--current_size; 49 | Return stack.pop (); 50 |} 51 | 52 | Public Void Push_MANY (Object [] SOURCE) 53 | { 54 | IF (current_size source.length> high_water_mark) 55 | High_water_mark = current_size source.length; 56 | 57 | stack.push_many (source); 58 |} 59 | 60 | Public int maximum_size () 61 | {RETURN High_water_mark; 62 |} 63 |} 64 | [/ Code] No frame programming is mentioned, which makes discussions for fragile base classes are incomplete. A base class such as Microsoft Foundation Classes (MFC) has become a popular way to establish a class library. Although the MFC itself is holy retreat, the MFC's baseway is already rooted, and this does not have Microsoft to terminate, and the programmer will always think that Microsoft's approach is the best way. A frame-based system typically uses the composition of the class of semi-finished products, which does not do anything you need to do, but depends on derived classes to provide the needs. In Java, a good example is the component's Paint () method, which is a valid placement; a derived class must provide a real version. You can make a modest multi-country thing, but a complete class framework based on customized derived class is very fragile. The base class is too fragile. When we use the MFC programming, each time Microsoft announces a new version, I have to rewrite my app. These codes will be compiled frequently, but they cannot run due to changes in some base classes. All Java package is working very well. To make them run, you don't need to expand anything. This already provided structure is better than the frame structure of the derived class. It is easy to maintain and use, and if the class provided by Sun Microsystems has changed its implementation, it will not make your code in danger. Summarize vulnerability Generally, it is best to avoid specific basic and Extends relationships, and interface and imports are used. My processing rule is to complete the interface in my at least 80% code. For example, I never need reference to HashMap; I use a reference to the MAP interface. (I am not strict about Interface. When you look at how to use the interface, InputStream is a good interface, although it is implemented as an abstract class in Java.) The more abstract you increase, the more flexible. In today's business environment, demand changes with program development, flexible is the most important. And most of the code in the sensitive programming will use abstraction to write. If you check the four-man mode, you will see a lot of these modes to provide methods to eliminate implementation, and it is best to inherit the interface, and the common feature of most modes is inherited with an interface. This important fact is what we started: mode is discovered rather than inventions. The appearance of the pattern is when you find it well, easy to maintain the code. It is said that these well-written, easy-to-maintain code fundamentally avoids the inheritance. This article is from the book that I am about to publish, temporarily named "Holub on patterns: learning at code", and Apress will be published this fall. About author Allen Holub works in the computer industry since 1979. Currently, as an advisor, he provides advice in the company's implementation, training and design and encoding services, saving the company costs in software. It has published 8 books, including "Taming Java Thread" (APRESS, 2000) and "Pearson Higher Education, 1990), and at the University of California Berkeley. In his website, you will find more information. ( http://www.holub.com)