策略模式

来源:互联网 发布:安卓版本 知乎 编辑:程序博客网 时间:2024/05/17 18:14
public class Zhaoyun {    public static void main(String[] args) {        Test test=new Test(new FirstSgy());        test.operation();        test.setSgy(new SecondSgy());        test.operation();        test.setSgy(new ThirdSgy());        test.operation();    }}interface Isgy{    public void operation();}class FirstSgy implements Isgy{    public void operation(){        System.out.println("第一条妙计");    }}class SecondSgy implements Isgy{    public void operation(){        System.out.println("第二条妙计");    }}class ThirdSgy implements Isgy{    public void operation(){        System.out.println("第三条妙计");    }}class Test{    private Isgy sgy;    public Test(Isgy sgy) {        super();        this.sgy = sgy;    }    public Isgy getSgy() {        return sgy;    }    public void setSgy(Isgy sgy) {        this.sgy = sgy;    }    public void operation(){        sgy.operation();    }}
原创粉丝点击