《深入浅出设计模式(影印版)》章节试读

当前位置:首页 > 网络编程 > > 深入浅出设计模式(影印版)章节试读

出版社:东南大学出版社
出版日期:2005-11
ISBN:9787564101657
作者:Eric Freeman,Elisabeth Freeman,Kathy Sierra,Bert Bates
页数:638页

《深入浅出设计模式(影印版)》的笔记-模式三:装饰者模式 - 模式三:装饰者模式

定义:动态地将责任附加到对象上。想要扩展功能,装饰者提供有别于继承的另一种选择。
要点:
1、继承属于扩展形式之一,但不见得是达到弹性设计的最佳方式。
2、在我们的设计中,应该允许行为可以被扩展,而无须修改现有的代码。
3、组合和委托可用于在行动时动态地加上行为。
4、除了继承,装饰者模式也可以让我们扩展行为。
5、装饰者模式意味着一群装饰者类,这些类用来包装具体组件。
6、装饰者类反映出被装饰的组件的类型,都经过接口或继承实现。
7、装饰者可以在被装饰者的行为前面与/或后面加上自己的行为整个取代掉,而达到特定的目的。
8、你可以用无数个装饰者类包装一个组件。
9、装饰者一般对组件的客户是透明的,除非客户程序依赖于组件的具体类型。
10、装饰者会导致设计中出现许多小对象,如果过度使用,会让程序变得很复杂。
11、java有许多类都是用了装饰者模式,比如:JAVA I/O类。

《深入浅出设计模式(影印版)》的笔记-第337页 - Iterator Pattern defined

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
> The Iterator Patterns allows traversal of the elements of an aggregate without exposing the underlying implementation.
> It also places the task of traversal on the iterator object, not on the aggregate, which simplifies the aggregate interface and implementation, and places the responsibility where it should be.
>Having a common interface for your aggregates is handy for your client; it decouples your client from the implementation of your collection of objects.
>The iterator interface provides the interface that all iterators must implement, and a set of methods for traversing over elements of a collection.
>The ConcreteAggregate has a collection of objects and implements the method that returns an iterator for its collection.
>Each ConcreteAggregate is responsible for instantiating a ConcreteIterator that can iterate over its collection of objects.
>The ConcreteIterator is responsible for managing the current position of the iteration.

《深入浅出设计模式(影印版)》的笔记-Chap1-2 - Chap1-2

Chap1:
introductory duck quack fly
Favor composition over inheritance
The Strategy Pattern defines a family of algorithms,
encapsulates each one, and makes them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it.
Communicate using pattern, think in pattern.
key: thinking about how they might change in the
future
Chap2:
Observer pattern
weather data example
Publishers + Subscribers = Observer Pattern
The Observer Pattern defines a one-to-many
dependency between objects so that when one
object changes state, all of its dependents are
notified and updated automatically.
Loose Coupling
解耦
Design Principle
Strive for loosely coupled designs
between objects that interact.
Java built-in Observable class

《深入浅出设计模式(影印版)》的笔记-第289页 - Template Method Pattern define

The Template Method Pattern defines a of an algorithm in a method, deferring some steps to subclasses. Template Methods lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
The Template Method defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps.

《深入浅出设计模式(影印版)》的笔记-第410页 - The State Pattern defined

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
> This is because the pattern encapsulate state into separate classes an delegates to the object representing the current state.
> It is the perspective of a client: if an object you're using can completely change its behavior, then it appears to you that the object is actually instantiated form another class. And we use Composition to give the appearance of a class change by simply referencing different state objects.
> The Context is the class that can have a number of internal states.
> The State interface defines a common interface for all concrete states; the states all implement the same interface, so they are interchangeable.
> Whenever the quest() is made on the Context, it is delegated to the state to handle.
> ConcreteStates handles request from the Context. Each ConcreteState provides its own implementation for q request. In this way, when the Context changes state, its behavior will change as well.

《深入浅出设计模式(影印版)》的笔记-第1页 - Head First Design Pattern

Chapter 01 Intro to Design Patterns
第01章 设计模式入门

1. Design Principle Identify the aspects of your application that vary and separate them from what stays the same. (P9)
设计原则:找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起。
2. Design Principle Program to an interface, not an implementation. (P11)
设计原则:针对接口编程,而不是针对实现编程。
3. Design Principle Favor composition over inheritance. (P23)
设计原则:多用组合,少用继承。
4. The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. (P24)
策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
Chapter 02 the observer pattern
第02章 观察者模式

1. The Observer Pattern defines a one-to-many dependency between objects so that one object change state,all of its dependents are notified and updated automatically. (P51)
观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
2. Design Principle Strive for loosely coupled designs between objects that interact. (P53)
设计原则:为了交互对象之间的松耦合设计而努力。
3. Loosely coupled designs allow use to build flexible OO systems that can handle change because they minimize the interdependency between objects. (P53)
松耦合的设计之所以能让我们建立有弹性的OO系统,能够应对变化,是因为对象之间的互相依赖降到了最低。
Chapter 03 the decorator pattern
第03章 装饰者模式

