design pattern"Headfirst Design Pattern"

This is a backup of my reading notes: From:http://blog.4kiki.net/hsy75/article/1306/
==============================================================================================================
[8-21] finish the creaters of head first series and read the table of contents which
include the basic introduce of design patterns. start to read the how to use this
books.(page53)
[8-23]Intro to design patterns.Joe make a flaw while use the inheritance , and he
want to use the override method to solve the problem.Joe then decided to use the
added interface to implement duck feature.But this subclasses implement would creat a
maintenace nightmare,what the design pattern would save as,(page 60)
[8-24]Design Principle:Take what varies and "encapsulate"it so it won't affect the
rest of your code.The writer question us the method to "separating what changes from
what stays the same"
[8-25]Design Principle:Program to an interface,not an implementation.Program to an
interface really means program to a supertype: Here Eric give us some details
explaination, here, as I ask a super guy of OO, he said that in OOD, the program to
an interface is realized by program to a supertype. another word that program to the
supertype is a method to realize the program to an interface in OOD.
implement the fly and quack behaviors.Using a setter method to make the behavior
dynamically change.The concrete behavior is implemented on the interface,but not the
one which we can called concrete class.take care the versus of each. The JAVA do
have the construct interface , but the interface here writer means is different. it
is a wide use here.
[8-26] integrating.To realize the integrate of Duck behavior, Joe need to add the
instance variables (quackBehavior) used for interface which reference to the the
specific behavior object (QuackBehavior) and then modify the method (perforQuack)in
the (Duck)class,so that the object (Duck) can delegates the behavior (Quack one of
the concrete implement class of QuackBehavior ) to the object (QuackBehavior
)referenced by quackBehavior.
In my understanding, as view point from the memory, while the MallardDuck
inherites(constructor initializes) the Duck class, the Superclass' reference is to
the concrete object Quack(the same share store place in the memory), so the
responsibility for the quack is delegated to the Quack object and then we get the
real quack which we want.
[Q1] what can we tell the declared is a interface type but not a concrete class
implementation type? A interface is a part of the code we do not care what kinds of
object it is , all we care about is that the object should knows how to implement.
In sub class (mallardduck) , just made the instance variable(quackBehavior) delegates
to the real behavior quack, for detailes.
inherits the instance variables(quackBehavior) from the super class (Duck) which
reference to the behavior object (QuackBehavior ),then "quackBehavior=new Quack" to
vivilife_design pattern"Headfirst Design Pattern"
use the Quack class to handle the quack, so when the super class(Duck)'s method
(perforQuack)is called, the interface quack is delegated to the Quack object . when
the MallardDuck is instantiated, its constructor initializes the MallardDuck's
inherited quackBehavior instance variable to a new instance of type Quack.
[10-13]71 test the Duck code:[Notic]Use the abstract class so that the polymorphism
can work, the abstract class is a class can not be instantiated.[Notic]interface in
java, means: its member are not implemented.[refer to] interface vs abstract class:
1interface class,2calss.vs.interfacevs.abs class.the interface in driver benifit the
advantages to multiple inheritance in java.Design Principle:Favor composition over
inheritance.
God,I has just finished learn the first design pattern: STRATEGY pattern: Define the
family of algorithms,encapsulates each one, and make them interchangeable,Stategy
lets the algorithm vary independently from clients that use it.

你可能感兴趣的:(design pattern"Headfirst Design Pattern")