I'm learning about Design patterns in java and I can't understand which one is this and why?
I especially confuse the facade and factory design patterns.
public abstract class Ghost {
public Ghost() { System.out.println(getClass().getName()); }
public abstract void tick(Shell shell);
public void tock(Shell shell) {}
}
public class A extends Ghost {
public void tick(Shell shell) { shell.use(new C()); }
}
public class B extends Ghost {
public void tick(Shell shell) { shell.use(new A()); }
public void tock(Shell shell) { shell.use(new A()); }
}
public class C extends Ghost {
public void tick(Shell shell) { shell.use(new B()); }
public void tock(Shell shell) { shell.use(new A()); }
}
public class Shell {
Ghost _ghost = new A();
public void tick() { _ghost.tick(this); }
public void tock() { _ghost.tock(this); }
public void use(Ghost ghost) { _ghost = ghost; }
}
public class GhostInTheShell {
public static void main(String args[]) {
Shell shell = new Shell();
shell.tick();
//!2.7
shell.tick(); shell.tock(); shell.tick();
}
}
Aucun commentaire:
Enregistrer un commentaire