1. Design Principle Classes should be open for extension, but closed for modification. (P86)
设计原则:类应该对扩展开放,对修改关闭。
2. Be careful when choosing the areas of code that need to be extended; applying the Open-Closed Principle EVERYWHERE is wasteful, unnecessary, and can lead to complex, hard to understand code. (P87)
在选择需要被扩展的代码部分时需要小心。每个地方都采用 开放-关闭 原则是一种浪费,也没必要,还会导致代码变得复杂而且难以理解。
3. The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. (P91)
装饰者模式:动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
4. But Java I/O also points out one of the downsides of the Decorator Pattern: designs using this pattern often result in a large number of small classes that can be overwhelming to a developer trying to use the Decorator-based API. (P101)
但是Java I/O 也引出装饰者模式的一个“缺点”:利用装饰者模式,常常造成设计中有大量的小类,数量实在太多,可能会造成使用此API程序员的困扰。
Chapter 04 the factory pattern
第04章 工厂模式

1. 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. (P134)
工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的是哪一个。工厂方法让类把实例化推迟到子类。
2. Design Principle Depend upon abstractions. Do not depend upon concrete classes. (P139)
设计原则:要依赖抽象,不要依赖具体类。
3. The Abstract Factory Pattern provides an interface for creating families related or dependent objects without specifying their concrete classes. (P156)
抽象工厂模式:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
4. The intent of Factory Method is to allow a class to defer instantiation to its subclasses. (P162)
工厂方法允许类将实例化延迟到子类进行。
5. The intent of Abstract Factory is to create families of related objects without having to depend on their concrete classes. (P162)
抽象工厂创建相关的对象家族,而不需要依赖它们的具体类。
Chapter 05 the singleton pattern
第05章 单例模式
1. The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it. (P177)
单例模式:确保一个类只有一个实例,并提供一个全局访问点。
Chapter 06 the command pattern
第06章 命令模式

1. The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations. (P206)
命令模式:将“请求”封装成对象,以便使用不用的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。
2. The Command Pattern decouples an object making a request from one that knows how to perform it. (P230)
命令模式将发出请求的对象和执行请求的对象解耦。
Chapter 07 the Adapter and Facade Patterns
第07章 适配器模式与外观模式

1. Here's how the client uses the Adapter (P241)
1.1 The client makes a request to the adapter by calling a method on it using the target interface.
1.2 The adapter translates that request into one or more calls on the adaptee using the adaptee interface.
1.3 The client receives the result of the call and never knows there is an adapter doing the translation.
客户使用适配器的过程如下:
1.1 客户通过目标接口调用适配器的方法对适配器发出请求。
1.2 适配器使用被适配者接口把请求转换成被适配者的一个或多个调用接口。
1.3 客户接收到调用的结果,但未察觉这一切是适配器在起转换作用
2. The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. (P243)
适配器模式:将一个类的接口,转换成客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间。
3. A facade not only simplifies an interface, it decouples a client from a subsystem of components. (P260)
外观模式不只是简化了接口,也将客户从组件的子系统中解耦。
4. Facades and adapters may wrap multiple classes, but a facade's intent is to simplify, while an adapter's is to convert the interface to something different. (P260)
外观和适配器可以包装许多类,但是外观的意图是简化接口,而适配器的意图是将接口转换成不同接口。
5. The Facade Pattern provides a unified interface to a set of interfaces inn a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. (P264)
外观模式:提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。
6. Design Principle Principle of Least Knowledge - talk only to your immediate friends. (P265)
设计原则:最少知识原则,只和你的密友谈话。
7. The principle tells us that we should only invoke methods that belong to: (P266)
The object itself
Objects passed in as a parameter to the method
Any object the method creates or instantiates
Any components of the object
这个原则告诉我们应该调用以下方法:
对象本身
被当作方法的参数而传递进来的对象
此方法所创建或实例化的任何对象
对象的所有组件
--------------------------------------------------
author: cs_cjl
website: http://blog.csdn.net/cs_cjl
--------------------------------------------------
Chapter 08 the template method Pattern
第08章 模版方法模式

1. The Template Method defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps. (P286)
模版方法定义了一个算法的步骤,并允许子类为一个或多个步骤提供实现。
2. The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some stepss to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. (P289)
模版方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模版方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
3. The Hollywood Principle Don't call us, we'll call you. (P296)
好莱坞原则:别调用我们,我们会调用你。
Chapter 09 the iterator and composite pattern
第09章 迭代器与组合模式
1. The Iterator Patternprovides a way to access the eleemnts of an aggregate object sequentially without exposing its underlying respresentation. (P336)
迭代器模式 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。
2. Design Principle : A class should have only one reason to change. (P339)
设计原则: 一个类应该只有一个引起变化的原因。
3. The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. (P356)
组合模式:允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。
4. The Composite Pattern allows us to build structures of objects in the form of trees that contain both compositions of objects and individual objects as nodes. (P357)
组合模式让我们能用树形方式创建对象的结构,树里面包含了组合以及个别的对象。
5. Using a composite structure, we can apply the same operations over both composites and individual objects. In other words, in most cases we can ignore the differences between compositions of objects and individual objects. (P357)
使用组合结构,我们能把相同的操作应用在组合和单独的对象上。换句话说,在大多数情况下,我们可以忽略对象组合和单独对象之间的差别。
Chapter 10 the State Pattern
第10章 状态模式

