public class ApplicationRunner {
private Application itsApplication = null;
public ApplicationRunner(Application app)
{
itsApplication = app;
}
public void run()
{
itsApplication.init();
itsApplication .idle();
itsApplication.cleanup();
}
}
public interface Application {
public void init();
public void idle();
public void cleanup();
}
public class ftocStrat implements Application {
//实现Application
new ApplicationRunner实例
}
1、单例模式 SINGLETON
Singleton类的实例不会超过一个。
实现:
public class Singleton {
private static Singleton theInstance = null;
private Singleton(){}
public static Singleton Instance() {
if(theInstance == null)
theInstance = new Singleton();
return theInstance;
}
}
2、MONOSTATE模式
实现:
Public class Monostate {
private static int itsX = 0;
public Monostate(){}
public void set(int x) {
itsX=x;
}
public int getX()
{ return itsX; }
}
3、NULL OBJECT模式
该模式通常会消除对null进行检查的需要,并且有助于代码简化。
实现:
public interface Employee {
public boolean isTimeToPlay(Date payDate);
public void pay();
public static final Employee NULL = new Employee()
{
public boolean isTimeToPay(Date payDate)
{
return false;
}
public void pay()
{
}
}
}
if(e == Employee.NULL)
4、模板方法模式
实现:
public abstract class Application {
private boolean isDone = false;
protected abstractca void init();
protected abstractca void idle();
protected abstractca void cleanup();
protected void setDone()
{
isDone = true;
}
protected boolean done()
{
return isDone;
}
public void run()
{
init();
idle();
cleanup();
}
} //子类实现
5、策略模式
public class ApplicationRunner
{
private Application itsApplication = null;
public ApplicationRunner(Application app)
{
itsApplication = app;
}
public void run()
{
itsApplication.init();
itsApplication .idle();
itsApplication.cleanup();
}
}
public interface Application {
public void init();
public void idle();
public void cleanup();
}
public class ftocStrat implements Application {
//实现Application
new ApplicationRunner实例
}