網(wǎng)站怎么弄二維碼服務(wù)營(yíng)銷理論
一、單例模式(Singleton Pattern)
-
定義:確保一個(gè)類只有一個(gè)實(shí)例,并提供全局訪問(wèn)點(diǎn)
-
Spring中的應(yīng)用:Spring默認(rèn)將Bean配置為單例模式
-
案例:
@Component public class MySingletonBean {// Spring 默認(rèn)將其管理為單例 }
-
在spring容器中,MySingletonBean只會(huì)有一個(gè)實(shí)例
二、工廠模式(Factory Pattern)
-
定義:定義一個(gè)創(chuàng)建對(duì)象的接口,由子類決定實(shí)例化哪個(gè)類
-
spring中的應(yīng)用:BeanFactory和ApplicationContext是工廠模式的實(shí)現(xiàn)
-
案例:
@Configuration public class AppConfig {@Beanpublic MyBean myBean() {return new MyBean();} }
-
AppConfig是一個(gè)工廠類,myBean()方法負(fù)責(zé)創(chuàng)建MyBean實(shí)例
三、原型模式(Prototype Pattern)
-
定義:通過(guò)復(fù)制現(xiàn)有對(duì)象來(lái)創(chuàng)建新對(duì)象
-
Spring中的應(yīng)用:通過(guò)@Scope("prototype")配置Bean為原型模式
-
案例
@Component @Scope("prototype") public class MyPrototypeBean {// 每次獲取時(shí)都會(huì)創(chuàng)建一個(gè)新實(shí)例 }
四、模板方法模式(Template Method Pattern)
-
定義:定義一個(gè)算法的骨架,將某些步驟延遲到子類中實(shí)現(xiàn)
-
Spring中的應(yīng)用:JdbcTemplate、RestTemplate等
-
案例:
@Autowired private JdbcTemplate jdbcTemplate; ? public void queryData() {String sql = "SELECT * FROM users";jdbcTemplate.query(sql, (rs, rowNum) -> {System.out.println(rs.getString("username"));return null;}); }
五、適配器模式(Adapter Pattern)
-
定義:將一個(gè)類的接口轉(zhuǎn)換成客戶端期望的另一個(gè)接口
-
spring中的應(yīng)用:Spring MVC中的HandlerAdapter
-
案例:
@Controller public class MyController {@RequestMapping("/hello")public String hello() {return "Hello, World!";} }
六、裝飾者模式(Decorator Pattern)
-
定義:動(dòng)態(tài)地為對(duì)象添加額外的職責(zé)
-
spring中的應(yīng)用:spring AOP中的代理
-
案例:
@Aspect @Component public class LoggingAspect {@Before("execution(* com.example.service.*.*(..))")public void logBefore(JoinPoint joinPoint) {System.out.println("Before method: " + joinPoint.getSignature().getName());} }
-
AOP通過(guò)裝飾者模式為方法添加日志功能
七、觀察者模式(Observer Pattern)
-
定義:定義對(duì)象間的一對(duì)多依賴關(guān)系,當(dāng)一個(gè)對(duì)象狀態(tài)改變時(shí),所有依賴對(duì)象都會(huì)收到通知
-
spring容器中的應(yīng)用:Spring的事件機(jī)制
-
案例:
@Component public class MyEventListener implements ApplicationListener<MyEvent> {@Overridepublic void onApplicationEvent(MyEvent event) {System.out.println("Event received: " + event.getMessage());} } ? @Component public class MyEventPublisher {@Autowiredprivate ApplicationEventPublisher publisher; ?public void publishEvent(String message) {publisher.publishEvent(new MyEvent(this, message));} }
-
MyEventPublisher發(fā)布事件,MyEventListener監(jiān)聽(tīng)并處理
八、代理模式(Proxy Pattern)
-
定義:為其他對(duì)象提供一個(gè)代理以控制對(duì)這個(gè)對(duì)象的訪問(wèn)
-
Spring中的應(yīng)用:SpringAOP和動(dòng)態(tài)代理
-
案例:
@Service public class MyService {public void doSomething() {System.out.println("Doing something...");} } ? @Aspect @Component public class MyAspect {@Around("execution(* com.example.service.MyService.*(..))")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("Before method");Object result = joinPoint.proceed();System.out.println("After method");return result;} }
-
AOP通過(guò)代理模式為MyService的方法添加額外邏輯
九、組合模式(Composite Pattern)
-
定義:將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示"部分-整體"的層次結(jié)構(gòu)
-
spring中的應(yīng)用:Spring Security的過(guò)濾器鏈
-
案例:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public/**").permitAll().antMatchers("/private/**").authenticated();} }
-
Spring Security將多個(gè)過(guò)濾器組合成一個(gè)過(guò)濾器鏈
十、策略模式(Strategy Pattern)
-
定義:定義一系列算法,將它們封裝起來(lái),并使它們可以互相替換
-
Spring中的應(yīng)用:Spring的資源加載策略(ResourceLoader)
-
案例:
@Autowired private ResourceLoader resourceLoader; ? public void loadResource() {Resource resource = resourceLoader.getResource("classpath:data.txt");System.out.println("Resource loaded: " + resource.exists()); }
-
ResourceLoader根據(jù)不同的資源加載策略加載資源(如文件系統(tǒng)、類路徑等)。