1. 程式人生 > >設計模式之訪問者模式

設計模式之訪問者模式

訪問者模式的實現

訪問者模式就是針對不同的資源設定不同的訪問許可權, 反轉這訪問許可權的設定位置,從而達到不修改資源來控制訪問許可權的目的.

  • 先設定一個元素材資源和元訪問許可權

public class unionLevel {


    public String getLevelName(unionVisitor visitor){
        return "see union level";
    };

}

public interface unionVisitor {
    /**
     * 看第一級素材
     * @return
     */
    default String seeLevelOne(unionLevel level){
        return level+"  forbidden";
    }
    /**
     * level two
     * @return
     */
    default String seeLevelTwo(unionLevel level){
        return level+" forbidden";
    }
    /**
     * level three
     * @return
     */
    default String seeLevelThree(unionLevel level)
    {
        return level+" forbidden";
    }
}
  • 設定多級素材繼承元素材
public class LevelOne extends unionLevel{

    @Override
    public String getLevelName(unionVisitor visitor) {
        System.out.println(visitor.seeLevelOne(this));
        return super.getLevelName(visitor);
    }

    @Override
    public String toString() {
        return "levelone";
    }
}
  • 設定多級許可權實現元許可權
public class VisitorOne implements unionVisitor{

    /**
     * 看第一級素材
     *
     * @return
     */
    @Override
    public String seeLevelOne(unionLevel level) {
        return "VisitorOne can see "+level;
    }
}
  • 寫個測試類(其他元素和素材照著上面demo寫就行)
public class start {

    public static void main(String[] args) {

        LevelTwo two  =  new LevelTwo();
        two.getLevelName(new VisitorOne());
        two.getLevelName(new VisitorTwo());
        two.getLevelName(new VisitorThree());
    }
}

總結

平常不怎麼喜歡寫總結的,但是說要是使用的時候還是會去翻一下他的定義。以免自己弄錯了都不知道,其實對於訪問者模式來說,最大的好處就是對許可權這邊的解放(不過你要是資源級別會隨意變動而許可權設定不會隨便變動的話,可以將這個設計反過來。畢竟設計是死的而人是活的。肯定要寫成對實現更加方便的程式碼出來)

訪問者模式

是23種基本設計模式中的一種,屬於行為型設計模式。維基百科定義:Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.(表示要在物件結構的元素上執行的操作。訪問者可讓您定義新操作,而無需更改其所操作元素的類)

適用範圍

Use the Visitor pattern when

  • an object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes
  • many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them
  • the classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes

              在以下情況下使用訪問者模式 
              * 一個物件結構包含許多具有不同介面的物件類,並且您希望根據這些物件的具體類對這些物件執行操作 
              * 需要對物件結構中的物件執行許多不同且不相關的操作,並且您要避免使用這些操作“汙染”它們的類。訪客可以通過在一個類中定義相關操作來將它們保持在一起。
              * 當許多應用程式共享物件結構時,請使用Visitor將操作僅放在需要它們的應用程式中 定義物件結構的類很少更改,但是您經常想在該結構上定義新的操作。更改物件結構類需要重新定義所有訪問者的介面,這可能會導致成本高昂。如果物件結構類經常更改,那麼最好在這些類中定義操作

https://github.com/fulln