2011-06-30 28 views
5

Tôi có chi nhánh if-else khác trong java.Sửa đổi if-else thành mẫu chiến lược

if (str.equals("a")) { A;} 
else if (str.equals("b")) { B;} 
else if (str.equals("c")) { C;} 
else if (str.length == 5) { D;} 
else { E;} 

cách sửa đổi mã này thành mẫu chiến lược?

+1

Có thể thay vào đó bạn có thể xem xét sử dụng Enums và Chuyển báo cáo? Mô hình chiến lược sẽ có ý nghĩa nếu bạn cần trao đổi việc triển khai cơ bản. Ví dụ cổ điển sẽ là các thuật toán sắp xếp khác nhau. – CoolBeans

Trả lời

9

Dưới đây là một ví dụ về một mô hình chiến lược sử dụng một nhà máy:

public interface Strategy { 
    public Object[] execute(Object[] args); 
} 

public class StrategyFactory { 

    public enum Name { 
     REVERSE, STRINGIFY, DUPLICATE; 
    } 

    private StrategyFactory() { 
     // never instantiate; only use static factory methods 
    } 

    public static Strategy getStrategyReverse() { 
     return new Strategy() { 
      public Object[] execute(Object[] args) { 
       Object[] reversed = new Object[args.length]; 
       for (int i = 0; i < args.length; i++) { 
        reversed[i] = args[args.length - i - 1]; 
       } 
       return reversed; 
      } 
     }; 
    } 

    public static Strategy getStrategyStringify() { 
     return new Strategy() { 
      public Object[] execute(Object[] args) { 
       String[] stringified = new String[args.length]; 
       for (int i = 0; i < args.length; i++) { 
        stringified[i] = String.valueOf(args[i]); 
       } 
       return stringified; 
      } 
     }; 
    } 

    public static Strategy getStrategyDuplicate() { 
     return new Strategy() { 
      public Object[] execute(Object[] args) { 
       Object[] duplicated = new Object[2 * args.length]; 
       for (int i = 0; i < args.length; i++) { 
        duplicated[i * 2] = args[i]; 
        duplicated[i * 2 + 1] = args[i]; 
       } 
       return duplicated; 
      } 
     }; 
    } 

    public static Strategy getStrategy(String name) { 
     return getStrategy(Name.valueOf(name)); 
    } 

    public static Strategy getStrategy(Name name) { 
     switch (name) { 
      case REVERSE: 
       return getStrategyReverse(); 
      case STRINGIFY: 
       return getStrategyStringify(); 
      case DUPLICATE: 
       return getStrategyDuplicate(); 
      default: 
       throw new IllegalStateException("No strategy known with name " + name); 
     } 
    } 
} 

public class Main { 
    public static void main(String[] args) { 

     Strategy strategy = StrategyFactory.getStrategy("DUPLICATE"); 
     System.out.println(Arrays.toString(strategy.execute(args))); 
    } 
} 
0

Bạn phải suy nghĩ về mặt lập trình hướng đối tượng. Sử dụng đa hình. Đối với mẫu chiến lược, Xác định giao diện và cung cấp các triển khai khác nhau cho các lớp thực hiện giao diện. Chọn bối cảnh và quyết định lớp ploymorphically. http://en.wikipedia.org/wiki/Strategy_pattern

Tuy nhiên, đối với if-else mẫu chính xác của bạn tương ứng với 'Mẫu nhà máy'. http://en.wikipedia.org/wiki/Factory_method_pattern

Các vấn đề liên quan