Reading Head First Design Pattern note.
2008-02-19 10:07
375 查看
*********************
The Open-Closed Principle
Design Principle
Classes should be open
for extension, but closed for
modification.
you’ll see a good example of using the
Decorator pattern to follow the Open-
Closed principle.
Decorators have the same supertype as the objects they decorate.
You can use one or more decorators to wrap an object.
Given that the decorator has the same supertype as the object it decorates, we can pass
around a decorated object in place of the original (wrapped) object.
The decorator adds its own behavior either before and/or after delegating to the object it
decorates to do the rest of the job.
Objects can be decorated at any time, so we can decorate objects dynamically at runtime
with as many decorators as we like.
The Decorator Pattern attaches additional
responsibilities to an object dynamically.
Decorators provide a flexible alternative to
subclassing for extending functionality.
(1) Each decorator HAS-A
(wraps) a component, which
means the decorator has an
instance variable that holds
a reference to a component.
(2)Decorators implement the
same interface or abstract
class as the component they
are going to decorate.
OO Principles
Encapsulate what varies.
Favor composition over inheritance.
Program to interfaces, not
implementations.
Strive for loosely coupled designs
between objects that interact.
Classes should be open for
extension but closed for
modification.
All factory patterns encapsulate object creation. The Factory Method Pattern encapsulates
object creation by letting subclasses decide what objects to create.Let's check out these
class diagrams to see who the players are in this pattern.
***The Factory Method Pattern*** defines an interface for creating an object,
but lets subclasses decide which
class to instantiate.Factory Method lets a class defer instantiation to subclasses.
***The Dependency Inversion Principle***
Design Principle
Depend upon abstractions.Do not depend upon concrete classes.
At first,this principle sounds a lot like "Program to an interface,not an implementation," right?
It is similar;however,the Dependency Inversion Principle makes an even stronger statement
about abstraction.It suggests that our high-level components should not depend on our low-level
components;rather, they should both depend on abstractions.
Abstract Factory Pattern :provides an interface for creating families of related or dependent objects
without specifying their concrete classes.
The Singleton Pattern :ensures a class has only one instance,and provides a global point of access to it.
The Open-Closed Principle
Design Principle
Classes should be open
for extension, but closed for
modification.
you’ll see a good example of using the
Decorator pattern to follow the Open-
Closed principle.
Decorators have the same supertype as the objects they decorate.
You can use one or more decorators to wrap an object.
Given that the decorator has the same supertype as the object it decorates, we can pass
around a decorated object in place of the original (wrapped) object.
The decorator adds its own behavior either before and/or after delegating to the object it
decorates to do the rest of the job.
Objects can be decorated at any time, so we can decorate objects dynamically at runtime
with as many decorators as we like.
The Decorator Pattern attaches additional
responsibilities to an object dynamically.
Decorators provide a flexible alternative to
subclassing for extending functionality.
(1) Each decorator HAS-A
(wraps) a component, which
means the decorator has an
instance variable that holds
a reference to a component.
(2)Decorators implement the
same interface or abstract
class as the component they
are going to decorate.
OO Principles
Encapsulate what varies.
Favor composition over inheritance.
Program to interfaces, not
implementations.
Strive for loosely coupled designs
between objects that interact.
Classes should be open for
extension but closed for
modification.
All factory patterns encapsulate object creation. The Factory Method Pattern encapsulates
object creation by letting subclasses decide what objects to create.Let's check out these
class diagrams to see who the players are in this pattern.
***The Factory Method Pattern*** defines an interface for creating an object,
but lets subclasses decide which
class to instantiate.Factory Method lets a class defer instantiation to subclasses.
***The Dependency Inversion Principle***
Design Principle
Depend upon abstractions.Do not depend upon concrete classes.
At first,this principle sounds a lot like "Program to an interface,not an implementation," right?
It is similar;however,the Dependency Inversion Principle makes an even stronger statement
about abstraction.It suggests that our high-level components should not depend on our low-level
components;rather, they should both depend on abstractions.
Abstract Factory Pattern :provides an interface for creating families of related or dependent objects
without specifying their concrete classes.
The Singleton Pattern :ensures a class has only one instance,and provides a global point of access to it.
相关文章推荐
- head first design pattern笔记整理
- Head First Design Pattern - 3 - This week's interview: Confessions of a Decorator
- Book Review about Head First Design Pattern
- 设计模式之 观察者模式 the observer design pattern ---------《head first design pattern》
- OO设计模式(摘自:《Head First Design Pattern》)
- head first Design Pattern Iterator & Composite
- HeadFirst design pattern笔记-观察者模式
- head first design pattern 阅读笔记
- 观察者模式(一)--《Head First DesignPattern》
- 《Head First Design Patterns》笔记六:命令模式(Command Pattern)
- 学习Head First Design Pattern——翻译Chapter 3:The Decorator Pattern
- Head First Design Pattern - 4 - This week's interview: Factory Method and Abstract Facotry, on each other
- -Head First Design Pattern- 大师与门徒(2) -怀疑的蚱蜢
- HeadFirst design pattern笔记-单例模式
- Head First Design Patterns - Template Method Pattern
- 《Head First Design Pattern 》--设计模式 读书笔记
- Head-First Servelts&JSP reading note 5
- 《Head First Design Patterns》笔记十:迭代器模式(Iterator Pattern)
- Head First Design Pattern读后感
- HeadFirst design pattern笔记-装饰者模式