SE450: Practice (do not hand in): Problem 2: State [2/3] Previous pageContentsNext page

Consider the following code:

file:I.java [source] [doc-public] [doc-private]
00001: package state.one;
00002: 
00003: interface I {
00004:   public int f();
00005:   public int g();
00006:   public void changeDirection();
00007: }
00008: 
00009: class C implements I {
00010:   private boolean state;
00011:   private int i;
00012:   private int j;
00013:   public int f() {
00014:     if (state)
00015:       i += 26;
00016:     else
00017:       i -= 32;
00018:     return i;
00019:   }
00020:   public int g() {
00021:     if (state)
00022:       j += 42;
00023:     else
00024:       j -= 27;
00025:     return j;
00026:   }
00027:   public void changeDirection() {
00028:     state = !state;
00029:   }
00030: }
00031: 

Refactor using the state pattern.

Draw a UML class diagram of your solution.

Draw a UML sequence diagram of your solution.

Explain the difference between intrinsic and extrinsic state. Which solution type did you use?

Previous pageContentsNext page