X.d 笔记

小Web,大世界

0%

在各种框架中经常看到一些简便的或且表达式取值,比如:

var a = xx && obj.xx && win.xxx;
var b = xx || obj.xx || win.xxx;

这是什么意思呢,用过的人都知道,对于javascript而言,不一定是boolean才可以判断的,所有对象都可以判断,包括undefined,null等

以下是对JS6种类型的判

类型 条件判断为
Function True
Undefined False
String 空字符串为false,否则为true
number 0为false,其它为true
boolean true/false
object null为flase,不为空为true

这样就可以看到这个意思了

比如:

阅读全文 »

观察者模式貌似就是传说中的Listener吧~

观察者模式 Observer

观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使他们能够自动更新自己。

问题:

A large monolithic design does not scale well as new graphing or monitoring requirements are levied.

意图:

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • Encapsulate the core (or common or engine) components in a Subject abstraction, and the variable (or optional or user interface) components in an Observer hierarchy.
  • The “View” part of Model-View-Controller.
阅读全文 »

说的通俗点,就是备份,与一键GOHST备份不同,备忘录模式只备份数据,不备份程序本身哦~

备忘录模式 Memento

备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捉住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。

问题:

Need to restore an object back to its previous state (e.g. “undo” or “rollback” operations).

意图:

  • Without violating encapsulation, capture and externalize an object’s internal state so that the object can be returned to this state later.
  • A magic cookie that encapsulates a “check point” capability.
  • Promote undo or rollback to full object status.
阅读全文 »

一般称中介者模式会更好会意一点,就是那个收集信息,然后搞介绍再收服务费的那玩意。

调停者模式 Mediator

题义:调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显作用。从而使他们可以松散偶合。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用。保证这些作用可以彼此独立的变化。调停者模式将多对多的相互作用转化为一对多的相互作用。调停者模式将对象的行为和协作抽象化,把对象在小尺度的行为上与其他对象的相互作用分开处理。

问题:

We want to design reusable components, but dependencies between the potentially reusable pieces demonstrates the “spaghetti code” phenomenon (trying to scoop a single serving results in an “all or nothing clump”).

意图:

  • Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
  • Design an intermediary to decouple many peers.
  • Promote the many-to-many relationships between interacting peers to “full object status”.
阅读全文 »

好像很多语言里面都有Iterator接口什么的,如果hasNext()方法返回true时,就可以用next()方法取对象了。

迭代器模式 Iterator

题义:迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。多个对象聚在一起形成的总体称之为聚集,聚集对象是能够包容一组对象的容器对象。迭代子模式将迭代逻辑封装到一个独立的子对象中,从而与聚集本身隔开。迭代子模式简化了聚集的界面。每一个聚集对象都可以有一个或一个以上的迭代子对象,每一个迭代子的迭代状态可以是彼此独立的。迭代算法可以独立于聚集角色变化。

问题:

Need to “abstract” the traversal of wildly different data structures so that algorithms can be defined that are capable of interfacing with each transparently.

意图:

  • Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  • The C++ and Java standard library abstraction that makes it possible to decouple collection classes and algorithms.
  • Promote to “full object status” the traversal of a collection.
  • Polymorphic traversal
阅读全文 »

收到个小需求,要求在页面上给个功能“复制”,一般网上使用flash复制API就够了,但这里要图片也要复制进去。
虽然是相当的无意义,但还是找了点资料在弄了,只支持IE,小小记录一下。

把这段代码复制到HTML里面就行了,可以图片和文字一起复制,然后在QQ窗口或WORD中CTRL+V,就可以贴入了。
缺点 是只支持IE,还是会弹个需要用户允许访问剪切板的提示。

阅读全文 »

解释器嘛,就是个翻译,比如有道词典什么的,哈哈。

解释器模式 Interpreter

题义:给定一个语言后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器。客户端可以使用这个解释器来解释这个语言中的句子。解释器模式将描述怎样在有了一个简单的文法后,使用模式设计解释这些语句。在解释器模式里面提到的语言是指任何解释器对象能够解释的任何组合。在解释器模式中需要定义一个代表文法的命令类的等级结构,也就是一系列的组合规则。每一个命令对象都有一个解释方法,代表对命令对象的解释。命令对象的等级结构中的对象的任何排列组合都是一个语言。

问题:

A class of problems occurs repeatedly in a well-defined and well-understood domain. If the domain were characterized with a “language”, then problems could be easily solved with an interpretation “engine”.

意图:

  • Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
  • Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design.
阅读全文 »

今天忽然看到几篇博客,一个说 Servlet 是单例的,一个说不是单例的,于是自己试下。

废话不说上代码:

web.xml

 <servlet>
  <servlet-name>Test</servlet-name>
  <servlet-class>com.xdnote.test.TestServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/test</url-pattern>
 </servlet-mapping>
阅读全文 »