Reading Head First Design Pattern note.

来源:互联网 发布:悦游网络加速器官方 编辑:程序博客网 时间:2024/05/15 05:17


*********************
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.

 

 

原创粉丝点击