1.什么是工厂模式
创建对象和使用对象的过程分开
2.工厂模式有哪几种
静态工厂
简单工厂
工厂方法
抽象工厂
3.Spring中哪些地方用到了工厂
beanFactory
4.简单工厂代码
链接:https://pan.baidu.com/s/1vnc-88f3JObElf2mX0wVjw
提取码:33fx
1)pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
2)接口
public interface PaymentService {
void pay();
}
3)实现
import com.demo.service.PaymentService;
import org.springframework.stereotype.Service;
@Service
public class AlipayServiceImpl implements PaymentService {
@Override
public void pay() {
System.out.println("支付宝支付");
}
}
import com.demo.service.PaymentService;
import org.springframework.stereotype.Service;
@Service
public class WxPayServiceImpl implements PaymentService {
@Override
public void pay() {
System.out.println("微信支付");
}
}
4)简单工厂
import com.demo.service.PaymentService;
import com.demo.utils.SpringUtils;
import org.springframework.stereotype.Component;
@Component
public class PaymentFactory {
public PaymentService getPaymentService(String payType) {
return (PaymentService)SpringUtils.getBean(payType);
}
}
5)工具类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
6)控制层
import com.demo.factory.PaymentFactory;
import com.demo.service.PaymentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class PaymentController {
@Resource
private PaymentFactory paymentFactory;
@GetMapping("pay")
public void pay(String payType) {
PaymentService paymentService = paymentFactory.getPaymentService(payType);
paymentService.pay();
}
}
7)启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppFactory {
public static void main(String[] args) {
SpringApplication.run(AppFactory.class);
}
}