Pipes slide # 2
Hinges slide # 3
One can not modify existing software arbitarily
Software has designated places where it is designed to change
These spots can be though of as hinges or hot spots
Changes at one of these hinges is relatively easy
Changing software in other locations or ways tends to:
Force
class Example extends Frame { public Example() { setTitle( "Hinge Example" ); resize( 100, 100 ); setLayout( new FlowLayout() ); add( new Button( "CS 596") ); show(); } public boolean action( Event processNow, Object buttonPressed ) { if ( buttonPressed.equals( "CS 596" ) ) System.out.println("Pressed"); return true; } }First Hinge
class Example extends Frame { String courseName = "CS 596"; public Example() { setTitle( "Hinge Example" ); resize( 100, 100 ); setLayout( new FlowLayout() ); add( new Button( courseName ) ); show(); } public boolean action( Event processNow, Object buttonPressed ) { if ( buttonPressed.equals( courseName ) ) System.out.println("Pressed"); return true; } }
class Example extends Frame { String courseName; public Example() { try { ASCIIInputStream cin = new ASCIIInputStream( new BufferedInputStream ( new FileInputStream( "ExampleConfig" )) ); courseName = cin.readWord() + cin.readWord(); setTitle( "Hinge Example" ); resize( 100, 100 ); setLayout( new FlowLayout() ); add( new Button( courseName ) ); show(); } catch ( FileNotFoundException noFile ) { System.out.println( "No Config file"); } catch ( IOException readError ) { System.out.println( "Read Error"); } } public boolean action( Event processNow, Object buttonPressed ) //Same as before }
How extendable?
Parsing
class LabeledDataExample { public static void main( String[] arguments) { LabeledData defaults = new LabeledData(); defaults.put( "instructor", "Roger Whitney" ); defaults.put( "courseName", "CS108" ); String serialized = defaults.toString(); System.out.println( serialized ); LabeledData refried = new LabeledData( ); refried.fromString( serialized ); System.out.println( refried.getData("courseName" ) ); System.out.println( refried.getData("instructor" ) ); OutputStream cout; cout = new BufferedOutputStream ( new FileOutputStream( "test" ) ); refried.save( cout ); } }Output
# a comment, goes to the end of the line instructor = 'Roger Whitney'; # 's are used keep name one string courseName = 'CS 596'; courseLocation = ba254 # ; is a separator, not a terminator
public static void main( String[] arguments ) { try { InputStream cin = new BufferedInputStream ( new FileInputStream( "ExampleConfig" ) ); LabeledData config = new LabeledData(); config.load( cin ); System.out.println( config.getData("courseName" )); System.out.println( config.getData("instructor" )); } catch ( IOException readError ) { System.out.println( readError ); } }Output
class Example extends Frame { String courseName; public Example() { LabeledData settings = readConfigFile(); courseName = settings.getData( "courseName" ); setTitle( "Hinge Example" ); resize( 100, 100 ); setLayout( new FlowLayout() ); add( new Button( courseName ) ); show(); } public boolean action( Event processNow, Object buttonPressed ) { if ( buttonPressed.equals( courseName ) ) System.out.println("Pressed"); return true; }Third Hinge Example Continued
public LabeledData readConfigFile() { try { InputStream cin = new BufferedInputStream ( new FileInputStream( "ExampleConfig" ) ); LabeledData config = new LabeledData(); config.load( cin ); return config; } catch ( FileNotFoundException noFile ) { System.out.println( "No Config file"); } catch ( IOException readError ) { System.out.println( "Read Error"); } } }// end class
Labels on data:
If absolute, multiple users cause problems
If relative, calling from different directorys is a problem
Others?
Need a better hinge
class LabeledDataExample { public static void main( String[] arguments) { LabeledData config = new LabeledData(); config.fromCommandLine( arguments ); System.out.println( config.getData("courseName" )); } }
java LabeledDataExample -courseName=CS596 -instructor WhitneyDefault Values & LabeledDataOutput CS596Flag Options - flag=value- flag value- flag (for last argument or next argument starts with - )-- zyx (individual characters become separate flags)-- (ignore rest of the arguments) Flags with no values are given value of LabeledData.NO_VALUE Which is the string "_NO_VALUE"
class LabeledDataExample { public static void main( String[] arguments) { LabeledData config = new LabeledData(); config.fromCommandLine( arguments ); String name = config.getData("courseName", "CS108" ); System.out.println( name ); } }
java LabeledDataExample -instructor WhitneyOutput
If the hashtable does not contain "key", then getData returns
"defaultValue"
class LabeledDataExample { public static void main( String[] arguments) { LabeledData defaults = new LabeledData(); defaults.put( "instructor", "Roger Whitney" ); defaults.put( "courseName", "CS108" ); LabeledData config = new LabeledData( defaults ); config.fromCommandLine( arguments ); System.out.println( config.getData( "courseName" ) ); System.out.println( config.getData( "instructor" ) ); } }
/* Constructs a new, empty labeledData object with the specified * default values. */ public LabeledData( LabeledData defaultValues )
class Example extends Frame { String courseName; public static void main( String[] arguments ) { new Example( getProgramParameters( arguments ) ); } public Example( LabeledData settings ) { courseName = settings.getData( "courseName" ); setTitle( "Hinge Example" ); resize( 100, 100 ); setLayout( new FlowLayout() ); add( new Button( courseName ) ); show(); } public boolean action( Event processNow, Object buttonPressed ) { if ( buttonPressed.equals( courseName ) ) System.out.println("Pressed"); return true; }Fourth Hinge Example Continued
public static LabeledData getProgramParameters( String[] arguments ) { LabeledData defaults = new LabeledData(); LabeledData configFile = new LabeledData( defaults ); LabeledData parameters = new LabeledData( configFile ); defaults.put( "configFile", "ExampleConfig"); defaults.put( "courseName", "CS 596"); paramaters.fromCommandLine( arguments ); try { String fileName = parameters.getData( "configFile" ); InputStream cin; cin = new BufferedInputStream ( new FileInputStream( fileName ) ); configFile.load( cin ); } catch ( FileNotFoundException noFile ) { System.out.println( "No Config file"); } catch ( IOException readError ) { System.out.println( "Read Error"); } return parameters; } }How About This?
class Example extends Frame { String courseName; static String courseNameFlag = "courseName"; static String configFileFlag = "configFile"; static String windowTitleFlag = "windowTitle"; static String windowWidthFlag = "windowWidth"; static String windowHeightFlag = "windowHeight"; public static void main( String[] arguments ) { new Example( getProgramParameters( arguments ) ); } public Example( LabeledData settings ) { setTitle( settings.getData( windowTitleFlag )); int width = Integer.parseInt( settings.getData( windowWidthFlag ) ); int height = Integer.parseInt( settings.getData( windowHeightFlag ) ); resize( width, height ); setLayout( new FlowLayout() ); courseName = settings.getData( courseNameFlag ); add( new Button( courseName ) ); show(); }Continued
public boolean action( Event processNow, Object buttonPressed ) { if ( buttonPressed.equals( courseName ) ) System.out.println("Pressed"); return true; } public static LabeledData getProgramParameters( String[] arguments ){ LabeledData defaults = new LabeledData(); LabeledData configFile = new LabeledData( defaults ); LabeledData paramaters = new LabeledData( configFile ); defaults.put( configFileFlag, "ExampleConfig"); defaults.put( courseNameFlag, "CS 596"); paramaters.fromCommandLine( arguments ); try { String fileName = paramaters.getData( configFileFlag ); InputStream cin; cin = new BufferedInputStream ( new FileInputStream( fileName ) ); configFile.load( cin ); } catch ( FileNotFoundException noFile ) { System.out.println( "No Config file"); } catch ( IOException readError ) { System.out.println( "Read Error"); } return paramaters; } }
class Example extends Frame { public Example() { setTitle( "Hinge Example" ); resize( 100, 100 ); setLayout( new FlowLayout() ); add( new Button( "CS 596") ); show(); } public boolean action( Event processNow, Object buttonPressed ) { if ( buttonPressed.equals( "CS 596" ) ) System.out.println("Pressed"); return true; } }
The balance between force and counter-force in school is off
scale compaired to "real life", it is all counter-force