Thinking in Java对象设计的一些tips

2009-06-20

Guidelines for object development 
These stages suggest some guidelines when thinking about developing your classes: 
   1. Let a specific problem generate a class, then let the class grow and mature during the solution of other problems.
   2. Remember, discovering the classes you need (and their inter-faces) is the majority of the system design. If you already had those classes, this would be an easy project.
     3. Don’t force yourself to know everything at the beginning. Learn as you go. This will happen anyway.  
     4. Start programming. Get something working so you can prove   or disprove your design. Don’t fear that you’ll   end   up   with   procedural-style spaghetti code—classes partition   the   problem   and help control anarchy and entropy. Bad classes   do   not   break good classes.  
   5. Always keep it simple.   Little   clean objects with   obvious utility are better than big complicated interfaces. When decision points come up, use an Ockham’s   Razor   approach:   Consider   the choices and select the one that is simplest, because simple class-es are   almost   always best. Start   small and simple, and you can expand the class interface when you understand it   better. It’s   easy to add methods, but   as time   goes   on,   it’s   difficult to remove methods from a class. 
开发类时:
       1. 为特定问题生成一个类,然后在解决其他问题的过程中,让这个类成长并成熟。
       2. 记住,发现你所需要的类(和接口)是系统设计的主要工作。如果这些类已经有了,那么这将是个简单的项目。
       3. 不要强迫自己在开始阶段就明白所有事情。在学习的过程中,随时可以了解某些事情。
       4. 开始编程。做出可以运行的某些东西,才可以证明或者反驳你的设计。不要担心你的代码会变成意大利面条式的过程行代码——类可以讲问题分解,并有助于对无序和混乱的状态进行控制。不好的类不会破坏好的类。
       5. 保持简单。功能明确、短小而清楚的对象,好过庞大而复杂的接口。需要做决定的时候,可以使用Ockham的剃刀法则:考虑最简单的选择,因为简单的类总是最好的。由短小而简单的开始,在更好的理解问题之后,可以扩展类或接口。向类中添加方法总是容易的,但随着时间的推移,从类中删除方法可就困难了。
                                                                             -----------摘自《Thinking in Java》第三版16章

猜你喜欢

转载自aeolus1983.iteye.com/blog/2309276
今日推荐