180 likes | 292 Views
Development by Extension. Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes may not quite do what you want them to. Extending an existing class will allow you to make it do exactly what you want it to.
E N D
Development by Extension Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes may not quite do what you want them to. Extending an existing class will allow you to make it do exactly what you want it to. Developing a class hierarchy from the start makes it more likely that you will find an existing class suitable to extend.
a b c d e f a b c d e f Managing Complexity Building five small classes in a hierarchy is simpler that building one large class (although it may not seem so at first!).
test test test test d a b c build build build build design design design design Design, build, test ... Production can be divided into a number of stages. In each stage a class is designed, built and tested beforenext class in the hierarchy is started.
The Counters hierarchy BasicCounter LimitedCounter WarningCounter StoppingCounter RolloverCounter RoomMonitor MoneyRegister The Counters hierarchy supplies the functionality of a hand held counter. The BasicCounter supplies the essential counting behaviour, the LimitedCounter adds limits to the range it can count in and the three *Counter classes provide specialised counting behaviour.
BasicCounter BasicCounter count unCount reset numberCountedIs setCountTo BasicCounter class diagram Object BasicCounter packagecounters counted initialCount theInitialCount setTo occurrences
BasicCounter header Object BasicCounter packagecounters 0001 // Filename counters/BasicCounter.java. 0002 // Root class of the Counters hierarchy providing the 0003 // essential counting functionality. 0004 // 0005 // This version written for SDO lecture 1. 0006 // Fintan Culwin, v0.1, February 1998. 0007 0008 packagecounters; 0009 0010 public classBasicCounterextends Object {
BasicCounter - data attributes counted theInitialCount 0011 0012 private intcounted = 0; 0013 private inttheInitialCount = 0; 0014 These are encapsulated instance attributes, of the primitive int type. Every instance of the class will have their own copy of them.
BasicCounter BasicCounter BasicCounter - constructors initialCount 0015 publicBasicCounter() { 0016 this( 0); 0017 } // End default constructor. 0018 0019 publicBasicCounter( intinitialCount) { 0020 counted = initialCount; 0021 theInitialCount = initialCount; 0022 } // End alternative constructor. The purpose of a constructor is to place the new instance of the class into a well defined initial state. (This usually means setting the state of the instance attributes.)
count unCount reset BasicCounter - counting methods 0024 public voidcount() { 0025 counted++; 0026 } // End count. 0027 0028 public voidunCount() { 0029 counted--; 0030 } // end unCount. 0031 0032 public voidreset() { 0033 counted = theInitialCount; 0034 } // End numberCountedIs. These methods change the state of the instance by changing the values of its instance attributes.
numberCountedIs BasicCounter - inquiry method occurrences 0036 public intnumberCountedIs() { 0037 returncounted; 0038 } // End numberCountedIs. This is an inquiry method which returns some information about the internal state of the instance.
setCountTo BasicCounter - setCountTo() method setTo 0040 protected voidsetCountTo( intsetTo) { 0041 counted = setTo; 0042 } // End setCountTo. 0043 0044 } // End BasicCounter. This protected method can only be seen, and so used, by other classes in the same package of classes.
Object is a is a BasicCounterDemo BasicCounter instance of instance of demonstrates main demoCounter BasicCounterDemo - instance diagram Instance diagrams show the roles and relationships relationships between the class instances which make up a program.
BasicCounterDemo - constructors 0008 packagecounters; 0009 0010 classBasicCounterDemonstration { 0011 0012 public static void main( String argv[]) { 0013 0014 BasicCounter aCounter; 0015 0016 System.out.println( "\n\n\t Basic Counter Demonstration"); 0017 0018 System.out.print( "\n\nConstructing an instance with “); 0019 System.out.println( "the initial value 4."); 0020 aCounter = newBasicCounter( 4); 0021 System.out.print( "Instance created ... "); Basic Counter Demonstration Constructing an instance with the initial value 4. Instance created ...
BasicCounterDemo - NumberCountedIs() 0023 0024 System.out.print( "\n\nDemonstrating numberCountedIs()”+ 0025 “, it should be 4 ... "); 0026 System.out.println( aCounter.numberCountedIs()); 0027 Demonstrating numberCountedIs(), it should be 4 ... 4
BasicCounterDemo - counting methods 0028 System.out.println( "\n\nDemonstrating count()."); 0029 aCounter.count(); 0030 System.out.print( "Showing the changed value, it should be 5 ... "); 0031 System.out.println( aCounter.numberCountedIs()); 0032 0033 System.out.println( "\n\nDemonstrating unCount(). "); 0034 aCounter.unCount(); 0035 System.out.print( "Showing the changed value, it should be 4 ... "); 0036 System.out.println( aCounter.numberCountedIs()); Demonstrating count(). Showing the changed value, it should be 5 ...5 Demonstrating unCount(). Showing the changed value, it should be 4 ...4
BasicCounterDemo - setCountTo() 0038 System.out.println( "\n\nDemonstrating setCountTo()”+ “, setting to 10."); 0039 aCounter.setCountTo( 10); 0040 System.out.print( "Showing the changed value, “ + “it should be 10 ... "); 0041 System.out.println( aCounter.numberCountedIs()); Demonstrating setCountTo(), setting to 10. Showing the changed value, it should be 10 ... 10 As setCountTo() is protected, it can only be demonstratedif it is temporarily made public.
BasicCounterDemo - reset() 0043 System.out.println( "\n\nDemonstrating reset()."); 0044 aCounter.reset(); 0045 System.out.print( "Showing the reset value, it should be 4 ... "); 0046 System.out.println( aCounter.numberCountedIs()); Demonstrating reset(). Showing the reset value, it should be 4 ... 4
BasicCounterDemo - second instance 0049 System.out.print( "\n\nConstructing a new instance “); 0050 System.out.println( “with the default value 0."); 0051 aCounter = newBasicCounter(); 0052 System.out.println( "New instance created ..."); 0053 System.out.print( "\n\nShowing the value of the new instance,“); 0054 System.out.print( "it should be 0 ... "); 0055 System.out.println( aCounter.numberCountedIs()); 0056 0057 System.out.println( "\n\nDemonstration finished.\n\n"); 0058 } // End main. 0059 0060 } // End class BasicCounterDemonstration. Constructing a new instance with the default value 0. New instance created ... Showing the value of the new instance, it should be 0 ... 0 Demonstration finished.