👉 這是一個或許對你有用的社群
《專案實戰(影片)》:從書中學,往事上“練” 《網際網路高頻面試題》:面朝簡歷學習,春暖花開 《架構 x 系統設計》:摧枯拉朽,掌控面試高頻場景題 《精進 Java 學習指南》:系統學習,網際網路主流技術棧 《必讀 Java 原始碼專欄》:知其然,知其所以然

👉這是一個或許對你有用的開源專案國產 Star 破 10w+ 的開源專案,前端包括管理後臺 + 微信小程式,後端支援單體和微服務架構。功能涵蓋 RBAC 許可權、SaaS 多租戶、資料許可權、商城、支付、工作流、大屏報表、微信公眾號、ERP、CRM、AI 大模型等等功能:
Boot 多模組架構:https://gitee.com/zhijiantianya/ruoyi-vue-pro Cloud 微服務架構:https://gitee.com/zhijiantianya/yudao-cloud 影片教程:https://doc.iocoder.cn 【國內首批】支援 JDK 17/21 + SpringBoot 3.3、JDK 8/11 + Spring Boot 2.7 雙版本

前言
基於 Spring Boot + MyBatis Plus + Vue & Element 實現的後臺管理系統 + 使用者小程式,支援 RBAC 動態許可權、多租戶、資料許可權、工作流、三方登入、支付、簡訊、商城等功能
專案地址:https://github.com/YunaiV/ruoyi-vue-pro 影片教程:https://doc.iocoder.cn/video/
1. 單例模式
publicclassSingleton
{
privatestaticvolatile
Singleton instance;
privateSingleton()
{}
publicstatic Singleton getInstance()
{
if
(instance ==
null
) {
synchronized
(Singleton
.class)
{
if
(instance ==
null
) {
instance =
new
Singleton();
}
}
}
return
instance;
}
}
-
java.lang.Runtime.getRuntime()
-
java.util.logging.Logger
Bean
預設是單例模式。可以透過 @Scope("prototype")
將其改為多例。基於 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現的後臺管理系統 + 使用者小程式,支援 RBAC 動態許可權、多租戶、資料許可權、工作流、三方登入、支付、簡訊、商城等功能
專案地址:https://github.com/YunaiV/yudao-cloud 影片教程:https://doc.iocoder.cn/video/
2. 工廠模式
publicclassPaymentFactory
{
publicstatic Payment createPayment(String type)
{
switch
(type) {
case"AliPay"
:
returnnew
AliPay();
case"WeChatPay"
:
returnnew
WeChatPay();
default
:
thrownew
IllegalArgumentException(
"Unknown payment type"
);
}
}
}
-
java.util.Calendar.getInstance()
-
javax.xml.parsers.DocumentBuilderFactory.newInstance()
-
BeanFactory
和ApplicationContext
都是工廠模式的體現。
3. 策略模式
publicinterfacePromotionStrategy
{
voidapplyPromotion()
;
}
publicclassDiscountStrategyimplementsPromotionStrategy
{
@Override
publicvoidapplyPromotion()
{
System.out.println(
"Applying discount..."
);
}
}
publicclassPromotionContext
{
private
PromotionStrategy strategy;
publicPromotionContext(PromotionStrategy strategy)
{
this
.strategy = strategy;
}
publicvoidexecutePromotion()
{
strategy.applyPromotion();
}
}
-
java.util.Comparator
是典型的策略模式。
-
事務管理( TransactionManager
),支援程式設計式和宣告式事務。
4. 代理模式
publicinterfaceService
{
voidexecute()
;
}
publicclassRealServiceimplementsService
{
@Override
publicvoidexecute()
{
System.out.println(
"Executing real service..."
);
}
}
publicclassServiceProxyimplementsService
{
private
RealService realService;
@Override
publicvoidexecute()
{
System.out.println(
"Checking permissions..."
);
if
(realService ==
null
) {
realService =
new
RealService();
}
realService.execute();
}
}
-
動態代理 java.lang.reflect.Proxy
-
RMI(遠端方法呼叫)
-
AOP(面向切面程式設計)廣泛使用代理模式。
5. 觀察者模式
publicinterfaceObserver
{
voidupdate(String message)
;
}
publicclassUserimplementsObserver
{
private
String name;
publicUser(String name)
{
this
.name = name;
}
@Override
publicvoidupdate(String message)
{
System.out.println(name +
" received message: "
+ message);
}
}
publicclassWeibo
{
private
List<Observer> observers =
new
ArrayList<>();
publicvoidfollow(Observer observer)
{
observers.add(observer);
}
publicvoidpost(String message)
{
for
(Observer observer : observers) {
observer.update(message);
}
}
}
-
java.util.Observer
和java.util.Observable
-
javax.swing.event.ChangeListener
-
ApplicationEvent
和ApplicationListener
是典型實現。
6. 裝飾器模式
publicinterfaceCoffee
{
String getDescription()
;
doublegetCost()
;
}
publicclassSimpleCoffeeimplementsCoffee
{
@Override
public String getDescription()
{
return"Simple Coffee"
;
}
@Override
publicdoublegetCost()
{
return5.0
;
}
}
publicclassMilkDecoratorimplementsCoffee
{
private
Coffee coffee;
publicMilkDecorator(Coffee coffee)
{
this
.coffee = coffee;
}
@Override
public String getDescription()
{
return
coffee.getDescription() +
", Milk"
;
}
@Override
publicdoublegetCost()
{
return
coffee.getCost() +
1.5
;
}
}
-
java.io.BufferedInputStream
和java.io.BufferedOutputStream
-
BeanPostProcessor
用於動態修改 Bean 的行為。
7. 模板方法模式
publicabstractclassTask
{
publicfinalvoidexecute()
{
init();
doWork();
cleanup();
}
protectedabstractvoidinit()
;
protectedabstractvoiddoWork()
;
protectedvoidcleanup()
{
System.out.println(
"Default cleanup..."
);
}
}
publicclassDataProcessingTaskextendsTask
{
@Override
protectedvoidinit()
{
System.out.println(
"Initializing data..."
);
}
@Override
protectedvoiddoWork()
{
System.out.println(
"Processing data..."
);
}
}
-
java.util.AbstractList
和java.util.AbstractMap
-
JdbcTemplate
和RestTemplate
8. 建造者模式
publicclassHttpRequest
{
private
String method;
private
String url;
private
String body;
privateHttpRequest(Builder builder)
{
this
.method = builder.method;
this
.url = builder.url;
this
.body = builder.body;
}
publicstaticclassBuilder
{
private
String method;
private
String url;
private
String body;
public Builder method(String method)
{
this
.method = method;
returnthis
;
}
public Builder url(String url)
{
this
.url = url;
returnthis
;
}
public Builder body(String body)
{
this
.body = body;
returnthis
;
}
public HttpRequest build()
{
returnnew
HttpRequest(
this
);
}
}
}
-
StringBuilder
-
Stream.Builder
-
UriComponentsBuilder
用於構建 URI。
總結





