Getters和Setter是糟糕的OO设计? [英] Getters and Setters are bad OO design?

查看:68
本文介绍了Getters和Setter是糟糕的OO设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Getters和Setters是不好的

简单地阅读上面的文章,我发现getter和setter是糟糕的OO设计,应该避免使用封装和数据隐藏。在创建对象时如何才能避免这种情况,一个模型对象如何考虑到这一点。

Briefly reading over the above article I find that getters and setters are bad OO design and should be avoided as they go against Encapsulation and Data Hiding. As this is the case how can it be avoided when creating objects and how can one model objects to take this into account.

如果需要吸气剂或固定剂,可以使用其他替代方案?

In cases where a getter or setter is required what other alternatives can be used?

谢谢。

推荐答案

设置者本身并不糟糕的OO设计。

Getters or setters by themselves are not bad OO design.

什么是编码实践,包括一个getter和一个setter为每个单个成员自动,无论是否需要getter / setter (加上不应该公开的成员),因为这基本上暴露了对外界违反信息隐藏/抽象的类的实现。有时这是由IDE自动完成的,这意味着这种做法比预期的要广泛得多。

What is bad is coding practice which includes a getter AND a setter for EVERY single member automatically, whether that getter/setter is needed or not (coupled with making members public which should not be public) - because this basically exposes class's implementation to outside world violating the information hiding/abstraction. Sometimes this is done automatically by IDE, which means such practice is significantly more widespread than it's hoped for.

这篇关于Getters和Setter是糟糕的OO设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