Thinking in Java对象设计的一些tips

本文提供了面向对象设计的一些指导原则,包括如何根据具体问题定义类、让类随着解决方案的发展而成熟、保持设计简单等。强调了类的设计对于系统设计的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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章

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值