设计模式之访问者模式
访问者模式的实现
访问者模式就是针对不同的资源设置不同的访问权限, 反转这访问权限的设置位置,从而达到不修改资源来控制访问权限的目的.
- 先设置一个元素材资源和元访问权限
- 设置多级素材继承元素材
- 设置多级权限实现元权限
- 写个测试类(其他元素和素材照着上面demo写就行)
总结
平常不怎么喜欢写总结的,但是说要是使用的时候还是会去翻一下他的定义。以免自己弄错了都不知道,其实对于访问者模式来说,最大的好处就是对权限这边的解放(不过你要是资源级别会随意变动而权限设置不会随便变动的话,可以将这个设计反过来。毕竟设计是死的而人是活的。肯定要写成对实现更加方便的代码出来)
访问者模式
是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
__EOF__

本文链接:https://www.cnblogs.com/wzqshb/p/11795787.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