1. The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. (P410)
状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
Chapter 11 the proxy pattern
第11章 代理模式
1. By invoking methods on the proxy, a remote call is made across the wire and a String, an integer and a State object are returned. Because we are using a proxy, the GumballMonitor doesn't know, or care, that calls are remote(other than having to worry about remote exceptions). (P457)
通过调用代理的方法,远程调用可以跨过网络,返回字符串、整数和State对象。因为我们使用的是代理,调用的方法会在远程执行,GumallMonitor根本就不知道/或不在于这一点(唯一要操心的是:要处理远程异常)。
2. The Proxy Pattern provides a surrogate or placeholder for another object to control access to it. (P460)
代理模式:为另一个对象提供一个替身或占位符以控制对这个对象的访问。
3. Use the Proxy Pattern to create a representative object that controls access to another object, which may be remote, expensive to create or in need of securing. (P460)
使用代理模式创建代表对象,让代表对象控制某对象的访问,被代理的对象可以是远程的对象、创建开销大的对象或需要安全控制的对象。
Chapter 12 Compound Patterns
第12章 复合模式

1. Patterns are often used together and combined within the same design solution. (P500)
模式通常被一起使用,并被组合在同一个设计解决方案中。
2. A compound pattern combines two or more patterns into a solution that solves a recurring or general problem. (P500)
符合模式在一个解决方案中结合两个或多个模式,以解决一般或重复发生的问题。

《深入浅出设计模式(影印版)》的笔记-模式十一:组合模式 - 模式十一:组合模式

一、定义:符合模式结合两个或以上的模式,组成一个解决方案,解决一再发生的一般性问题。
二、要点:
1、MVC是复合模式,结合了观察者模式、策略模式和组合模式。
2、模型使用观察者模式,以便观察者更新,同时保持两者之间解耦。
3、控制器是视图的策略,视图可以使用不同的控制器实现,得到不同的行为。
4、视图使用组合模式实现用户界面,用户界面通常组合了嵌套的组件,像面板、框架和按钮。
5、这些模式携手合作,把MVC模型的三层解耦,这样可以保持设计干净又有弹性。
6、适配器模式用来将新的模型适配成已有的视图和控制器。
7、Model2是MVC在Web上的应用。
8、在Model2中 ,控制器实现成Servelet,而JSP/HTML实现视图。

《深入浅出设计模式(影印版)》的笔记-第206页 - The command Pattern defined

The command Pattern encapsulates a request as an object, thereby letting you parameterize objects with different requests, queue or log requests, and support undoable operations.
The class diagram
> The Client is responsible for creating a ConcreteCommand and setting its Receiver.
> The Invoker holds a command and at some point asks the command to carry out a request by calling its execute() method.
> Command declares an interface for all commands. As you already know, a command is invoked through its execute() method, which asks a receiver to perform an action. You'll also notice this interface has an undo() method, which we'll cover a bit later.
> The execute method invokes the actions(s) on the receiver needed to fulfill the request.
> The Receiver knows how to perform the work needed to carry out the request. Any class can act as a Receiver.
> The ConcreteCommand defines a binding between an action and a Receiver. The Invoker makes a request by calling execute() and the ConcreteCommand carries it out by calling one or more actions on the Receiver.

《深入浅出设计模式(影印版)》的笔记-第91页 - The Decorator Pattern defined

The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

《深入浅出设计模式(影印版)》的笔记-模式十:代理模式 - 模式十:代理模式

一、定义:为另一个对象提供一个替身或占位符以访问这个对象。
二、要点:
1、代理模式为另一个对象提供代表,以便控制客户对对象的访问。管理访问的方式有许多种。
2、远程代理管理客户端和远程对象之间的交互。
3、虚拟代理控制访问实例化开销大的对象。
4、保护代理基于调用者控制对象方法的访问。
5、代理模式有许多变体,例如:缓存代理、同步代理、防火墙代理和写入时复制代理。
6、代理在结构上类似装饰者,但是目的不同。
7、装饰者模式为对象加上行为,而代理则是控制访问。
8、Java内置的代理支持,可以根据需要建立动态代理,并将所有调用分配到所选的处理器。
9、就和其他的包装者(Wrapper)一样,代理会造成你的设计中类的数目增加。

《深入浅出设计模式(影印版)》的笔记-第143页 - A few guidelines to help you follow the Principle...

