簡體   English   中英

我正在嘗試使用 Java 8 在以下代碼段中實現策略設計模式。尋找更好的實現方法?

[英]I am trying to implement the strategy design pattern in the below snippet using Java 8 .Looking for better ways to implement?

我有以下服務實施: -

package Java8.controller;

import java.util.function.Function;

public class ServiceImpl implements ITicket {

    Function<Integer,Double> ticketCal;

    public ServiceImpl(Function<Integer,Double> ticketCal){
        this.ticketCal= ticketCal;
    }

    @Override
    public double calculateFare(int quantity) {
        return ticketCal.apply(quantity);
    }

}

以下是我創建的策略:-

躺椅票策略:-

package Java8.controller;

import java.util.function.Function;

public interface ReclinerTicketStrategy {

    default Function<Integer,Double> reclinerTicketStrategy(){

        return (noOfTickets)->{
            return noOfTickets * 200.00;
        };
    }
}

貴賓票策略:-

package Java8.controller;

import java.util.function.Function;

public interface VipTicketStrategy {

    default Function<Integer,Double> vipTicketStrategy(){

         return (noOfTickets)->{
             return noOfTickets*400.00;
        };

    }
}

以下是使用該策略的主要 class :-

package Java8.controller;

public class Main implements ReclinerTicketStrategy {

    public Main(){

        ITicket ticketsVip = new ServiceImpl(reclinerTicketStrategy());
        System.out.println(ticketsVip.calculateFare(5));

}

    public static void main(String args[]){
        Main main = new Main();
    }


}


我的問題是這是否是將界面中的策略 function 作為默認方法的正確方法? 或者有更好的方法嗎?

策略應該實現服務所需的抽象。 在這種情況下,服務需要一個Function<Integer,Double>所以策略是......

class ReclinerTicketStrategy implements Function<Integer,Double> {
    @Override
    public Double apply(Integer noOfTickets) {
        return noOfTickets * 200.0;
    }
}

然后將服務實例化為,
ITicket reclinerTicket = new ServiceImpl(new ReclinerTicketStrategy());

當然,如果不需要重用策略,那么一個 lambda 就足夠了。
ITicket reclinerTicket = new ServiceImpl(noOfTickets -> noOfTickets * 200.0);

你確實在這里定義了一個策略。 但是您應該定義一個更能代表業務的通用接口,例如:

public interface TicketStrategy {
    Double fare(final Integer quantity);
}

如果需要,可以將其設為 FunctionalInterface:

@FunctionalInterface
public interface TicketStrategy {
    Double fare(final Integer quantity);
}

然后你像你一樣實現:

public class TicketStrategies {

    public static final TicketStrategy VIP = noOfTickets -> noOfTickets * 400.00;
    public static final TicketStrategy RECLINER = noOfTickets-> noOfTickets * 200.00;
}

然后你的主要看起來像這樣:

public static void main(String[] args) {
    ITicket ticketsVip = new ServiceImpl(RECLINER));
    System.out.println(ticketsVip.calculateFare(5));
}

您可以做的是將它與這樣的工廠模式一起使用:

public class TicketStrategyFactory {

    private static final Map<String, TicketStrategy> STRATEGIES;

    static {
        final HashMap<String, TicketStrategy> strategies = new HashMap<>();
        strategies.put("vip", TicketStrategies.VIP);
        strategies.put("recliner", TicketStrategies.RECLINER);
        STRATEGIES = Collections.unmodifiableMap(strategies);
    }

    public TicketStrategyFactory() {

    }

    public TicketStrategy create(String type) {
        TicketStrategy strategy = STRATEGIES.get(type);
        if (strategy == null) {
            throw new IllegalArgumentException("Unknown strategy " + type);
        }
        return strategy;
    }
}

public static void main(String[] args) {
    TicketStrategyFactory factory = new TicketStrategyFactory();
    ITicket ticketsVip = new ServiceImpl(factory.create("vip"));
    System.out.println(ticketsVip.calculateFare(5));
}

您可以通過像這樣利用 function 組合進一步 go :

public class TicketStrategies {

    public static final TicketStrategy RECLINER = noOfTickets -> noOfTickets * 200.00;
    public static final TicketStrategy VIP = noOfTickets -> RECLINER.fare(noOfTickets) * 200;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM