訪問(wèn)者模式的實(shí)現(xiàn)
訪問(wèn)者模式就是針對(duì)不同的資源設(shè)置不同的訪問(wèn)權(quán)限, 反轉(zhuǎn)這訪問(wèn)權(quán)限的設(shè)置位置,從而達(dá)到不修改資源來(lái)控制訪問(wèn)權(quán)限的目的.
- 先設(shè)置一個(gè)元素材資源和元訪問(wèn)權(quán)限
public class unionLevel {
public String getLevelName(unionVisitor visitor){
return "see union level";
};
}
public interface unionVisitor {
/**
* 看第一級(jí)素材
* @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";
}
}
- 設(shè)置多級(jí)素材繼承元素材
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";
}
}
- 設(shè)置多級(jí)權(quán)限實(shí)現(xiàn)元權(quán)限
public class VisitorOne implements unionVisitor{
/**
* 看第一級(jí)素材
*
* @return
*/
@Override
public String seeLevelOne(unionLevel level) {
return "VisitorOne can see "+level;
}
}
- 寫個(gè)測(cè)試類(其他元素和素材照著上面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());
}
}
總結(jié)
平常不怎么喜歡寫總結(jié)的闪湾,但是說(shuō)要是使用的時(shí)候還是會(huì)去翻一下他的定義予权。以免自己弄錯(cuò)了都不知道,其實(shí)對(duì)于訪問(wèn)者模式來(lái)說(shuō)怜姿,最大的好處就是對(duì)權(quán)限這邊的解放(不過(guò)你要是資源級(jí)別會(huì)隨意變動(dòng)而權(quán)限設(shè)置不會(huì)隨便變動(dòng)的話春感,可以將這個(gè)設(shè)計(jì)反過(guò)來(lái)砌创。畢竟設(shè)計(jì)是死的而人是活的■昀粒肯定要寫成對(duì)實(shí)現(xiàn)更加方便的代碼出來(lái))
訪問(wèn)者模式
是23種基本設(shè)計(jì)模式中的一種彻采,屬于行為型設(shè)計(jì)模式枪孩。維基百科定義: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.(表示要在對(duì)象結(jié)構(gòu)的元素上執(zhí)行的操作。訪問(wèn)者可讓您定義新操作,而無(wú)需更改其所操作元素的類)
適用范圍
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
在以下情況下使用訪問(wèn)者模式 * 一個(gè)對(duì)象結(jié)構(gòu)包含許多具有不同接口的對(duì)象類醒第,并且您希望根據(jù)這些對(duì)象的具體類對(duì)這些對(duì)象執(zhí)行操作 * 需要對(duì)對(duì)象結(jié)構(gòu)中的對(duì)象執(zhí)行許多不同且不相關(guān)的操作,并且您要避免使用這些操作“污染”它們的類伯复。訪客可以通過(guò)在一個(gè)類中定義相關(guān)操作來(lái)將它們保持在一起搞莺。 * 當(dāng)許多應(yīng)用程序共享對(duì)象結(jié)構(gòu)時(shí),請(qǐng)使用Visitor將操作僅放在需要它們的應(yīng)用程序中 定義對(duì)象結(jié)構(gòu)的類很少更改疚鲤,但是您經(jīng)常想在該結(jié)構(gòu)上定義新的操作锥累。更改對(duì)象結(jié)構(gòu)類需要重新定義所有訪問(wèn)者的接口,這可能會(huì)導(dǎo)致成本高昂集歇。如果對(duì)象結(jié)構(gòu)類經(jīng)常更改桶略,那么最好在這些類中定義操作