-
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Revise 1.1 #3605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AnakinYen0329
wants to merge
12
commits into
iluwatar:master
Choose a base branch
from
AnakinYen0329:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Revise 1.1 #3605
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e5f82f8
修改完成
AnakinYen0329 91b7826
Add Singleton pattern example
AnakinYen0329 0ba0213
Merge branch 'iluwatar:master' into master
AnakinYen0329 52508ab
修改1.1
AnakinYen0329 f0de978
translation
AnakinYen0329 fea0ddb
ci: remove LLM_MODEL from presubmit workflow to avoid ai-reviewer sch…
AnakinYen0329 ddc6c19
ci: pin presubmit/ai-reviewer to v0.2.5 to avoid breaking changes fro…
AnakinYen0329 eba6b92
fix: stabilize presubmit AI reviewer workflow
AnakinYen0329 a16cba6
Merge origin/master into master
AnakinYen0329 3bddf4e
fix: refine zh-TW localization wording
AnakinYen0329 8117fcc
docs: add ten zh-TW pattern translations
AnakinYen0329 1a4d224
docs: add five zh-TW pattern translations
AnakinYen0329 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| --- | ||
| title: Acyclic Visitor | ||
| shortTitle: Acyclic Visitor | ||
| category: Behavioral | ||
| language: zh | ||
| tag: | ||
| - Extensibility | ||
| --- | ||
|
|
||
| ## 目的 | ||
|
|
||
| 允許將新功能新增到現有的類層次結構中,而不會影響這些層次結構,也不會有四人幫訪客模式中那樣迴圈依賴的問題。 | ||
|
|
||
| ## 解釋 | ||
|
|
||
| 真實世界例子 | ||
|
|
||
| > 我們有一個調變解調器類的層次結構。 需要使用基於過濾條件的外部演算法(是Unix或DOS相容的調變解調器)來訪問此層次結構中的調變解調器。 | ||
|
|
||
| 通俗地說 | ||
|
|
||
| > 非迴圈訪問者允許將功能新增到現有的類層次結構中,而無需修改層次結構 | ||
|
|
||
| [WikiWikiWeb](https://wiki.c2.com/?AcyclicVisitor) 上說 | ||
|
|
||
| > 非迴圈訪客模式允許將新功能新增到現有的類層次結構中,而不會影響這些層次結構,也不會建立四人幫訪客模式中固有的迴圈依賴問題。 | ||
|
|
||
| **程式示例** | ||
|
|
||
| 這是調變解調器的層次結構。 | ||
|
|
||
| ```java | ||
| public abstract class Modem { | ||
| public abstract void accept(ModemVisitor modemVisitor); | ||
| } | ||
|
|
||
| public class Zoom extends Modem { | ||
| ... | ||
| @Override | ||
| public void accept(ModemVisitor modemVisitor) { | ||
| if (modemVisitor instanceof ZoomVisitor) { | ||
| ((ZoomVisitor) modemVisitor).visit(this); | ||
| } else { | ||
| LOGGER.info("Only ZoomVisitor is allowed to visit Zoom modem"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class Hayes extends Modem { | ||
| ... | ||
| @Override | ||
| public void accept(ModemVisitor modemVisitor) { | ||
| if (modemVisitor instanceof HayesVisitor) { | ||
| ((HayesVisitor) modemVisitor).visit(this); | ||
| } else { | ||
| LOGGER.info("Only HayesVisitor is allowed to visit Hayes modem"); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 下面我們介紹`調變解調器訪問者`類結構。 | ||
|
|
||
| ```java | ||
| public interface ModemVisitor { | ||
| } | ||
|
|
||
| public interface HayesVisitor extends ModemVisitor { | ||
| void visit(Hayes hayes); | ||
| } | ||
|
|
||
| public interface ZoomVisitor extends ModemVisitor { | ||
| void visit(Zoom zoom); | ||
| } | ||
|
|
||
| public interface AllModemVisitor extends ZoomVisitor, HayesVisitor { | ||
| } | ||
|
|
||
| public class ConfigureForDosVisitor implements AllModemVisitor { | ||
| ... | ||
| @Override | ||
| public void visit(Hayes hayes) { | ||
| LOGGER.info(hayes + " used with Dos configurator."); | ||
| } | ||
| @Override | ||
| public void visit(Zoom zoom) { | ||
| LOGGER.info(zoom + " used with Dos configurator."); | ||
| } | ||
| } | ||
|
|
||
| public class ConfigureForUnixVisitor implements ZoomVisitor { | ||
| ... | ||
| @Override | ||
| public void visit(Zoom zoom) { | ||
| LOGGER.info(zoom + " used with Unix configurator."); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 最後,這裡是訪問者的實踐。 | ||
|
|
||
| ```java | ||
| var conUnix = new ConfigureForUnixVisitor(); | ||
| var conDos = new ConfigureForDosVisitor(); | ||
| var zoom = new Zoom(); | ||
| var hayes = new Hayes(); | ||
| hayes.accept(conDos); | ||
| zoom.accept(conDos); | ||
| hayes.accept(conUnix); | ||
| zoom.accept(conUnix); | ||
| ``` | ||
|
|
||
| 程式輸出: | ||
|
|
||
| ``` | ||
| // Hayes modem used with Dos configurator. | ||
| // Zoom modem used with Dos configurator. | ||
| // Only HayesVisitor is allowed to visit Hayes modem | ||
| // Zoom modem used with Unix configurator. | ||
| ``` | ||
|
|
||
| ## 類圖 | ||
|
|
||
|  | ||
|
|
||
| ## 適用性 | ||
|
|
||
| 以下情況可以使用此模式: | ||
|
|
||
| * 需要在現有層次結構中新增新功能而無需更改或影響該層次結構時。 | ||
| * 當某些功能在層次結構上執行,但不屬於層次結構本身時。 例如 ConfigureForDOS / ConfigureForUnix / ConfigureForX問題。 | ||
| * 當你需要根據物件的型別對物件執行非常不同的操作時。 | ||
| * 當訪問的類層次結構將經常使用元素類的新派生進行擴充套件時。 | ||
| * 當重新編譯,重新連結,重新測試或重新分發派生元素非常昂貴時。 | ||
|
|
||
| ## 結果 | ||
|
|
||
| 好處: | ||
|
|
||
| * 類層次結構之間沒有依賴關係迴圈。 | ||
| * 如果新增了新訪客,則無需重新編譯所有訪客。 | ||
| * 如果類層次結構具有新成員,則不會導致現有訪問者中的編譯失敗。 | ||
|
|
||
| 壞處: | ||
|
|
||
| * 透過證明它可以接受所有訪客,但實際上僅對特定訪客感興趣,從而違反了[Liskov的替代原則](https://java-design-patterns.com/principles/#liskov-substitution-principle) | ||
| * 必須為可訪問的類層次結構中的所有成員建立訪問者的並行層次結構。 | ||
|
|
||
| ## 相關的模式 | ||
|
|
||
| * [Visitor Pattern](https://java-design-patterns.com/patterns/visitor/) | ||
|
|
||
| ## 鳴謝 | ||
|
|
||
| * [Acyclic Visitor by Robert C. Martin](http://condor.depaul.edu/dmumaugh/OOT/Design-Principles/acv.pdf) | ||
| * [Acyclic Visitor in WikiWikiWeb](https://wiki.c2.com/?AcyclicVisitor) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| --- | ||
| title: Adapter | ||
| shortTitle: Adapter | ||
| category: Structural | ||
| language: zh | ||
| tag: | ||
| - Gang of Four | ||
| --- | ||
|
|
||
| ## 又被稱為 | ||
| 包裝器 | ||
|
|
||
| ## 目的 | ||
| 將一個介面轉換成另一個客戶所期望的介面。介面卡讓那些本來因為介面不相容的類可以合作無間。 | ||
|
|
||
| ## 解釋 | ||
|
|
||
| 現實世界例子 | ||
|
|
||
| > 考慮有這麼一種情況,在你的儲存卡中有一些照片,你想將其傳到你的電腦中。為了傳送資料,你需要某種能夠相容你電腦介面的介面卡以便你的儲存卡能連上你的電腦。在這種情況下,讀卡器就是一個介面卡。 | ||
| > 另一個例子就是註明的電源介面卡;三腳插頭不能插在兩腳插座上,需要一個電源介面卡來使其能夠插在兩腳插座上。 | ||
| > 還有一個例子就是翻譯官,他翻譯一個人對另一個人說的話。 | ||
|
|
||
| 用直白的話來說 | ||
|
|
||
| > 介面卡模式讓你可以把不相容的物件包在介面卡中,以讓其相容其他類。 | ||
|
|
||
| 維基百科中說 | ||
|
|
||
| > 在軟體工程中,介面卡模式是一種可以讓現有類的介面把其作為其他介面來使用的設計模式。它經常用來使現有的類和其他類能夠工作並且不用修改其他類的原始碼。 | ||
|
|
||
| **程式設計樣例(物件介面卡)** | ||
|
|
||
| 假如有一個船長他只會划船,但不會航行。 | ||
|
|
||
| 首先我們有介面`RowingBoat`和`FishingBoat` | ||
|
|
||
| ```java | ||
| public interface RowingBoat { | ||
| void row(); | ||
| } | ||
|
|
||
| @Slf4j | ||
| public class FishingBoat { | ||
| public void sail() { | ||
| LOGGER.info("The fishing boat is sailing"); | ||
| } | ||
| } | ||
| ``` | ||
| 船長希望有一個`RowingBoat`介面的實現,這樣就可以移動 | ||
|
|
||
| ```java | ||
| public class Captain { | ||
|
|
||
| private final RowingBoat rowingBoat; | ||
| // default constructor and setter for rowingBoat | ||
| public Captain(RowingBoat rowingBoat) { | ||
| this.rowingBoat = rowingBoat; | ||
| } | ||
|
|
||
| public void row() { | ||
| rowingBoat.row(); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 現在海盜來了,我們的船長需要逃跑但是隻有一個漁船可用。我們需要建立一個可以讓船長使用其划船技能來操作漁船的介面卡。 | ||
|
|
||
| ```java | ||
| @Slf4j | ||
| public class FishingBoatAdapter implements RowingBoat { | ||
|
|
||
| private final FishingBoat boat; | ||
|
|
||
| public FishingBoatAdapter() { | ||
| boat = new FishingBoat(); | ||
| } | ||
|
|
||
| @Override | ||
| public void row() { | ||
| boat.sail(); | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| 現在 `船長` 可以使用`FishingBoat`介面來逃離海盜了。 | ||
|
|
||
| ```java | ||
| var captain = new Captain(new FishingBoatAdapter()); | ||
| captain.row(); | ||
| ``` | ||
|
|
||
| ## 類圖 | ||
|  | ||
|
|
||
|
|
||
| ## 應用 | ||
| 使用介面卡模式當 | ||
|
|
||
| * 你想使用一個已有類,但是它的介面不能和你需要的所匹配 | ||
| * 你需要建立一個可重用類,該類與不相關或不可預見的類進行協作,即不一定具有相容介面的類 | ||
| * 你需要使用一些現有的子類,但是子類化他們每一個的子類來進行介面的適配是不現實的。一個物件介面卡可以適配他們父類的介面。 | ||
| * 大多數使用第三方類庫的應用使用介面卡作為一個在應用和第三方類庫間的中間層來使應用和類庫解耦。如果必須使用另一個庫,則只需使用一個新庫的介面卡而無需改變應用程式的程式碼。 | ||
|
|
||
| ## 後果: | ||
| 類和物件介面卡有不同的權衡取捨。一個類介面卡 | ||
|
|
||
| * 適配被適配者到目標介面,需要保證只有一個具體的被適配者類。作為結果,當我們想適配一個類和它所有的子類時,類介面卡將不會起作用。 | ||
| * 可以讓介面卡重寫一些被適配者的行為,因為介面卡是被適配者的子類。 | ||
| * 只引入了一個物件,並且不需要其他指標間接訪問被適配者。 | ||
|
|
||
| 物件介面卡 | ||
|
|
||
| * 一個介面卡可以和許多被適配者工作,也就是被適配者自己和所有它的子類。介面卡同時可以為所有被適配者新增功能。 | ||
| * 覆蓋被適配者的行為變得更難。需要子類化被適配者然後讓介面卡引用這個子類不是被適配者。 | ||
|
|
||
|
|
||
| ## 現實世界的案例 | ||
|
|
||
| * [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29) | ||
| * [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-) | ||
| * [java.util.Collections#enumeration()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#enumeration-java.util.Collection-) | ||
| * [javax.xml.bind.annotation.adapters.XMLAdapter](http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html#marshal-BoundType-) | ||
|
|
||
|
|
||
| ## 鳴謝 | ||
|
|
||
| * [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) | ||
| * [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31) | ||
| * [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) | ||
| * [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7) | ||
|
|
||
| ``` | ||
|
|
||
| ``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider YAML indentation and structure for the new presubmit step. The new action uses presubmit/ai-reviewer@v0.2.5; ensure the 'env:' mapping is correctly indented under the step (two-space indentation under the 'uses' line). Mis-indentation could break the workflow.