No variable should hold a reference to a concrete class.
> If you use new, you'll be holding a reference to a concrete class. Use a factory to get around that.
No class should derive from a concrete class.
> If you derive from a concrete class, you're depending on a concrete class. Derive from an abstraction, like an interface or an abstraction class.
No method should override an implemented method of any of its base classes.
> If you override an implemented method, then your base class wasn't really an abstraction to start with. Those methods implemented in the base class are meant to be shared by all your subclass..

《深入浅出设计模式(影印版)》的笔记-第23页 - Design Principle

Design Principle : Favor composition over inheritance. (相比较继承,优先考虑组合)
Alias: HAS-A can be better than IS-A.
Description: As you've seen, creating systems using composition gives you a lot of flexibility. Not only does it let you encapsulate a family of algorithms into their own set of classes, but it also let you change behavior at runtime as long as the object you're composing with implements the correct behavior interface.

《深入浅出设计模式(影印版)》的笔记-模式五:单件模式 - 模式五:单件模式

一、定义:确保一个类只有一个实例,并提供全局的访问点。
二、模式适用场景:有一些对象其实我们只需要一个,比方说:线程池、缓存、对话框、处理偏好设置和注册表的对象、日志对象、充当打印机、显卡等设备的驱动程序的对象。事实上,这类对象只能有一个实例,如果制造出多个实例,就会导致许多问题的产生,例如:程序的行为异常、资源使用过量,或者是不一致的结果。
三、要点:
1、单件模式确保程序中一个类最多只有一个实例。
2、单件模式也提供访问这个实例的全局点。
3、在Java中实现单件模式需要私有构造器、一个静态方法和一个静态变量。
4、确定在性能和资源上的限制,然后小新低选择适当的方案来实现单间,以解决多线程的问题(我们必须认定所有的程序都是多线程的)。
5、如果不是采用第五版的Java 2,双重检查锁实现会失效。
6、小心,你如果使用多个类加载器,可能导致单间失效而产生多个实例。(因为每一个类加载器都定义了一个命名空间,如果有两个以上的类加载器,不同的类加载器可能会加载统一各类,从整个程序来看,同一个类会被加载多次。)
7、如果使用JVM 1.2或之前的版本,你必须建立单件注册表,以免垃圾收集器将单件回收。
更多有用的相关资料:
http://www.cnblogs.com/seesea125/archive/2012/04/05/2433463.html#top
http://cantellow.iteye.com/blog/838473

《深入浅出设计模式(影印版)》的笔记-模式九:状态模式 - 模式九:状态模式

一、定义:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
二、本章要点:
1、状态模式允许一个对象基于内部状态而拥有不同的行为。
2、和程序状态机不同,状态模式用类代表状态。
3、Context会将行为委托给当前状态对象。
4、通过将每个状态封装进一个类,我们把以后需要做的任何改变局部化了。
5、状态模式和策略模式有相同的类图,但是他们的意图不同。
6、策略模式通常会用行为或算法来配置Context类。
7、状态模式允许Context随着状态改变而改变行为。
8、状态转换可以由State类或Context类控制。
9、使用状态模式通常会导致设计中类的数目大量增加。
10、状态类可以被多个Context实例共享。

《深入浅出设计模式(影印版)》的笔记-第339页 - Single Responsibility

Design Principle A class should have only one reason to change.
Cohesion is a term used as a measure of how closely a class or a module supports a single purpose or responsibility.
We say a module or class has high cohesion when it is designed around a set of related functions, and we say it has low cohesion when it is designed around a set of unrelated functions.

《深入浅出设计模式(影印版)》的笔记-第100页

Don't feel alone if you said "whoa" the first (and second and third) time you looked at this API.

《深入浅出设计模式(影印版)》的笔记-模式八:迭代器与组合模式 - 模式八:迭代器与组合模式

本章节中介绍了迭代器与组合模式,还引入了一个新的OO原则:类应该只有一个改变的理由。
一、迭代器模式定义:
提供一种方法顺序访问一个聚合对象中的多个元素,而又不暴露其内部的标识。
二、组合模式定义:
允许你将对象组成树形结构来表现“整体/部分”的层次结构。组合能让客户以一致的方式处理个别对象和对象组合。
三、新的OO原则:类应该只有一个改变的理由。类的每个责任都有改变的潜在区域。超过一个责任,意味着超过一个改变的区域。这个原则告诉我们,尽量让每个类保持单一责任。保持代码的内聚性。
本章节要点:
1、迭代器允许访问聚合的元素,而不需要暴露他的内部结构。
2、迭代器将遍历聚合的工作封装进一个对象中。
3、当使用迭代器的时候,我们依赖聚合提供遍历。
4、迭代器提供了一个通用的接口,让我们遍历聚合项,当我们编码使用聚合的项时,就可以使用多态机制。
5、我们应该努力让一个类只分配一个责任。
6、组合模式提供一个结构,可同时包容个别对象和组合对象。
7、组合模式允许客户对个别对象以及组合对象一视同仁。
8、组合结构内的任意对象称为组件,组件可以是组合,也可以是结点。
9、在实现组合模式时,有许多设计上的折中。你要根据需要平衡透明性和安全性。

《深入浅出设计模式(影印版)》的笔记-第32页 - Tools for your Design Toolbox

OO principles:
Encapsulate what varies.
“封装变化”是一条非常好的编程准则,在我看来不但适用于OO语言,也适用于面向过程的编程语言。例如可能会变化的代码封装到函数里面,当然,前提我们需要预见到哪些地方是可能会变化的,哪些地方不会变,我的经验是采用自顶向下的思考方法,先忽略细节,从高层次抽象问题的解决方法。

《深入浅出设计模式(影印版)》的笔记-模式六:命令模式 - 模式六:命令模式

一、定义:将请求封装成对象,这可以让你使用不同的请求、队列,或者日志请求来参数化其他对象,命令模式也可以支持撤销操作。
二、要点:
1、命令模式将发出请求的对象和执行请求的对象解耦。
2、在北解耦的两者之间是通过命令对象进行沟通的。命令对象封装了接受者和一个或一组动作。
3、调用者通过调用命令对象的execute()发出请求,这会使得调用者的动作被调用。
4、调用者可以接收命令但顾总参数,甚至在运行时动态地进行。
5、命令可以支持撤销,做法是实现一个undo()方法来回调到execute()被执行前的状态。
6、宏命令是命令的一种简单的延伸,允许调用多个命令。宏方法也可以支持撤销。
7、实际操作时,很常见使用“聪明”命令对象,也就是直接实现了请求,而不是将工作委托给接收者。但这种情况下,调用者和接受者之间的解耦程度相对较低,也不能把接收者当做参数传递给命令。
8、命令也可以用来实现日志和事务系统:某些应用需要我们将所有的动作都记录在日志中,并能在系统死机之后,重新调用这些动作恢复到之前的那个状态。通过新增加两个方法(store()和load()),命令模式就能够支持这一点。在java中,我们可以利用对象的序列化试下你这些方法,反一般认为序列化最好还是只用在对象的持久化上。
当我们执行命令的时候,将历史记录存储在磁盘中。一旦系统死机,我们就可以将命令对象重新加载,并成批地一次调用这些对象的execute()方法。

《深入浅出设计模式(影印版)》的笔记-模式四:工厂模式 - 模式四:工厂模式

一、OO新原则:依赖抽象,不要依赖具体类。
二、工厂模式章节主要讲解了三种不同的工厂:
1、简单工厂:简单工厂模式是将对象的创建者作为一个参数传入到使用的类当中,将类的创建和使用分开。简单工厂其实不是一个设计模式,反而比较像是一种编程习惯。经常被用于将对象的创建和使用分离开来。
2、抽象工厂模式:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
3、工厂方法模式:定义了一个创建对象的接口,但由子类决定是要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
三、要点:
1、所有的工厂都是用来封装对象的创建。
2、简单工厂,虽然不是真正的设计模式,但仍不失为一个简单的方法,可以讲客户程序从具体类解耦。
3、工厂方法是用继承:把对象的创建委托给子类,子类实现工厂方法来创建对象。
4、抽象工厂是用对象组合:对象的创建被实现在工厂接口所暴露出来的方法中。
5、所有工厂模式都通过减少应用程序和具体类之间的依赖促进松耦合。
6、工厂方法允许将实例化延迟到子类进行。
7、抽象工厂创建相关的对象家族,而不需要依赖它们的具体类。
8、依赖倒置原则,指导我们避免依赖具体类型,而要尽量依赖抽象。
9、工厂是很有威力的技巧,帮助我们针对抽象编程,而不要针对具体类编程。

《深入浅出设计模式(影印版)》的笔记-3 - 3

coffee example cost() function add ingredients
Design Principle
Classes should be open
for extension, but closed for modification.
Be careful when choosing the areas of code that need to be extended; applying the
Open-Closed Principle EVERYWHERE is wasteful, unnecessary, and can lead to complex, hard to understand code.
Java IO: use heavily Decorator pattern

《深入浅出设计模式(影印版)》的笔记-第180页 - Dealing with multithreading

public static synchronized Singleton getInstance();By adding the synchronized keyword before getInstance(), we force every thread to wait its turn before it can enter the method. That is, no two threads may entry the method at the same time.
……it looks fairly expensive to synchronize the getInstance() method. And the following is some way to fixed it.
1. Do nothing if the performance of getInstance() isn't critial to your application.
2. Move to an eagerly created instance rather than a lazily. public class Singleton {
private static Singleton uniqueInstance = new Singleton();
private Singleton() {}
public static Singleton getInstance() { return uniqueInstanc; }
}3. Use "double-checked locking" to reduce the use of synchronization in getInstance() public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance == null) {
synchronized (Singleton.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
}
}
return uniqueInstanc;
}
}

《深入浅出设计模式(影印版)》的笔记-第292页 - Hooked on Template Method

A hook is a method that is declared in the abstract class, but only given an empty or default implementation. This gives subclasses the ability to "hook into" the algorithm at various points, if they wish; a subclass is also free to ignore the hook.Hook method 在编程的过程中还是经常用到的,记得在写Fragments的baseActivityFragment的时候,声明过一个比较复杂的实现。
个人觉得,这个方法一个好的特点就是,提供一个修改父类特征的方法。一般来说我们是不能直接操控父类的method,更改其行为,但是,我们可以override该方法,这也是java里面一个很重要的特性运行时判定。

《深入浅出设计模式(影印版)》的笔记-第117页 - The Simple Factory defined

The Simple Factory isn't actually a Design Pattern; it's more of a programming idiom.

《深入浅出设计模式(影印版)》的笔记-第24页 - Speaking of Design Patterns

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

《深入浅出设计模式(影印版)》的笔记-第177页 - Singleton Pattern defined

The concise definition of the pattern:
> The Singleton Pattern ensures a class has only one instance, and provides global point of access to it.
> What's really going on here ? We're taking a class and letting it manager a single instance of itself. We're also preventing any other class form creating a new instance on its own. To get an instance, you've got to go through the class itself.
>We're also providing a global access point to the instance: whenever you need an instance, just query the class and it will hand you back the single instance. As you've seen, we can implement this so that the Singleton is created in a lazy manner, which is especially important for resource intensive objects.

《深入浅出设计模式(影印版)》的笔记-第131页 - It's finally time to meet the Factory Method Pattern

All factory patterns encapsulate object creation. The factory Method Pattern encapsulates object creation by letting subclasses decide what objects to create.

《深入浅出设计模式(影印版)》的笔记-第86页 - The Open-Closed Principle

Design Principle:
Classes should be open for extension, but closed for modification.
> Our goal is to allow classes to be easily extended to incorporate new behavior without modifying existing code. What do we get if we accomplish this ? Designs that are resilient to change and flexible enough to take on new functionality to meet changing requirements.

《深入浅出设计模式(影印版)》的笔记-第139页 - The Dependency Inversion Principle

Design Principle Depend upon abstraction. Do not depend upon concrete classes.
> The dependency Inversion Principle makes and stronger statement about abstraction. Its suggests that our high-level components should not depend on our low-level components; rather, they should both depend on abstraction. ( A "high-level" component is a class with behavior defined in terms of other, "low-level" components.)

《深入浅出设计模式(影印版)》的笔记-设计模式入门&策略模式 - 设计模式入门&策略模式

一、OO基础:
1、抽象
2、封装
3、多态
4、继承
二、设计原则:
1、封装变化:找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起。
2、针对接口编程,而不是针对实现编程。
3、多用组合,少用继承。
三、策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
四、设计模式入门要点:
要点:
1、知道OO基础,并不足以让你设计出良好的OO系统。
2、良好的OO涉及必须具备可复用,可扩充、可维护三个特性。
3、模式可以让我们建造出具有良好OO设计质量的系统。
4、模式被认为是历经验证的OO设计经验。
5、模式不是代码,而是针对设计问题的通用解决方案。你可把它们应用到特定的应用中。
6、大多数的模式和原则,都着眼于软件变化的主题。
7、大多数的模式都允许系统局部改变独立于其他部分。
8、我们常把系统中会变化的部分抽出来封装。
9、模式让开发人员之间有共享的语言,能够最大化沟通价值。

《深入浅出设计模式(影印版)》的笔记-第134页 - Factory Method Pattern defined

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.
> As with every factory, the Factory Method Pattern gives us a way to encapsulate the instantiations of concrete types. Looking at the class diagram below, you can see that the abstract Creator gives you an interface with a method for creating objects, also known as "factory method." Any other method implemented in the abstract Creator are written to operate on products produced by the factory method. Only subclasses actually implement the factory method and create products.

《深入浅出设计模式(影印版)》的笔记-第53页 - Design Principle

Design Principle: Strive for loosely coupled designs between objects that interact.
> Loosely coupled designs allow us to build flexible OO systems that can handle change because they minimize the interdependency between objects.

《深入浅出设计模式(影印版)》的笔记-第243页 - Adapter Pattern defined

The Adapter Pattern converts the interface of a class into another interface the clients expects. Adapter lets classes work together that couldn't otherwise because of incompatible.
> This acts to decouple the client from the implemented interface, and if we expect the interface to change over time, the adapter encapsulates that change so that the client doesn't have to modified each time it needs to operate against a different interface.
>Be Full of good OO design principles: check out the use of object composition to wrap the adaptee with an altered interface.
> Bind the client to an interface, not an implementation.

《深入浅出设计模式(影印版)》的笔记-第9页 - Design Principle

Identify the aspects of your application that vary and separate them from what stays the same. (确定您的应用程序中变化的方面,并将他们与不变的部分割开。)
Take what varies and "encapsulate" it so it won't affect the rest of your code.

《深入浅出设计模式(影印版)》的笔记-第460页 - The Proxy Pattern defined

