1. Trang chủ
  2. » Công Nghệ Thông Tin

Software Engineering For Students: A Programming Approach Part 26 pps

10 203 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 157,04 KB

Nội dung

228 Chapter 16 ■ Programming in the large implement every method described in the interface. Any deviation results in compiler errors. We can describe the relationship between a class and its interface using a UML class diagram. See for example Figure 16.2. In a UML class diagram, an interface is shown as a rectangle. The interface name is preceded by the word <<interface>>. The implements relationship is shown as a dotted arrow. Interfaces can also be used to describe an inheritance structure. For example, sup- pose we wanted to describe an interface for a BetterStack that is a subclass of the Stack interface described above. We can write in Java: public interface BetterStackInterface extends StackInterface { boolean empty(); } which inherits the interface StackInterface and states that the interface BetterStackInterface has an additional method, named empty, to test whether the stack is empty. We could similarly describe a whole tree structure of classes as interfaces, describing purely their outward appearance and their subclass–superclass relationships. In summary, the characteristics of an interface facility (module description language) are: ■ it is textual (though we could think of tools to convert the text into graphics) ■ it is an extension of the programming language (therefore consistent, easy to learn and checkable by the compiler) ■ it allows specification of the external appearance of classes – their names, their visible methods, the parameters required. The advantages of being able to write descriptions like this are: ■ during design, we can describe the grand structure of a piece of software in a fairly formal way. The description can be checked by a language processor. <<interface>> StackInterface Stack Figure 16.2 A class and its interface BELL_C16.QXD 1/30/05 4:24 PM Page 228 16.7 Interfaces and interoperability 229 Household appliances, such as toasters and electric kettles, come with a power cord with a plug on the end of it. The design of the plug is standard (throughout a country) and ensures that an appliance can be used anywhere (within the country). Thus the adoption of a common interface ensures interoperability. In Java, interfaces can be used in a similar fashion to ensure that objects exhibit a common interface. When such an object is passed around a program, we can be sure that it supports all the methods spec- ified by the interface description. 16.7 ● Interfaces and interoperability SELF-TEST QUESTION 16.4 Write specifications (interfaces) for the classes in the following software system. A patient monitoring system (Appendix A) monitors the vital signs of a single hospital patient and delivers messages to the nurses’ station. It consists of the fol- lowing classes, each followed by their methods. Make assumptions about the parameters associated with the methods. ■ class Clock provides methods init, waitSeconds, getHours, getMinutes, getSeconds ■ class Display provides methods init, flashLight, unflashLight, soundKlaxon, unsoundKlaxon, displayMessage and clearScreen ■ class Heart provides methods readRate and readPressure ■ class Lungs provides method readRate ■ class Temp provides method readTemp ■ during coding, the description can be used as part of the class specification. The com- piler can also check for consistency between the description and the class coding. ■ during maintenance, the description can be used as documentation in order to learn about the overall structure of the system. It can be used as input to a tool that creates various reports on the structure – e.g., a class diagram. Finally, fol- lowing update of the software, consistency checking can be reapplied using the compiler. What interfaces cannot describe are: ■ the implementations of methods (but that is the whole point of interfaces) ■ which classes use which other classes, the has-a relationships (this needs some other notation). BELL_C16.QXD 1/30/05 4:24 PM Page 229 230 Chapter 16 ■ Programming in the large Just as a TV has interfaces both to a power source and to a signal source, so can we specify that a class implements a number of interfaces. Java, for example, is a language that provides single inheritance – a class can inherit from (or be the subclass of) only one class. The class structure is a tree, with the root at the top, in which a class can have many subclasses but only one superclass. Figure 16.3 shows illustrative classes Circle and Game as subclasses of superclass JFrame. Each class appears only within a single tree and each class has only a single superclass. 16.8 ● Multiple interfaces SELF-TEST QUESTION 16.5 We wish to write a new class Circle that implements the Displayable interface. Write the header for the class. As an example, we declare an interface named Displayable. Any class complying with this interface must include a method named display which displays the object. The interface declaration in Java is: public interface Displayable { void display(Graphics paper); } Now we write a new class, named Square, which represents square graphical objects. We say in the header of the class that it implements Displayable. We include within the body of the class the method display: public class Square implements Displayable { private int x, y, size; public void display(Graphics paper) { paper.setColor(Color.black); paper.drawRectangle(x, y, size, size); } // other methods of the class Square } As the heading states, this class (and any object created from it) conforms to the Displayable interface. It means that any object of this class can be passed around a program and we are confident that, when necessary, it can be displayed by calling its method display. > > BELL_C16.QXD 1/30/05 4:24 PM Page 230 16.8 Multiple interfaces 231 Sometimes we would like a class to inherit from more than one superclass as described in the following class header and shown in Figure 16.4. public class Game extends JFrame, Thread // error But this heading is wrong because it attempts to extend two classes. This would be called multiple inheritance. Some languages, such as C++, permit multiple inheritance while Java does not. Multiple inheritance allows a class to inherit sets of methods from a number of classes, and it is therefore potentially very powerful. If we think about classification systems in science and nature, it is often the case that objects belong to more than one class. We humans, for example, belong to one gender class, but also to a class that likes a particular type of music. So we all belong in one inheritance tree for gender, another for musical taste, another for mother tongue, and so on. Interfaces provide a way of emulating a facility similar to multiple inheritance. This is because, while a class can only extend a single class, it can implement any number of interfaces. Multiple interfaces are illustrated in Figure 16.5. This example is coded in Java as follows: public class Game extends JFrame implements InterfaceA, InterfaceB If Game inherited from InterfaceA and InterfaceB, it would inherit a set of methods from InterfaceA and InterfaceB. But instead Game is implementing inter- faces InterfaceA and InterfaceB, and these interfaces have no methods on offer. JFrame Circle Game Figure 16.3 Single inheritance Game JFrame Thread Figure 16.4 Multiple inheritance (supported in C++ but not in Java) BELL_C16.QXD 1/30/05 4:24 PM Page 231 232 Chapter 16 ■ Programming in the large What this means is that class Game agrees to provide the methods described in InterfaceA and InterfaceB – that Game has agreed to conform to certain behavior. The code for implementing InterfaceA and InterfaceB has to be written as part of the class Game. A programming language is ill suited for the development of large, complex programs if it does not provide facilities for the separate compilation of program modules. Large programs must necessarily be developed by teams of programmers; individual pro- grammers must be able to work independently and at the same time be able to access programs written by other members of the team. Programming language support is required for the integration of routines that have been developed separately. Additional sup- port in this area is often provided by environmental tools, such as linkers, cross-reference generators, file librarians and source code control systems. What support should the programming language itself provide? We suggest the following: ■ independent compilation of program modules ■ easy access to libraries of precompiled software ■ the ability to integrate together components written in different languages ■ strong type checking across module boundaries ■ the ability to avoid the unnecessary recompilation of precompiled modules. One of the foremost reasons for the continued popularity of Fortran is the tremen- dous resource of reusable software available to scientists and engineers through the readily accessible libraries of scientific and engineering subroutines. Fortran provides independent compilation of modules at the subroutine level and easy access to library routines but performs no run-time checking of calls to external routines. It is the responsibility of the programmer to check that the correct number and type of param- eters are used in the calling program. Java and similar languages provide far greater support for separate compilation than Fortran. Classes may be compiled as separate modules with strong type checking across module boundaries to ensure that they are used in accordance with their specifications. The specification and implementation of a class may be compiled in two separate parts. 16.9 ● Separate compilation Game JFrame <<interface>> InterfaceB <<interface>> InterfaceA Figure 16.5 Multiple interfaces BELL_C16.QXD 1/30/05 4:24 PM Page 232 Exercises 233 This has a number of advantages for the software engineer. The strong type checking ensures that all specifications stay in line with their implementations. Also, it means that once the specification for a class has been compiled, modules which use that class may also be compiled (even before the implementation of the class has been completed). C, C++, C# and Java are all (fairly) small languages and most of their functionality is provided by large and comprehensive libraries. These libraries are separately compiled. Summary It is often convenient to group the classes of a large program into packages. Each package is given a name. Classes within a package can be used by giving the full name of the package and class. A more convenient alternative is to use a statement such as the Java import statement. The classes can be placed in the appropriate package by employing the Java package statement. Scope rules mean that classes within the same package have special access rights to each other. Interfaces are used to describe the services provided by a class. Interfaces are use- ful for describing the structure of software. This description can be checked by the compiler. Interfaces can also be used to ensure that a class conforms to a particu- lar interface. This supports interoperability. Most modern languages support mul- tiple interfaces, but only support single inheritance. For large software, separate compilation is a vital facility. 16.1 Assess the difficulties of developing large-scale software and suggest program- ming language features to help solve the problems that you have identified. 16.2 The facility to describe interfaces as seen in modern languages enables specifica- tion of the services provided by a class. Consider extending such a language so that it also describes: ■ the classes that each class uses ■ the classes that use each class. What would be the advantages and disadvantages of this notation? 16.3 Assess whether and how it would be possible to extend the interface notation to packages. 16.4 The design goals of Ada were readability, strong typing, programming in the large, exception handling data abstraction, and generic units. Explain the meanings of Exercises • BELL_C16.QXD 1/30/05 4:24 PM Page 233 234 Chapter 16 ■ Programming in the large these objectives and comment on their validity. Assess whether they are comple- mentary or contradictory. If you are familiar with Ada, assess it against these aims. 16.5 Some of the stated design aims for Java are that it should be simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-per- formance and dynamic. Explain the meanings of these objectives. Assess whether they are complemen- tary or contradictory. If you are familiar with Java, assess it against these aims. 16.6 Take a language of your choice. Search out the design aims and explain their mean- ing. Assess how well the language matches the design aims. Assess the language against the criteria developed in this chapter. 16.7 What aims and what language features would you expect to find in a language designed for each of the following application domains? ■ information systems ■ scientific programming ■ systems programming ■ real-time embedded systems. How suitable is your favorite language for each of these application domains? 16.8 Compare and contrast Ada with C++. 16.9 Compare and contrast C++ with Java. 16.10 Cobol (or Fortran) is an outdated language. Discuss. Answers to self-test questions 16.1 To use class Friday put: import week.Friday; To create an object of the class Friday put: Friday friday = new Friday(); To use all the classes in the package put: import week.*; 16.2 package database; public class Backup BELL_C16.QXD 1/30/05 4:24 PM Page 234 Further reading 235 16.3 A specification of this kind only specifies the method names and their parameters. It does not specify any more closely what a method does – other than via any comments. 16.4 interface Clock { void init(); void waitSeconds(int seconds); int getHours(); int getMinutes(); int getSeconds(); } interface Display { void init(); void flashLight(); void unflashLight(); void soundKlaxon(); void unsoundKlaxon(); void displayMessage(String message); void clearScreen(); } interface Heart { int readRate(); int readPressure(); } interface Lungs { int readRate(); } interface Temp { int readTemp(); } 16.5 public class Circle implements Displayable Further reading • A good collection of papers from the computer science literature which critically evaluate and compare these three programming languages: A. Feuer and N. Gehani, Comparing and Assessing Programming Languages, Ada, Pascal and C, Prentice Hall, 1984. An excellent text for students wishing to explore further the fundamental principles underlying programming languages: B.J. Maclennan, Principles of Programming Languages: Design, Evaluation and Implementation, Dryden Press, 1987. BELL_C16.QXD 1/30/05 4:24 PM Page 235 236 Chapter 16 ■ Programming in the large An authoritative account of the ANSI standard version of the language in a classic book. Ritchie was the designer of C, a language originally closely associated with the UNIX operating system: Brian W. Kernighan and Dennis Ritchie, The C Programming Language, Prentice Hall, 2nd edn, 1988. The definitive source on C++, but not an easy read: Bjarne Stroustrup, The C++ Programming Language, Addison-Wesley, 2nd edn, 1991. This is a selection of books that look at programming languages in general: Carlo Ghezzi and Mehdi Jazayeri, Programming Language Concepts, John Wiley, 1997. Terence Pratt, Programming languages: Design and Implementation, Prentice Hall, 1995. Michael L. Scott, Programming Language Pragmatics, Morgan Kaufman, 1998. Mark Woodman (ed.), Programming languages, International Thomson Computer Press, 1996. BELL_C16.QXD 1/30/05 4:24 PM Page 236 Robust software is software that tolerates faults. Computer faults are often classified according to who or what causes them: ■ user errors ■ software faults (bugs) ■ hardware faults. An example of a user error is entering alphabetic data when numeric data is expect- ed. An example of a software fault is any of the many bugs that inhabit most software systems. An example of a hardware fault is a disk failure or a telecommunication line that fails to respond. In fault tolerance, the hardware and software collaborate in a kind of symbiosis. Sometimes the hardware detects a software fault; sometimes the software detects a hardware fault. In some designs, when a hardware fault occurs, the hardware copes with the situation, but often it is the role of the software to deal with the problem. When a software fault occurs, it is usually the job of the software to deal with the problem. In 17.1 ● Introduction CHAPTER 17 Software robustness This chapter explains: ■ how to categorize faults ■ how faults can be detected ■ how recovery can be made from faults ■ exception handling ■ recovery blocks ■ how to use n-version programming ■ how to use assertions. BELL_C17.QXD 1/30/05 4:24 PM Page 237 . literature which critically evaluate and compare these three programming languages: A. Feuer and N. Gehani, Comparing and Assessing Programming Languages, Ada, Pascal and C, Prentice Hall, 1984. An. data is expect- ed. An example of a software fault is any of the many bugs that inhabit most software systems. An example of a hardware fault is a disk failure or a telecommunication line that. that Game has agreed to conform to certain behavior. The code for implementing InterfaceA and InterfaceB has to be written as part of the class Game. A programming language is ill suited for

Ngày đăng: 03/07/2014, 01:20