SDSU CS 580 Client-Server Programming
Fall Semester, 2000
Hinges
Previous    Lecture Notes Index    Next    
© 2000, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 30-Aug-00

Contents of Doc 2, Hinges




References

Design Patterns for Object-Oriented Software Development , Pree, Addison-Wesley, 1995

SDSU Java Library, http://www.eli.sdsu.edu/java-SDSU/docs/



Doc 2, Hinges Slide # 2

Hinges


Software is not arbitrarily adaptable

One can not modify existing software arbitrarily


Can add places 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:

break the system
be expensive



Doc 2, Hinges Slide # 3

Force
Adding correct hinges makes a program easier to modify

Counter-Force
There is a cost to building hinges
Hinges can make the software hard to understand
Hard to predict which hinges will be needed



When in doubt keep it out



Doc 2, Hinges Slide # 4

Hinge Example - Constants


Program that uses course numbers

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;
      }
   }




Doc 2, Hinges Slide # 5
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;
      }       
   }

Doc 2, Hinges Slide # 6
Another Hinge

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
}

Doc 2, Hinges Slide # 7
ExampleConfig Contents

CS 569


This is a Hinge?

How safe?

How extendable?




Doc 2, Hinges Slide # 8

Some Hinge Oil

sdsu.util.ProgramProperties


Parses
Configuration files
Command line arguments
Command Line argument

-flag=value
-flag value
-flag
--xyz
--

File Formats
properties format
#A comment to the end of the line
key1=value1
key2=value2 with spaces
key3 with spaces=value3 #part of the value
sdsu.util.LabeledData format
#A comment to the end of the line,
key1 = value1;
key2='value2 with spaces';
'key3 with spaces'=value3; # a comment


Doc 2, Hinges Slide # 9
Simple Example

import sdsu.util.ProgramProperties;
public class ConfigurationExample {
   public static void main(String args[]) {
      try
         {
         ProgramProperties flags = 
            new ProgramProperties( args, "configurationFile");
         String nameValue = 
            flags.getString( "name" , "No name given");
         int size = flags.getInt( "size", 0);
         boolean switchOn = flags.containsKey( "s");
         System.out.println( " nameValue: " + nameValue);
         System.out.println( " size: " + size);
         System.out.println( " switchOn: " + switchOn);
         
         }
      catch (java.io.IOException readParseProblem)
         {
         System.err.println( "Program aborted on error " + 
         readParseProblem);
         }
   }
}

File "configurationFile.labeledData"
name=Roger;
size=12;

Doc 2, Hinges Slide # 10
Sample Runs

java ConfigurationExample

Output
nameValue: Roger
size: 12
switchOn: false


java ConfigurationExample -s -name Pete

Output
nameValue: Pete
size: 12
switchOn: true


java ConfigurationExample -conf=otherFile

Output
nameValue: Sam
size: 8
switchOn: true


Copyright ©, All rights reserved.
2000 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 30-Aug-00    Next