I have below service implementation :-
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);
}
}
Below are the strategies that I have created :-
Recliner ticket strategy :-
package Java8.controller;
import java.util.function.Function;
public interface ReclinerTicketStrategy {
default Function<Integer,Double> reclinerTicketStrategy(){
return (noOfTickets)->{
return noOfTickets * 200.00;
};
}
}
VIP Ticket strategy :-
package Java8.controller;
import java.util.function.Function;
public interface VipTicketStrategy {
default Function<Integer,Double> vipTicketStrategy(){
return (noOfTickets)->{
return noOfTickets*400.00;
};
}
}
Below is the main class which is using the strategy :-
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();
}
}
My question is whether is this the correct way of housing the strategy function in the interface as a default method? OR are there better ways to have it?
Aucun commentaire:
Enregistrer un commentaire