本来打算这周看完MVC部分的,看来没时间了,只能等到周一看了!代理模式讲的东西很多,需要一段时间才能消化,总的来说,速度的看了一遍,只能有些模糊的印象,更不要说完全吃透理解了。还欠缺的很。下周找个时间,可以做一个代理模式的总结,后面只是简单提了几种代理模式,没有具体的案例,可以搜集些这方面的资料。
The Proxy Pattern provides a surrogate(代理) or placeholder for another object to control access to it.

《深入浅出设计模式(影印版)》的笔记-第158页 - Factory Method And Abstract Factory

Pattern Definitions:
> The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide whitch class to instantiate. Factory Method lets a class defer instantiation to subclasses.
> The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Different:
1. The way to decouple:
> Abstract Factory does it through object composition.
> Factory Method does it through object inheritance. (or subclasses)
2. The number of products
> Abstract Factory creates entire families of products.
> Factory Method creates one product.
Common:
> Both encapsulate object creation to keep applications loosely coupled and less dependent on implementation.
When to use:
>Abstract Factory: Whenever you have families of products you need to create and you want to make sure your clients create products that belong together.
>Factory Method: To decouple your client code from the concrete classes you need to instantiate, or if you don't know ahead of time all the concrete classes you are going to need.

《深入浅出设计模式(影印版)》的笔记-第100页

好吧,有了中文版就没再看它

《深入浅出设计模式(影印版)》的笔记-第51页 - The Observer Pattern defined

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
The Observer Pattern defined.
When two objects are loosely coupled, then can interact, but have very little knowledge of each other.
The Observer Pattern provides an object design where subjects and observers are loosely coupled.
>The only thing the subject knows about an observer is that it implements a certain interface (the Observer inerface).
> We cann add new observers at any time.
> We never need to modify the subject to add new types of observers.
>Changes to either the subject or an observer will not affect the other.

《深入浅出设计模式(影印版)》的笔记-模式七:模板方法模式 - 模式七:模板方法模式

本章节中介绍了一种最常用的模式:模板方法模式,还引入了一个新的OO原则:别找我,我会找你。
一、模板方法模式:
在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
二、别找我,我会找你:
由超累主控一切,当它们需要的时候,自然会去调用子类。这就跟好莱坞一样。
本章节要点:
1、“模板方法”定义了算法的步骤,把这些步骤的实现延迟到了子类。
2、模板方法模式为我们提供了一种代码复用的重要技巧。
3、模板方法的抽象类可以定义具体方法、抽象方法和钩子。
4、抽象方法由子类实现。
5、钩子是一种方法,它在抽象类中不做事,或者只做默认的事情,子类可以选择要不要去覆盖它。
6、为了防止子类改变模板方法中的算法,可以将模板方法声明为final。
7、好莱坞原则告诉我们,将决策权放在高层模块中,以便决定如何以及何时调用低层模块。
8、你将在真实世界代码中看到模板方法模式的许多变体,不要期待它们全部都是一眼就可以被你认出的。
9、策略模式和模板方法模式都封装算法,一个用组合,一个用继承。
10、工厂方法是模板方法的一个特殊版本。

《深入浅出设计模式(影印版)》的笔记-第411页 - The difference between the State Pattern and the Strategy Pattern

> In general, think of the Strategy Pattern as a flexible alternative to subclassing; if you are use inheritance to define the behavior of a class, then you're stuck with that behavior even if you need to change it. With strategy you can change the behavior by composing with a different object.
> Think of the State Pattern as an alternative to putting lots of conditionals in you context; by encapsulating the behaviors within objects, you can simply change the state object in context to change it behavior.

《深入浅出设计模式(影印版)》的笔记-模式二:观察者模式 - 模式二:观察者模式

定义:
在对象间定义一对多的依赖,这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新。
要点:
1、观察者模式定义了对象之间一对多的关系。
2、主题(也就是可观察者)用一个共同的接口来更新观察者。
3、观察者和可观察者之间用松耦合方式结合(loosecoupl-ing),可观察者不知道观察者的细节,只知道观察者实现了观察者接口。
4、使用此模式时,你可从被观察者出推(push)或拉(pull)数据(然而,推的方式被认为更“正确”)。
5、有多个观察者时,不可以依赖特定的通知顺序。
6、java有多个观察者模式的实现,包括了通用的java.util.Observable。
7、要注意java.util.Observable带来的一些问题。
8、如果有必要的话,可以实现自己的Observable,这并不难,不要害怕。
9、Swing大量使用观察者模式,许多GUI框架也是如此。
10、此模式也被应用在许多地方、例如:JavaBean、RMI。

《深入浅出设计模式(影印版)》的笔记-第11页 - Design Priciple

Design Principle: Program to an interface, not an implementation.
"Program to an interface" really means "Program to a super-type."
The point is to exploit polymorphism by programming to a supertype so that the actual runtime object isn't locked into the code. And we could rephrase "program to a supertype" as "the declared type of the variables should be a supertype, usually an abstract class or interface, so that the objects assigned to those variables can be of any concrete implementation of the supertype, which means the class declaring them doesn't have about the actual object types!

