简体   繁体   中英

Java - Possible use of Strategy Design Pattern?

public class ClassA_V01 {
    private String name;
    private int age;

    // getter and setter
}

public class ClassA_V02 {
    private String name;
    private int age;
    private int gender;

    // getter and setter
}   

public static void main(String[] args) {
    SomeClass classA = new ClassA_V01();
    classA.setName("myName);
    classA.setAge(99);
    performLogic(classA);

    // OR
    SomeClass classA = new ClassA_V02();
    classA.setName("myName);
    classA.setAge(99);
    classA.setAge(1);
    performLogic(classA);
}

public void performLogic(SomeClass classA) {
    // do something
}

For strategy pattern to work, both classes must implement the same methods defined in the interface. But what if the classes need to have different fields and methods?

In my example, ClassA_V01 and ClassA_V02 are the same class except that one has more attribute "gender"

How does one implement the above such that classA can be equals to either ClassA_V01() or ClassA_V02?

" ...For strategy pattern to work, both classes must implement the same methods defined in the interface. But what if the classes need to have different fields and methods?... " really this is not a criteria for strategy pattern.

Strategy pattern's intent is to identify and make family of algorithms interchangeable . If you read the pattern's documentation carefully, Strategy can be used when many related classes differ only in their behavior .

Appropriate decomposition is the key for better (extendable) design. A typical (but primitive) solution to Employee assignment, sub-classing tempEmp and permanentEmp types will put us in trouble and will not allow temp employee to become permanent in its life time (which has no meaning in real terms). This happens because we miss an important point- each employees employeeness is not different, they are all same type of employees with different pay policies . (same logic can be extended for Leave policy and so on)

This becomes simple if all types of employees have Salary computation based on same components (same state). But your question is what if TempEmployee gets only basicPay whereas PermanentEmployee gets basicPay as well as travelAllowance (additional attribute which is not present for TempEmp). This can be modeled by a combination of simple inheritance hierarchy along with strategy taking care of computation algorithm dependent upon Employee's (aka. Context) attribute (age)

public class Employee {
    //name and id
    private PayPackage payPackage;
    private int age;
    PayPackage strategy;
    public double computeSalary() {
        return payPackage.computePay(age);
    }
    //get/setPayPackage(...)
}
public abstract class PayPackage {
    private double basicPay;
    abstract public double computePay(int age);
    protected double getBasicPay(){
        return basicPay;
    }
}
public class TempPayPackage extends PayPackage{
    @Override
    public double computePay(int age) {
        double veteranAllowance = 0;
        if (age > 40) {
            veteranAllowance = 2000.00;
        }
        return getBasicPay() + veteranAllowance;
    }
}
public class PermanentPayPackage extends PayPackage{
    private double travelAllowance;
    @Override
    public double computePay(int age) {
        double veteranAllowance = 0;
        if (age > 40) {
            veteranAllowance = 5000.00;
        }
        return getBasicPay() + travelAllowance + veteranAllowance;
    }
}

Important thing to remember is Design patterns never work alone or as an alternative, they work hand in hand with Object oriented code and other patterns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM