我正在尝试使用Java8实现下面代码段中的策略设计模式。是否寻找更好的实现方法?
我有以下服务实施:-我正在尝试使用Java8实现下面代码段中的策略设计模式。是否寻找更好的实现方法?,java,design-patterns,java-8,functional-programming,Java,Design Patterns,Java 8,Functional Programming,我有以下服务实施:- package Java8.controller; import java.util.function.Function; public class ServiceImpl implements ITicket { Function<Integer,Double> ticketCal; public ServiceImpl(Function<Integer,Double> ticketCal){ this.ti
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);
}
}
我的问题是,这是否是将策略函数作为默认方法保存在界面中的正确方法?或者有更好的方法吗?您在这里定义了一种策略。但您应该定义一个更能代表业务的通用接口,如:
public interface TicketStrategy {
Double fare(final Integer quantity);
}
如果需要,您可以将其设置为功能界面:
@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));
}
public class TicketStrategies {
public static final TicketStrategy RECLINER = noOfTickets -> noOfTickets * 200.00;
public static final TicketStrategy VIP = noOfTickets -> RECLINER.fare(noOfTickets) * 200;
}
您可以将其与工厂模式一起使用,如下所示:
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));
}
public class TicketStrategies {
public static final TicketStrategy RECLINER = noOfTickets -> noOfTickets * 200.00;
public static final TicketStrategy VIP = noOfTickets -> RECLINER.fare(noOfTickets) * 200;
}
策略应该实现服务所需的抽象。在这种情况下,服务需要一个函数
,因此策略将是
class ReclinerTicketStrategy implements Function<Integer,Double> {
@Override
public Double apply(Integer noOfTickets) {
return noOfTickets * 200.0;
}
}
类ReclinerTicketStrategy实现函数{
@凌驾
公共双应用(整数noOfTickets){
返回方便面*200.0;
}
}
然后将服务实例化为,ITicket RECLINERTICKE=new ServiceImpl(new ReclinerTicketStrategy())代码>
当然,如果不需要重用该策略,那么lambda就足够了
ITicket reclinerTicket=newServiceImpl(noOfTickets->noOfTickets*200.0)代码>它是否违反了OCP?比如说,如果我们想添加另一个策略,那么我们必须修改TicketStrategyFactory并执行类似策略的操作;并且在这门课上增加一个新的策略?把所有的策略放在同一个课堂策略中也是一个好主意吗?既然我们想添加任何新策略,我们就必须修改这个类?这是不是又一次违反了OCP?这是你获得想法的一个例子。但是,是的,你可以很容易地为工厂添加策略!你可以把这些策略放到其他地方!