c# – 这是一个语义违反封装的例子,如果是这样,我该如何解决?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 这是一个语义违反封装的例子,如果是这样,我该如何解决?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1494字,纯文字阅读大概需要3分钟。
内容图文
![c# – 这是一个语义违反封装的例子,如果是这样,我该如何解决?](/upload/InfoBanner/zyjiaocheng/764/31db684f320946f9a02ed9778786e42a.jpg)
如果我有像这样的服务器接口…
interface IServer
{
void Login();
void Post();
void Get();
}
…除非你先完成登录,否则Post和Get不起作用.它是否是对封装的语义违反,因为它使得您对接口的使用隐含地依赖于实现?你会怎么解决它?
解决方法:
我的想法:
interface IServer
{
ISession Authenticate();
}
interface ISession
{
IServer Server{get;}
void Post();
void Get();
}
如果您可以接触“泥”,请忽略它:
[泥]
to clarify this: imho you have to think about software design as you
would create a product… do you really want a safe for all your
valueable things where you can push a button “open” instead of FIRST
input the code on it? this is just an analogy for your use-case…an implementator can just call post and get before he validates…
another widely used approach are access-tokens and the use of it like
here:’interface IServer{ // returns the required token string
Authenticate(); void Post(string token); void Get(string token);
}but i think, that’s wrong too…
you can see and maybe call methods, which you are not allowed to
see/call before login… if the implementator just not checks if
“token” is correct, you have a security risk in here…if you divide the logic into several layers (guest/unauthenticated,
authenticated/session, and (for example) adminsession) you get clean
separation of logic and a lot more semantic usefullness….I PERSONALLY write code EVER in way, that good framework-builders would write code… it has the be reuseable, even after years, very
easy and clear.
[/泥]
内容总结
以上是互联网集市为您收集整理的c# – 这是一个语义违反封装的例子,如果是这样,我该如何解决?全部内容,希望文章能够帮你解决c# – 这是一个语义违反封装的例子,如果是这样,我该如何解决?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。