代码之家  ›  专栏  ›  技术社区  ›  Maxim

这是策略模式还是回调?

  •  1
  • Maxim  · 技术社区  · 8 年前

    我在读关于 SOLID's Open/Closed Principle 今天我记得的第一个例子是 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 . 实际上好像是的。这就是 Context ( 视图绘图工具 Strategy 抽象( Callback 类)。但有两点:

    • 战略实施的行为影响战略的实施 上下文 tryCaptureView 方法),在 Strategy pattern 说明a 上下文

    战略 或者其他一些模式或者只是实现这样一个共同的概念,比如 回拨 ?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Onik    8 年前

    这是一个 Strategy 或者其他一些模式或者只是实现这样一个共同的概念,比如 Callback

    不,这不是它的战略模式 classical definition 它是战略与技术的有机结合 Observer Pattern ViewDragHelper 的行为随时间而变化 回拨 的 getViewHorizontalDragRange() clampViewPositionHorizontal() ). 以及 通知 回拨 视图绘图工具 的当前状态 onViewPositionChanged() tryCaptureView()