策略模式
程序员文章站
2022-07-15 09:06:27
...
//通用接口
public interface IStrate {
public void operate();
}
//第一步
public class One implements IStrate {
@Override
public void operate() {
System.out.println("第一步开始");
}
}
//第二步
public class Two implements IStrate {
@Override
public void operate() {
System.out.println("第二步开始");
}
}
public class Context {
private IStrate istrate;
public Context(IStrate istrate){
this.istrate=istrate;
}
public void operate(){
this.istrate.operate();
}
}
public class Client {
public static void main(String[] args) {
Context context;
System.out.println("拆第一个妙计");
context = new Context(new One());
context.operate();
System.out.println("拆第二个妙计");
context = new Context(new Two());
context.operate();
System.out.println("拆第三个妙计");
context = new Context(new Three());
context.operate();
}
}
public interface IStrate {
public void operate();
}
//第一步
public class One implements IStrate {
@Override
public void operate() {
System.out.println("第一步开始");
}
}
//第二步
public class Two implements IStrate {
@Override
public void operate() {
System.out.println("第二步开始");
}
}
public class Context {
private IStrate istrate;
public Context(IStrate istrate){
this.istrate=istrate;
}
public void operate(){
this.istrate.operate();
}
}
public class Client {
public static void main(String[] args) {
Context context;
System.out.println("拆第一个妙计");
context = new Context(new One());
context.operate();
System.out.println("拆第二个妙计");
context = new Context(new Two());
context.operate();
System.out.println("拆第三个妙计");
context = new Context(new Three());
context.operate();
}
}
上一篇: java 中Collection 比较
下一篇: tomcat加内存启动