戰略模式的現實世界范例我一直在讀關于OCP主體以及如何利用戰略模式來實現這一目標。我本打算嘗試向幾個人解釋這一點,但我能想到的唯一例子是根據“Order”的狀態使用不同的驗證類。我在網上讀過幾篇文章,但這些文章通常并不描述使用這種策略的真正原因,比如生成報告/賬單/驗證等等.有沒有任何現實世界的例子,你認為戰略模式是常見的?
3 回答
MYYA
TA貢獻1868條經驗 獲得超4個贊
File file = getFile();
Cipher c = CipherFactory.getCipher( file.size() );
c.performAction();// implementations:interface Cipher {
public void performAction();}class InMemoryCipherStrategy implements Cipher {
public void performAction() {
// load in byte[] ....
}}class SwaptToDiskCipher implements Cipher {
public void performAction() {
// swapt partial results to file.
}}Cipher c = CipherFactory.getCipher( file.size() );
(我甚至不知道密碼是否是正確的詞:P)
添加回答
舉報
0/150
提交
取消
