X.d 笔记

小Web,大世界

0%

将抽象部分与它的实现部分分离,使它们都可以独立地变化。

桥接模式 Bridge

问题:

“Hardening of the software arteries” has occurred by using subclassing of an abstract base class to provide alternative implementations. This locks in compile-time binding between interface and implementation. The abstraction and implementation cannot be independently extended or composed..

意图:

  • Decouple an abstraction from its implementation so that the two can vary independently.
  • Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy.
  • Beyond encapsulation, to insulation.
阅读全文 »

将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.

适配器模式

问题:

An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed.

意图:

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
  • Wrap an existing class with a new interface.
  • Impedance match an old component to a new system.
阅读全文 »

在JavaScript中,通常使用prototype关键字实现面向对象。但在设计模式中,简单点说就是复制。其实入门时学习的那个什么排序,用一个temp变量复制另一个值用于对换也是一种相当迷你的原型模式。

原型模式 Prototype

问题:

Application “hard wires” the class of object to create in each “new” expression.

意图:

  • Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
  • Co-opt one instance of a class for use as a breeder of all future instances.
  • The new operator considered harmful.
阅读全文 »

如果一个对象由多个元素组成,而且这些元素可组成不同的对象,用构造者模式去分离出这个对象的构造过程。

构造者模式 Builder

问题

An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.

意图:

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets.
阅读全文 »

单例模式,最大使用,最大范围,什么,你的项目没用?功能主治:大量减小new运算,降低性能开销。

单例模式 Singleton

问题:

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

意图:

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets
阅读全文 »

工厂模式可以理解为简单工厂的修复版,增加要生产的对象的时候可以不用修改的你工厂,只用增加相应的生产类就行了,符合设计模式的那个什么什么规范。

工厂方法模式 Factory Method

问题:

A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own domain objects and provide for their instantiation.

意图:

  • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
  • Defining a “virtual” constructor.
  • The new operator considered harmful.
阅读全文 »

简单工厂太简单了,其它并不是GOF提到的23种模式里面的。只是都喜欢这么叫,哈哈。

前言

之前看到GOF的《设计模式 可复用面向对象软件的基础》 ,看的时候觉得思想不错,但一直没有手支练下,今天回想起来,好像写的很多东西都可以在里面找到其思想。所以就重温下,顺便记下笔记。书上的例子都是c写的,由于现在工作中主要写Java代码较多,就用Java记录下。

简单工厂模式 Simple Factory

我先申明一下:

每种设计模式都有它的使用场景,我不会在这里去解释,只把原文贴出来,一是本人语言水平差,那些定理类的话很难通俗说出来,二是建议不要被这种定理蒙蔽思想,你去学习设计模式是写代码实战的,不是去考证书的。这种程度的代码,自己敲一次去运行分析一下,就什么都懂了。

另外,做完了之后记得去看下UML类图,网上一大把,我这里也不重画了,当然你自己做的时候就可以在纸上画。

问题:

If an application is to be portable, it needs to encapsulate platform dependencies. These “platforms” might include: windowing system, operating system, database, etc. Too often, this encapsulatation is not engineered in advance, and lots of #ifdef case statements with options for all currently supported platforms begin to procreate like rabbits throughout the code.

意图:

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • A hierarchy that encapsulates: many possible “platforms”, and the construction of a suite of “products”.
  • The new operator considered harmful.
阅读全文 »

小小汇总一下,目前个人遇到的,有很多网上说到的太多的就不记了,那些肯定是对的而且遇到的更多,但哪都能看到基本都熟了。

判定一个变量是否存在,如果存在执行A,不存在执行B

一般使用的方法是

typeof(xxx) != 'undefined'?statementA:statementB;

感觉每次都写 typeof(xxx) != 'undefined' 在前面即不爽又难看,在js里面undefined不是false么于是改为

xxx?statementA:statementB;

可是又报错了:ReferenceError: xxx is not defined,其实可以改成这样.

windown.xxx?statementA:statementB;

说明:如果 if(xxx)可能会报ReferenceError: xxx is not defined的错误时,可以使用 if(window.xxx)

阅读全文 »