Why extends is (ii) August 31, 2003 Author harmful: neo
[/ Connecting an article] 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 = -1; private object [] stack = new object [1000];
Public void push {assert stack_pointer public Object pop () {assert stack_pointer> = 0; return stack [stack_pointer--];} public void push_many (Object [] articles) {assert (stack_pointer articles.length) 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. Use interface to eliminate vulnerable base classes 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 Stack_pointer = -1; 12 | Private Object [] Stack = New Object [1000]; 13 | 14 | Public Void Push (Object O) 15 | {Assert Stack_point 58 |} 59 | 60 | Public int maximum_size () 61 | {return high_water_mark; 62 |} 63 |} 64 | [/ code] did not mention frame programming, the discussion for fragile base classes is 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. Summarizing the fragile base, preferably avoiding specific basic and Extends relationships, and interfaces and imports. 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 the 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)