簡體   English   中英

這是策略模式還是回調?

[英]Is this a strategy pattern or just a callback?

今天,我正在閱讀有關SOLID's Open/Closed Principle ,我記得的第一個示例是Android支持庫中的ViewDragHelper類。

這是課程的詳細信息:

// allowing a user to drag and reposition views
public class ViewDragHelper {
    private final Callback mCallback;

    public static ViewDragHelper create(..., Callback cb)

    public abstract static class Callback {
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { }

        public int getViewHorizontalDragRange(View child) {
            return 0;
        }

        public abstract boolean tryCaptureView(View child, int pointerId);

        public int clampViewPositionHorizontal(View child, int left, int dx) {
            return 0;
        }
    }
}

我試圖弄清楚這是strategy pattern一種實現。 實際上看起來是這樣。 ContextViewDragHelper類)和Strategy抽象( Callback類)。 但是有兩點:

  • 該策略的具體實施委托給圖書館的最終用戶。
  • 策略實現的行為會影響Context (您可以在tryCaptureView方法中tryCaptureView視圖位置或禁止拖動操作),而在Strategy pattern描述中, Strategy似乎對Context沒有任何影響(即僅產生或使用某些數據) 。

這是一種Strategy還是某種其他模式,或者僅僅是諸如Callback之類的通用概念的實現?

這是一種Strategy還是某種其他模式,或者僅僅是諸如Callback之類的通用概念的實現?

不,它不是經典定義中的策略模式,而是策略和觀察者模式的組合。 ViewDragHelper的行為隨着CallbackgetViewHorizontalDragRange()clampViewPositionHorizontal()實現( 策略模式 )而改變。 ViewDragHelper通知Callback的有關實例ViewDragHelper通過當前狀態' onViewPositionChanged()tryCaptureView()實現(觀察者模式)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM