美文网首页
Replace Type Code with State/Str

Replace Type Code with State/Str

作者: 坚持编程_lyz | 来源:发表于2017-08-03 04:20 被阅读40次

你有一个类型码,它会影响类的行为,但你无法通过继承手法消除它. 以状态对象取代类型码.

如果你的宿主对象不能继承或是类型码的值在对象的生命周期中发生变化,那么可以使用本重构;
1. 新建一个类以类型码命名 这是状态对象,并创建他的子类,重写状态码的方法;
2. 在宿主类,组合生成对象.并且用多态对象返回状态码,使用这个多态对象来调用子类的方法;
public class Employee {
    private int type;
//    public static final int ENGINEER = 0;
//    public static final int SALEMAN = 1;
//    public static final int MANAGER = 2;
    private EmployeeType mEmployeeType;

    public Employee(int type) {
//        setType(type);
        mEmployeeType= EmployeeType.newType(type);
    }

    public int getType() {
//        return type;
        return mEmployeeType.getTypeCode();
    }

//    }

    //    public void setType(int type) {
////        this.type = type;
//        switch (type) {
//            case ENGINEER:
//                mEmployeeType = new Engineer();
//                break;
//            case SALEMAN:
//                mEmployeeType = new SaleMan();
//                break;
//            case MANAGER:
//                mEmployeeType = new Manager();
//                break;
//            default:
//                throw new RuntimeException("Incorrect Employee code");
//        }
    int payAmount() {
        switch (getType()) {
            case ENGINEER:
                return 100;
            case SALEMAN:
                return 200;
            case MANAGER:
                return 1000;
            default:
                throw new RuntimeException("Incorrect Employee");
        }
    }





    public static void main(String[] args) {
        Employee engineerEmployee = new Employee(ENGINEER);
        engineerEmployee.payAmount();
        System.out.println(engineerEmployee.payAmount());
        Employee salemanEmployee = new Employee(SALEMAN);
        System.out.println(salemanEmployee.payAmount());
        Employee managerEmployee = new Employee(MANAGER);
        managerEmployee.payAmount();

    }
}



public abstract class EmployeeType {
    abstract int getTypeCode();

    static final int ENGINEER = 0;
    static final int SALEMAN = 1;
    static final int MANAGER = 2;

    public static EmployeeType newType(int code) {
        switch (code) {
            case ENGINEER:
                return new Engineer();
            case SALEMAN:
                return new SaleMan();
            case MANAGER:
                return new Manager();
            default:
                throw new IllegalArgumentException("Incorrect Employee code");
        }
    }
}


public class Engineer extends EmployeeType {
    @Override
    int getTypeCode() {
        return ENGINEER;
    }
}
public class Manager extends EmployeeType {
    @Override
    int getTypeCode() {
        return MANAGER;
    }
}
public class SaleMan extends EmployeeType {
    @Override
    int getTypeCode() {
        return SALEMAN;
    }
}

相关文章

网友评论

      本文标题:Replace Type Code with State/Str

      本文链接:https://www.haomeiwen.com/subject/zisulxtx.html