import java.io.*; class Counter{ private int value; public Counter(int v){ System.out.println("init me here in The Counter with value!"); value=v; }
public Counter(Counter cc){ System.out.println("init me here in The Counter with class!"); value=cc.value; }
public int read_value(){ System.out.println("read me here The value is:"+value); System.out.println("read me here in The Counter!");
return value; }
public void increment(){ System.out.println("increment me here in The Counter !"); value++; }
public void decrement(){ System.out.println("decrement me here in The Counter !"); value--; } }
class Decorator extends Counter { Counter counter; public Decorator(Counter c) { super(c); System.out.println("init me here with class Decorator!"); } }
class UpperLimit extends Decorator//上限控制 { public UpperLimit(Counter c) { super(c); counter=c; System.out.println("init me here with class UpperLimit!"); } public void increment() { if(counter.read_value()>20) { System.out.println("Too High"); } else { System.out.println("increment me here with class UpperLimit!"); counter.increment(); } } /*public void decrement() { counter.decrement(); } public int read_value() { return counter.read_value(); }*/
}
class LowerLimit extends Decorator//下限控制 { public LowerLimit(Counter c) { super(c); counter=c; System.out.println("init me here in The Counter with class LowerLimit!"); } public void decrement() { System.out.println("Class value :"+read_value()); System.out.println("Dec value :"+counter.read_value()); if(counter.read_value()<=0) { System.out.println(counter.read_value()); System.out.println("Too Low"); } else { System.out.println("decrement me here in The Counter with class LowerLimit!"); counter.decrement(); } } /*public void increment() { counter.increment(); } public int read_value() { return counter.read_value(); }*/ }
class CounterFactory { public static Counter createCounter(int value,int op) { switch(op) { case 1: { return new Counter(value); } case 2: { return new UpperLimit(new Counter(value)); } case 3: { return new LowerLimit(new Counter(value)); } default: { return new UpperLimit(new LowerLimit(new Counter(value))); } } }
} class Console { private static BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
public static int readInt(String index){ System.out.println(index); try{ return Integer.parseInt(read.readLine()); } catch(Exception e){ return 0; } } }
public class Q1s{ public static void main(String[] args){ System.out.println("Counter Type:"); System.out.println("1: Normal"); System.out.println("2: Upper Limit"); System.out.println("3: Lower Limit"); System.out.println("4: Upper & Lower Limit"); int option=Console.readInt("Enter Choice:"); Counter c = CounterFactory.createCounter(6,option); int choice=1; while(choice!=4){ System.out.println("1: Increment"); System.out.println("2: Decrement"); System.out.println("3: Read Value"); System.out.println("4: Exit"); choice=Console.readInt("Enter Choice:"); switch(choice){ case 1: c.increment(); break; case 2: c.decrement(); break; case 3: int v=c.read_value(); System.out.println("Value="+v);break; } } } } 按如下步骤运行出现明显问题: