design pattern in ruby

 general principles, to four points: 
• Separate out the things that change from those that stay the same. 
• Program to an interface, not an implementation. 
• Prefer composition over inheritance. 
• Delegate, delegate, delegate. 
following sections, we will look at each of these principles in turn, to see 
what they can tell us about building software.
Separate Out the Things That Change from Those 
That Stay the Same 
Software engineering would be a lot easier if only things would stay the same. We 
could build our classes serene in the knowledge that, once finished, they would continue to do exactly what we built them to do. Of course, things never stay the same, 
not in the wider world and certainly not in software engineering. Changes in com- 
puting hardware, operating systems, and compilers, combined with ongoing bug fixes and ever-migrating requirements, all take their toll. 
A key goal of software engineering is to build systems that allow us to contain the 
damage. In an ideal system, all changes are local: You should never have to comb 
through all of the code because A changed, which required you to change B, which 
triggered a change in C, which rippled all the way down to Z. So how do you achieve— or at least get closer to—that ideal system, the one where all changes are local? 
You get there by separating the things that are likely to change from the things 
that are likely to stay the same. If you can identify which aspects of your system design 
are likely to change, you can isolate those bits from the more stable parts. When 
requirements change or a bug fix comes along, you will still have to modify your code, 
but perhaps, just perhaps, the changes can be confined to those walled-off, change-prone areas and the rest of your code can live on in stable peace. 
But how do you effect this quarantine? How do you keep the changing parts from 
infecting the stable parts? 
Program to an Interface, Not an Implementation 
A good start is to write code that is less tightly coupled to itself in the first place. If our 
classes are to do anything significant, they need to know about each other. But what




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/73334,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值