Presentation is loading. Please wait.

Presentation is loading. Please wait.

Development by Extension Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes.

Similar presentations


Presentation on theme: "Development by Extension Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes."— Presentation transcript:

1 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.

2 Managing Complexity Building five small classes in a hierarchy is simpler that building one large class (although it may not seem so at first!). a b c d e f a b c de f

3 Design, build, test... Production can be divided into a number of stages. In each stage a class is designed, built and tested before next class in the hierarchy is started. a design build test b design build test c design build test d design build test

4 The Counters hierarchy 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 LimitedCounter RolloverCounterStoppingCounterWarningCounter RoomMonitorMoneyRegister

5 BasicCounter class diagram Object BasicCounter number CountedIs BasicCountercountunCountreset set CountTo initialCount occurrencessetTo theInitialCount counted package counters

6 BasicCounter header 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 package counters; 0009 0010 public class BasicCounter extends Object { Object BasicCounter package counters

7 BasicCounter - data attributes 0011 0012 private int counted = 0; 0013 private int theInitialCount = 0; 0014 These are encapsulated instance attributes, of the primitive int type. Every instance of the class will have their own copy of them. theInitialCount counted

8 BasicCounter - constructors 0015 public BasicCounter() { 0016 this( 0); 0017 } // End default constructor. 0018 0019 public BasicCounter( int initialCount) { 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.) BasicCounter initialCount

9 BasicCounter - counting methods 0024 public void count() { 0025 counted++; 0026 } // End count. 0027 0028 public void unCount() { 0029 counted--; 0030 } // end unCount. 0031 0032 public void reset() { 0033 counted = theInitialCount; 0034 } // End numberCountedIs. These methods change the state of the instance by changing the values of its instance attributes. countunCountreset

10 BasicCounter - inquiry method 0036 public int numberCountedIs() { 0037 return counted; 0038 } // End numberCountedIs. This is an inquiry method which return s some information about the internal state of the instance. number CountedIs occurrences

11 BasicCounter - setCountTo() method 0040 protected void setCountTo( int setTo) { 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. set CountTo setTo

12 BasicCounterDemo - instance diagram Instance diagrams show the roles and relationships relationships between the class instances which make up a program. main instance of BasicCounterDemo demoCounter instance of BasicCounter demonstrates is a Object

13 BasicCounterDemo - constructors 0008 package counters; 0009 0010 class BasicCounterDemonstration { 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 = new BasicCounter( 4); 0021 System.out.print( "Instance created... "); Basic Counter Demonstration Constructing an instance with the initial value 4. Instance created...

14 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

15 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

16 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()); As setCountTo() is protected, it can only be demonstrated if it is temporarily made public. Demonstrating setCountTo(), setting to 10. Showing the changed value, it should be 10... 10

17 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

18 BasicCounterDemo - second instance 0049 System.out.print( "\n\nConstructing a new instance “); 0050 System.out.println( “with the default value 0."); 0051 aCounter = new BasicCounter(); 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.


Download ppt "Development by Extension Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes."

Similar presentations


Ads by Google