Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 技术 » 

Head First设计模式笔记(1)

《Head First设计模式》(深入浅出设计模式,Head First Design Patterns)是一本适合初学者学习设计模式的好书,情景丰富,简单易懂。
本文整理书中涉及到的各个设计原则及模式,仅供需要时参考。具体分为两个部分,本文主要记录设计原则和模式简介。

  1. Head First设计模式笔记(1):设计原则和模式简介
  2. Head First设计模式笔记(2):设计模式要点

模式简介

The one constant in software development. No matter how well you design an application, over time an application must grow and change or it will die.
模式是在某情景下,针对某问题的某种解决方案。
A Pattern is a solution to a problem in a context.
The context is the situation in which the pattern applies. This should be a recurring situation.
The problem refers to the goal you are trying to achieve in this context, but it also refers to any constraints that occur in the context.
The solution is what you’re after: a general design that anyone can apply which resolves the goal and set of constraints.
“If you find yourself in a context with a problem that has a goal that is affected by a set of constraints, then you can apply a design that resolves the goal and constraints and leads to a solution.”

设计原则(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. The result? Fewer unintended consequences from code changes and more flexibility in your systems!
Here’s another way to think about this principle: 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.
As simple as this concept is, it forms the basis for almost every design pattern. All patterns provide a way to let some part of a system vary independently of all other parts.

针对接口编程,而不是针对实现编程。
Program to an interface, not an implementation.
The word interface is overloaded here. There’s the concept of interface, but there’s also the Java construct interface. You can program to an interface, without having to actually use a Java interface. 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 to know about the actual object types!”

多用组合,少用继承。
Favor composition over inheritance.
Creating systems using composition gives you a lot more flexibility. Not only does it let you encapsulate a family of algorithms into their own set of classes, but it also lets you change behavior at runtime as long as the object you’re composing with implements the correct behavior interface.

为了交互对象之间的松耦合设计而努力。
Strive for loosely coupled designs between objects that interact.
When two objects are loosely coupled, they can interact, but have very little knowledge of each other. The Observer Pattern provides an object design where subjects and observers are loosely coupled.
Loosely coupled designs allow us to build flexible OO systems that can handle change because they minimize the interdependency between objects.

类应该对扩展开放,对修改关闭。
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 fl exible enough to take on new functionality to meet changing requirements.

(依赖倒置原则)要依赖抽象,不要依赖具体类。
(The Dependency Inversion 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 tronger 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.

(最少知识原则)只和你的密友谈话。
(The Principle of Least Knowledge)Principle of Least Knowledge talk only to your immediate friends.
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.

(好莱坞原则)别调用(打电话给)我们,我们会调用(打电话给)你。
(The Hollywood Principle)Don’t call us, we’ll call you.
With the Hollywood Principle, we allow low-level components to hook themselves into a system, but the high-level components determine when they are needed, and how. In other words, the high-level components give the low-level components a “don’t call us, we’ll call you” treatment.

(单一责任原则)一个类应该只有以一个引起变化的原因
(Single Responsibility)A class should have only one reason to change.
Every responsibility of a class is an area of potential change. More than one responsibility means more than one area of change.
This principle guides us to keep each class to a single responsibility.

一句话模式

Decorator: Wraps an object to provide new behavior.
State: Encapsulates state-based behaviors and uses delegation to switch between behaviors.
Iterator: Provides a way to traverse a collection of objects without exposing its implementation.
Facade: Simplifies the interface of a set of classes.
Strategy: Encapsulates interchangeable behaviors and uses delegation to decide which one to use.
Proxy: Wraps an object to control access to it.
Factory Method: Subclasses decide which concrete classes to create.
Adapter: Wraps an object and provides a different interface to it.
Observer: Allows objects to be notified when state changes.
Template Method: Subclasses decide how to implement steps in an algorithm.
Composite: Clients treat collections of objects and individual objects uniformly.
Singleton: Ensures one and only object is created.
Abstract Factory: Allows a client to create families of objects without specifying their concrete classes.
Command: Encapsulates a request as an object.

Note

关于多态,一般是定义父类型的指针变量,然后可以指向其不同的子类的对象,可以用这个指针调用不同的子类的方法,实现了“一个接口,多种方法”。
一般函数的调用在编译器编译期间就确定了函数的调用地址,并生产代码,属于静态绑定。而这种函数调用需要在运行时才能确定函数调用的地址,称为动态绑定。

C++的多态需要用虚函数实现,一个简单例子如下,相同的方法调用a->f()产生了不同的运行结果。

#include <iostream>
using namespace std;

class A {public: virtual void f(){cout<<"A"<<endl;}};
class A1:public A{public: void f(){cout<<"A1"<<endl;}};
class A2:public A{public: void f(){cout<<"A2"<<endl;}};

int main()
{
	A *a;
	a = new A();  a->f();	//输出 A
	a = new A1(); a->f();	//输出 A1
	a = new A2(); a->f();	//输出 A2
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论