面试题答案
一键面试1. 使用Java多态实现服务替换
- 定义接口: 首先定义一个接口,该接口作为不同服务实现的统一契约。
public interface PaymentService {
String processPayment(double amount);
}
- 创建具体实现类: 创建多个实现该接口的具体类,每个类代表一种不同的服务实现。
public class CreditCardPaymentService implements PaymentService {
@Override
public String processPayment(double amount) {
// 处理信用卡支付逻辑
return "Credit card payment of $" + amount + " processed successfully.";
}
}
public class PayPalPaymentService implements PaymentService {
@Override
public String processPayment(double amount) {
// 处理PayPal支付逻辑
return "PayPal payment of $" + amount + " processed successfully.";
}
}
- 利用多态进行服务替换: 在代码中,可以根据需要动态地选择不同的服务实现。
public class OrderService {
private PaymentService paymentService;
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
public String placeOrder(double amount) {
return "Order placed. " + paymentService.processPayment(amount);
}
}
使用时:
public class Main {
public static void main(String[] args) {
// 使用信用卡支付服务
PaymentService creditCardService = new CreditCardPaymentService();
OrderService orderServiceWithCreditCard = new OrderService(creditCardService);
System.out.println(orderServiceWithCreditCard.placeOrder(100.0));
// 使用PayPal支付服务
PaymentService payPalService = new PayPalPaymentService();
OrderService orderServiceWithPayPal = new OrderService(payPalService);
System.out.println(orderServiceWithPayPal.placeOrder(100.0));
}
}
2. 在Spring框架中配置多态的服务替换
- 定义接口和实现类:与上述Java多态示例中的接口和实现类定义相同。
- Spring配置:
- 使用XML配置:
在Spring配置文件(如
applicationContext.xml
)中:
- 使用XML配置:
在Spring配置文件(如
<bean id="creditCardPaymentService" class="com.example.CreditCardPaymentService"/>
<bean id="payPalPaymentService" class="com.example.PayPalPaymentService"/>
<bean id="orderServiceWithCreditCard" class="com.example.OrderService">
<constructor-arg ref="creditCardPaymentService"/>
</bean>
<bean id="orderServiceWithPayPal" class="com.example.OrderService">
<constructor-arg ref="payPalPaymentService"/>
</bean>
- **使用Java配置**:
创建一个配置类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public PaymentService creditCardPaymentService() {
return new CreditCardPaymentService();
}
@Bean
public PaymentService payPalPaymentService() {
return new PayPalPaymentService();
}
@Bean
public OrderService orderServiceWithCreditCard(PaymentService creditCardPaymentService) {
return new OrderService(creditCardPaymentService);
}
@Bean
public OrderService orderServiceWithPayPal(PaymentService payPalPaymentService) {
return new OrderService(payPalPaymentService);
}
}
- 使用依赖注入: 在Spring应用中,可以通过依赖注入获取不同实现的服务。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private OrderService orderServiceWithCreditCard;
@Autowired
private OrderService orderServiceWithPayPal;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(orderServiceWithCreditCard.placeOrder(100.0));
System.out.println(orderServiceWithPayPal.placeOrder(100.0));
}
}
通过上述方式,在Java中利用多态实现服务替换,并在Spring框架中通过配置支持这种替换。