会员打折之---Strategy设计模式

public interface Member {
    public double discount();

}

---------------------------

public class LowMember implements Member {
    @Override
    public double discount() {
        return 0.9;
    }
}

--------------------------------

public class MiddleMember implements Member {
    @Override
    public double discount() {
        return 0.7;
    }
}

----------------------------------

public class AdvancedMember implements Member{
    @Override
    public double discount() {
        return 0.5;
    }
}

----------------------------------

public class Context {
    private Member member;

    public Context(Member member) {
        this.member = member;
    }
    public double discount() {
        return this.member.discount();
    }
}

----------------------------------

public class Test {
    public static void main(String[] args) {
        /*Member member = new LowMember();
        Context ctx = new Context(member);
        System.out.println("The discount is:  "+ctx.discount());*/
        
        
        /*Member member = new MiddleMember();
        Context ctx = new Context(member);
        System.out.println("The discount is:  "+ctx.discount());*/
        
        
        Member member = new AdvancedMember();
        Context ctx = new Context(member);
        System.out.println("The discount is:  "+ctx.discount());
    }
}


你可能感兴趣的:(会员打折之---Strategy设计模式)