Chapter 7: Compile-Time Variabilities

Summary

Compile-time variabilities are decisions made at compile time. The compiler may take advantage of this information to optimize programs. Unfortunately, the Java language does not provide ideal facilities for simultaneously separating concerns between variabilities and commonalities and thus limits what can be accomplished. Compile-time variabilities may also be implemented with run-time mechanisms, but then you would lose any potential compile-time optimizations.



Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

Example 7-1: MyConfig.java-an Example of Separating Compile-Time Constants from the Program
Example 7-2: MyFrame4.java-Using Compile-Time Constants in MyConfig.java
Example 7-9: Play1CT.java
Example 7-10: JackAndJillCT.java

Example 7-1: MyConfig.java-an Example of Separating Compile-Time Constants from the Program

public class MyConfig {
    
    public static final int width = 300;
    public static final int height = 120;
    public static final String title = "MyFrame Example";
    public static final MyButton[] buttons = {
        new MyButton("xyz", "#000000", "#ffffff"),
        new MyButton("abc", "#000000", "#ffffff"),
        new MyButton("def", "#00ff00", "#ff00ff")
        
    };
}

class MyButton {
    String label;
    String foreground;
    String background;
    
    MyButton(String l, String fg, String bg) {
        label = l;
        foreground = fg;
        background = bg;
    }
}

Example 7-2: MyFrame4.java-Using Compile-Time Constants in MyConfig.java

import java.awt.*;

class MyFrame4 extends Frame {
    
    public MyFrame4() {
        super(MyConfig.title);
        setLayout(new FlowLayout());
        for (int j=0; j<MyConfig.buttons.length; ++j) {
            MyButton bdata = MyConfig.buttons[j];
            Button b = new Button(bdata.label);
            b.setBackground(Color.decode(
                bdata.background));
            b.setForeground(Color.decode(
                bdata.foreground));
            add(b);
        }
        setSize(MyConfig.width, MyConfig.height);
        show();
    }
        
    public static void main(String args[]) {
        new MyFrame4();
    }
}

Example 7-9: Play1CT.java

import java.awt.*;
import java.awt.event.*;

public class Play1CT extends Frame {
    public String playname;
    public String title;
    public int width = 500;
    public int height = 250;
    public Scene start = null;
    
    public void init() {
        setTitle(title);
        setSize(width, height);
        setLayout(new FlowLayout());
        start.enterScene();
    }
        
    public class Prop extends Button implements ActionListener {
        String name;
        
        public Prop()
        {
            addActionListener(this);
        }
        
        public void actionPerformed(ActionEvent e) {
            // script is coded here
        }
    }
    
    public class Scene {
        public String name;
        public String color;
        
        public void initScene() {
            // initialization script coded here
        }
        
        public void enterScene() {
            removeAll();
            setBackground(Color.decode(color));
            initScene();
            show();
        }
    }
}

Example 7-10: JackAndJillCT.java

import java.awt.*;
import java.awt.event.*;

public class JackAndJillCT extends Play1CT {
    { playname = "JackAndJill";
      title = "Jack and Jill";
      width = 200;
      height = 120;
    }
           
    class UpProp extends Prop {
        { name = "up";
          setLabel("Go up the hill");
        }
        
        public void actionPerformed(ActionEvent e) {
            fetchProp.setLabel("Fetch a pail of water");
            topScene.enterScene();
        }
    }
    
    UpProp upProp = new UpProp();
    
    class FetchProp extends Prop {
        { name = "fetch";
          setLabel("Fetch a pail of water");
        }
        
        public void actionPerformed(ActionEvent e) {
            setLabel("Fetch another pail");
        }
    }
    
    FetchProp fetchProp = new FetchProp();
    
    class FallProp extends Prop {
        { name = "fall";
          setLabel("Fall down, break crown");
        }
        
        public void actionPerformed(ActionEvent e) {
            setLabel("Break crown");
            tumbleProp.setLabel("Tumble after");
            bottomScene.enterScene();
        }
    }
    
    FallProp fallProp = new FallProp();
    
    class TumbleProp extends Prop {
        { name = "tumble";
          setLabel("Tumble down");
        }
        
        public void actionPerformed(ActionEvent e) {
            bottomScene.enterScene();
        }
    }
    
    TumbleProp tumbleProp = new TumbleProp();
         
    class BottomScene extends Scene {
        { name = "bottom";
          color = "#8888aa";
        }
        
        public void initScene() {
            add(upProp);
            add(fallProp);
        }
    }
    
    BottomScene bottomScene = new BottomScene();
     
    class TopScene extends Scene {
        { name = "top";
          color = "#dddddd";
        }
        
        public void initScene() {
            add(fetchProp);
            add(fallProp);
            add(tumbleProp);
        }
    }
    
    TopScene topScene = new TopScene();
    
    {
      start = bottomScene;
    }
    
    public static void main(String[] args) {
        JackAndJillCT p = new JackAndJillCT().init();
    }
}