设计模式--学习指导

一个月来,忙里偷闲,研读了《设计模式》这本书,掩卷回味,觉得收获颇丰。

我是在读完《设计模式解析》这本书之后来读《设计模式》的。在《设计模式解析》这本书中,我也受到了很大的冲击:原来,程序是这样设计出来的。她更新了我的面向对象设计的最基础的最根本的知识。试看如下的一些面向对象设计的一些观点:

1. An object is an entity that has responsibilities. Theseresponsibilities give the object its behavior. Sometimes, I also thinkof an object as an entity that has specific behavior.

2. Encapsulation should be thought of as "any kind of hiding." In other
words, it can hide data. But it can also hide implementations, derived
classes, or any number of things.

3.  Find what varies and encapsulate it.

4.  Favor composition over inheritance.

5. Some rules about OOD

• Objects are things with well-defined responsibilities.
• Objects are responsible for themselves.
• Encapsulation means any kind of hiding
- Data-hiding
- Class hiding (behind an abstract class or interface)
- Implementation hiding
• Abstract out variations in behavior and data with commonality/variability analysis.
• Design to interfaces.
• Think of inheritance as a method of conceptualizing variation,not for making special cases of existing objects
• Keep variations in a class decoupled from other variations in the class.
• Strive for loose coupling.
• Strive for high cohesion.
• Be absolutely meticulous in applying the once and only once rule.

总之,在《设计模式解析》这本书中,你随时都可以看到作者的睿智,他对面向对象设计的独到而深邃的理解,让你如沐春风,久而弥醉。

读完这本书后,我又拿起了这本书中不断提到的《设计模式》。通观《设计模式》这本书,她把23常见的模式分为三类来讲解:Creational Patterns,Structural Patterns,Behavioral Patterns,而后,在其中又可以分为:Class Patterns  and Object Patterns。并且对每个模式都采用了统一的描述方法。Intent,Motivation,Applicability,Structure,Participants,Collaborations,Consequences, Implementation,Sample Code,Known Uses,Related Patterns.为了辅助学习期间,我简要介绍一下各部分的主要内容和要把握的问题。

Intent:主要介绍的这个模式的目的,是我们应该关注的地方,认真体会一下作者的陈述,你会对此模式有一个学习的要点。

Motivation:一般作者给一个例子来引出这个模式的设计初衷,也是应该关注的地方,知其然并知其所以然吗。

Application:这是特别要认真学习的地方,在这里作者给出了在什么样的情况下,我们要使用这个模式,这是我们的指导性的部分。

Structure:这是这个模式中类、对象的UML图,认真领会这个图,可以对这个模式有一个整体上的理解,也是学习Paticipants,Collaborations,Implementation 和 Sample code的指导性部分。

Paticipants:针对Structure的类讲解,很容易看懂的部分。

Collaborations:是对Structure部分的深入讲解,比较难理解的模式作者都给出了协作图来描述。主要是对模式中对象间的协作关系的讲解。

Consequences:使用这个模式将会产生的好处和不足。如果,时间紧的话,这部分可以先略去不认真的研读。

Implementation:这部分主要讲解的是在使用这个模式时会遇到的一些问题,并给出了权衡的解决方案。需要认真研读。

Sample Code:针对这个模式,给出了一个框架上的代码设计。如果时间紧的话,可以直接来读这部分达到尽快上手的目的。不过,这需要很强的编程经验。

Known Uses:给出是使用此模式的已经成功的方案,完全可以略去不读,读了也不会对这个模式有什么帮助。

Related Patterns:这部分可能是读者最容易忽略的,但是,我认为这部分是非常重要的。因为,在一个比较大的系统下,都是很多模式的集合体,在这里,作者给出了这个模式和别的模式如何协作起来完成一个功能。是应该付出时间来体会的,回报也是很大的。

最后,以这本书中一段话结束:

It's easiest to see a pattern as a solution, as a technique that can be adapted and reused. It's harder to see when it is appropriate—to characterize the problems it solves and the context in which it's the best solution. In general, it's easier to see what someone is doing than to know why, and the "why" for a pattern is the problem it solves. Knowing the purpose of a pattern is important too, because it helps us choose patterns to apply. It also helps us understand the design of existing systems. A pattern author must determine and characterize the problem that the pattern solves, even if you have to do it after you've discovered its solution.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值