《深入浅出设计模式(影印版)》的笔记-第125页 - Declaring a factory method > Code Up Close

A factory method handles object creation and encapsulates it in a subclass. This decouples the client code in the super class from the object creation code in the subclass.
abstract Product factoryMethod(Arguments args)
> 1. A factory method is abstract, so the subclasses are counted on to handle object creation.
> 2. A factory method returns a Product that is typically used within methods defined in the supercalss.
> 3. A factory method isolates the client (the code in the superclass, like orderPizza()) from knowing what kind of concrete Product is actually created.
> 4. A factory method may be parameterized (or not) to select among several vaiations of a Product.

《深入浅出设计模式(影印版)》的笔记-四,工厂模式 - 四,工厂模式

简单的工厂模式也是将可扩展的行为类和可修改的实例类分离,具体的实例则继承可扩展的行为类,达到开闭原则。
依赖倒置原则:不要让高抽象层组件依赖低抽象层组件。

《深入浅出设计模式(影印版)》的笔记-第296页 - The Hollywood Principle

The Hollywood Principle Don't call us, we'll call you.
> The high-level components give the low-level components a "don't call us, we'll call you" treatment.
> The Hollywood Principle gives us a way to prevent "dependency rot". (依赖腐败)
依赖腐败的解释英文不好理解,下面是找到的中文翻译:
依赖腐败发生的时候是,
当高层组件依赖底层组件,而底层组件又(反过来)依赖高层组件,高层组件依赖边缘组件,而边缘组件又依赖底层组件。
不好理解!!!!
The connection between the Hollywood Principle and the Template Method Pattern is probably somewhat apparent: when we design with the Template Method Pattern, we're telling subclasses, "don't call us, we'll call you."

《深入浅出设计模式(影印版)》的笔记-第32页 - Tools for your Design Toolbox

OO Basics : Abstraction、Encapsulation、Polymorphism、Inheritance。
OO Principles: Encapsulate what varies. Favor composition over inheritance. Program to interfaces, not implementations.
OO Patterns > Strategy : defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

《深入浅出设计模式(影印版)》的笔记-第74页 - Tools for your Design Toolbox

OO Basics
> Abstraction、Ecapsulation、Polymorphsim、Inheritance.
OO Principles
> Encapsulate what varies.
> Favor composition over inheirtance.
> Program to interface, not implementation.
> Strive for loosely coupled designs between objects that interact.

《深入浅出设计模式(影印版)》的笔记-第264页 - Facade Pattern defined

The Facade Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.The Principle of Least Knowledge.
Design Principle: Principle of Least Knowledge - talk only to your immediate friends.
> In real terms, it means when you are designing a system, for any object, be careful of the number of classes it interacts with and also how it comes to interact with those classes.
> This principle prevents us from creating designs that have a large number of classes coupled together so that changes in one part of the system cascade to other parts. When you build a lot of dependencies between many classes, you are building a fragile system that will be costly to maintain and complex for others to understand.
> This principle provides some guidelines: take and object; now from any method in that object, the principle tell us that we should only invoke methods that belong to:
这个原则,就如何避免赢得朋友,并且不影响对象,给出了一些指导意见:对于任何一个对象,在该对象内的任何方法内部,我们都只能调用属于以下范围的方法:
> 1. 该对象本身的。(The object itself)
> 2. 当前方法中,被作为参数传入的对象的。(Objects passed in as a parameter to the method)
> 3. 当前方法构造或者实例化的任何对象。(Any object the method creates or instantiates.)
> 4. 该对象的任何组件的。(Any object of the object)
Without the Principlepublic float getTemp() {
Thermometer thermometer = station.getThermometer();
return thermometer.getTemperature();
}Here, first we should get the object of Thermometer with a name thermometer that acts as temp sub-part object. Then we use the sub-part object thermometer to get the getTemperature() method to get temperature. And this causes the result that: in current object domain method, we influence other object, and go against the least-knowledge principle.
With the Principle.public float getTemp() {
Thermometer thermometer = station.getThermometer();
return thermometer.getTemperature();
}After we apply the principle, we add a method to the station class that makes the request to the thermometer for us.

《深入浅出设计模式(影印版)》的笔记-第156页 - Abstract Factory Pattern defined

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
> The AbstractFactory defines the interface that all ConcreteFactories must implement, which consists of a set of methods for producing products.
> The client is written against the abstract factory and then composed at runtime with an actual factory.
> The concrete factories implement the different families To create a product, the client uses one of these factories, so it never has to instantiate a product a product object.


 深入浅出设计模式(影印版)下载 更多精彩书评


 

农业基础科学,时尚,美术/书法,绘画,软件工程/开发项目管理,研究生/本专科,爱情/情感,动漫学堂PDF下载,。 PDF下载网 

PDF下载网 @ 2024