CSC 307 Lecture Notes Week 10 Introduction to Code Coverage
Introduction to "Classic" Design Patterns
public class Singleton { public Singleton() throws AlreadyInstantiatedException { if (isInstantiated) { throw new AlreadyInstantiatedException(getClass().getName()); } isInstantiated = true; } protected static boolean isInstantiated = false; } public class AlreadyInstantiatedException extends RuntimeException { public AlreadyInstantiatedException(String targetClass) { this.targetClass = targetClass; } public String targetClass; }
public class Singleton { private Singleton() { . . . } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } protected static Singleton instance; }
where again the value-added provided by the facade is parameterized versions of extant methods, and the collection of all the tool's user-accessible methods into a single class.
Client Handler1 ... -------- Info1 nextHandler: -------- request(Info) -------- Handler <----- Handler1 ... Handlern Info <----- Info1 ... Infon
class Data { DataState state; public void handle() { state->handle(...) } } interface DataState { void handle(...); } class IdleState implements DataState { void handle(...); } class ActiveState implements DataState { void handle(...); } class WaitingState implements DataState { void handle(...); }
class Data { Enum state ... ; public void handle() { switch (data.state) { case IDLE: handleIdle(...) case ACTIVE: handleActive(...) case WAITING: handleWaiting(...) } } }
lexer ----> Parser ----> Interpreter
public class Data { /** * Compute using current data field values and storing results in the data * fields. */ void compute() { System.out.println("x,y=" + this.x + "," + this.y); } /** * Compute using parameters and returning results. As a side effect, data * fields are set. */ Data compute(int x, int y) { this.x = x; this.y = y; System.out.println("x,y=" + this.x + "," + this.y); return this; } void setX(int x) { this.x = x; } void setY(int y) { this.y = y; } /** * Perform a persistent-data computation by setting the class fields and * calling the field-accessing compute method. */ static void useWithSets() { int a = 1; int b = 2; int c = 3; int d = 4; Data data = new Data(); data.setX(a); data.setY(b); data.compute(); data.setX(c); data.setY(d); data.compute(); } /** * Perform a parametric computation by setting method-local variables, and * calling the parametric compute function. */ static void useWithParms() { int a = 1; int b = 2; int c = 3; int d = 4; Data data = new Data(); data.compute(a,b).compute(c,d); } public static void main(String[] args) { useWithParms(); useWithSets(); } protected static int x; protected static int y; }
__________________ __________________ Producer Consumer __________________ __________________ Data Data __________________ __________________ Data provideData acceptData(Data) __________________ __________________