博客网 >

Head First Design Paterns Notes 1
作者:分类:默认分类标签:
The book was written by Eric Freeman and Elisabeth Freeman. A very famous book in design pattern.

I begin reading this book recently, and I plan to post some notes which I valued very much in my later

developement..



1st Chapter: Into to Design Pattern

This chapter cites an sample of a duck app, which it may quack or squeak or no sound, also it may fly or no fly...

firstly it uses an abstract class:
Class Duck() implement with Swim(), Display() and Quack().
there are several sub-classes like MallardDuck() and RedHeadDuck()

then the boss asked some duck need to fly. There are several solutions for this:
1. Add a virtual function fly in Duck().
2. Create an interface for fly, and implement in sub-classes of duck when necessary.
3. Create an interface for fly, and create a behavior class like FlyWithWings() which implement the fly interface. In Duck() class, we will add a member variable "flyBehaviro" with the type of fly interface, and create a virtual  function to perform fly like performFlyI() in which we add code: flyBehavior.fly(); and add a function to set behavior like setFlyBehavior() which will make seting behavior dynamically possible.

In all, 3 is much best than 1 and 2, 2 is better than 1. Because in 1 solution, all sub-class ducks will fly...  In 2, we have implement the interface for each sub-duck that can fly, everytime we implement this function, it may bring bugs... and QA have to test for each kind of duck that can fly. In 3 solution, it's much flexible, there is no need to change any code in future for existing sub-class duck. And it is much more extensible, for example, if there is a duck which can fly by rocket, then we just need to create a class named FlyWithRocket in which we implement the logic of Rocket flying, and then we set fly behavior to FlyWithRocket() for the new duck. This will not change the existed code.


OO stands for Oriented Object.

OO Basics
Abstraction
Encapsulation
Polymorphism
Inheritence


OO Principles
1>. Encapsulate what varies.
take what varies and "encapsulate" it so it won't affect the rest of your code. Take the parts that vary and

encapsulate them, so that later you can alter or extend the parts that vary without affecting those that

don't. This will bring to fewer unintended consequences from code changes and more flexibility in your system.

2>. Favor composition over inheritence.
When you are put two classes that are implemented as a specific behavior from a common interface, you are

using composition. Creating system using compositing gives you a lot of flexibility. Not only does it enable

you to encapsulate a family of algorithms into their own set of classes, but it also lets you change behavior

at runtime as logn as the object you are composing with implements the correct behavior of interface.
Composition is used in many design patterns.

3>. Program to interface, not implementation.
"program to interface" really means "program to a suppertype."
sample: imagine an abstract class animal, with two concrete implementations, Dog and Cat.
Programming to an implementation would be:
Dog d= new Dog();
d.bark();

But programming to an interface/supertype would be:
Animal animal=new Dog();
animal.makeSound();
Even better, rather than hard-coding the instantion of the subtype(like new Dog()) into the code, assign the concrete implementation object at runtime:
a=getAnimal();
a.makeSound();


OO Paterns
The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.

Strategy lets the algorithm vary independntly from clients that use it.


Bullet Points
1. Knowing the OO basics does not make you a good OO designer.
2. Good OO designs are reusable, extensible and maintainable.
3. Patterns are proven OO experience.
4. Patterns aren't invented, they are discovered.
5. Most patterns and principles address issues of change in software.
6. Most patterns allow some part of a system to vary independetly of all other parts.

<< ORA-00911 wired ... / Javascript Memor... >>

专题推荐

不平凡的水果世界

不平凡的水果世界

平凡的水果世界,平凡中的不平凡。 今朝看水果是水果 ,看水果还是水果 ,看水果已不是水果。这境界,谁人可比?在不平凡的水果世界里,仁者见仁,智者见智。

中国春节的那些习俗

中国春节的那些习俗

正月是农历新年的开始,人们往往将它看作是新的一年年运好坏的兆示期。所以,过年的时候“禁忌”特别多。当然,各个地方的风俗习惯不一样,过年的禁忌也是不一样的。

评论
0/200
表情 验证码:

gisman

  • 文章总数0
  • 画报总数0
  • 画报点击数0
  • 文章点击数0
个人排行
        博文分类
        日期归档