Classes (1)

xiaoxiao2021-03-06  53

Good Encapsulation1. Minimiza accessibility of classes and members.2. Do not expose member data in public.3. Do not put private implementation details in a class's interface4. Favor read-time convenience to write-time convenience.Code is read far more times than it's written, even during initial development.5.Be very, very wary of semantic violaions of encapsulation.Here are some examples of the ways that a user of a class can break encapsulation semantically: Not calling class A's initialize () routine because you know that Class A's performFirstOperation () routine calls it automatically.Not calling the database.connect () routine before you call employee.retrieve (database) because you know that the employee.retrieve () function will connect to the database if there is not already a connection.The proble with each of these examples is that they make the client code dependent not on the class's public interface, but on its private implementation. Anytime you find yourself looking at a class' Si Use The Class, You're Not Programming To The Interface; You're Programming THROUGH The Interface To The Implementation.6. Watch for Coupling That's Tight.

转载请注明原文地址:https://www.9cbs.com/read-115424.html

New Post(0)