Class attribute declaration: private vs public

2019-04-19 13:04发布

What are the advantages of defining a private attribute instead of a public attribute? Why should I have the extra work of creating methods to access and modify privates attributes if I can just make them public?

9条回答
在下西门庆
2楼-- · 2019-04-19 13:29

In the short term there's none, other than making OOP purists unhappy.

(I'm assuming you mean exposing properties that would otherwise use getters/setters - obviously there's a big difference if you leave ALL your attributes public).

In the long-term there are a few really good reasons for doing it.

Firstly it allows you to validate input at its source instead of later having to back-track the origin with a combination of hardware breakpoints and black-magic.

E.g.

void Foo::setWeight(float weight)
{
  ASSERTMSG(weight >= 0.0f && weight <= 1.0f, "Weights must fall in [0..1]");
  mWeight = weight;
}

It also allows you to later change the behavior of your object without needing to refactor client code.

E.g.

void Foo::setSomething(float thing)
{
  mThing = thing;
  // 2009/4/2: turns out we need to recalc a few things when this changes..
  ...
}
查看更多
不美不萌又怎样
3楼-- · 2019-04-19 13:30

You are better off in the long term if all attributes are private. That includes not using getters. If you are accessing attributes of objects then you are breaking encapsulation, and aren't really doing OOP at all. At that point, most of the reason for building a class has been wasted. Why build a castle to protect from intruders, then leave all the doors open? Or (with getters) always welcome the intruders in on request.

The methodologies of "Law of Demeter" and "Tell don't ask" has been shown to reduce bug rate in production code. Using public attributes won't make object oriented purists unhappy, it'll just ward them off using your code.

Tell your objects what to do with their attributes, don't ask them for the values, manipulate them and write them back. That would be like taking a dog for a walk and then picking up its legs one by one to get it to move.

By the way, conversely, behaviour should all be public. Any method that seems like it should be private is actually an unextracted method of another class. Extract that class and method and watch your design simplify before your very eyes.

查看更多
够拽才男人
4楼-- · 2019-04-19 13:36

If you use getters/setters you can perform logic upon changes or access. You could validate input, instead of assuming it is always correct. You could track how many times the value is fetched.

Most of all, it's good design. It gives you, the developer of the class, more control over how it is used and a greater ability to prevent misuse, abuse, or just someone doing something wrong.

查看更多
登录 后发表回答