如何学习JDK里的设计模式

前言

最近在看JDK源码,想在毕业前再好好提高一下写代码的能力,JDK是个优秀的源码阅读范本(Spring的源码也不错)。JDK目录下的src.zip里可以直接获得源码,我也push到了我Github的一个repo里。

网上搜了JDK设计模式,coolshell里也有一篇,不过我还是参照了Stackoverflow(原文链接)上的一个“Examples of GoF Design Patterns”的答复,详细列举了JDK里具体对应的类和函数,并结合了wiki上的许多整理好的优秀词条内容。放到这个博客上,也方便自己深入学习,好好体会下设计模式内涵,光看书也会很枯燥,况且纸质的代码看起来也不舒服。

正文

You can find an overview of a lot of design patterns inWikipedia. It also mentions which patterns are mentioned by GoF. I'll sum them up here and try to assign as much as possible pattern implementations found in both the Java SE and Java EE API's.

Creational patterns

Abstract factory(recognizeable by creational methods returning the factory itself which in turn can be used to create another abstract/interface type)

Builder(recognizeable by creational methods returning the instance itself)

Factory method(recognizeable by creational methods returning an implementation of an abstract/interface type)

Prototype(recognizeable by creational methods returning adifferentinstance of itself with the same properties)

Singleton(recognizeable by creational methods returning thesameinstance (usually of itself) everytime)


Structural patterns

Adapter(recognizeable by creational methods taking an instance ofdifferentabstract/interface type and returning an implementation of own/another abstract/interface type whichdecorates/overridesthe given instance)

Bridge(recognizeable by creational methods taking an instance ofdifferentabstract/interface type and returning an implementation of own abstract/interface type whichdelegates/usesthe given instance)

  • None comes to mind yet. A fictive example would benew LinkedHashMap(LinkedHashSet<K>, List<V>)which returns an unmodifiable linked map which doesn't clone the items, butusesthem. Thejava.util.Collections#newSetFromMap()andsingletonXXX()methods however comes close.

Composite(recognizeable by behavioral methods taking an instance ofsameabstract/interface type into a tree structure)

Decorator(recognizeable by creational methods taking an instance ofsameabstract/interface type which adds additional behaviour)

Facade(recognizeable by behavioral methods which internally uses instances ofdifferentindependent abstract/interface types)

Flyweight(recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)

Proxy(recognizeable by creational methods which returns an implementation of given abstract/interface type which in turndelegates/usesadifferentimplementation of given abstract/interface type)

The Wikipedia example is IMHO a bit poor, lazy loading has actually completely nothing to do with the proxy pattern at all.


Behavioral patterns

Chain of responsibility(recognizeable by behavioral methods which (indirectly) invokes the same method inanotherimplementation ofsameabstract/interface type in a queue)

Command(recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of adifferentabstract/interface type which has beenencapsulatedby the command implementation during its creation)

Interpreter(recognizeable by behavioral methods returning astructurallydifferent instance/type of the given instance/type; note that parsing/formatting is not part of the pattern, determining the pattern and how to apply it is)

Iterator(recognizeable by behavioral methods sequentially returning instances of adifferenttype from a queue)

Mediator(recognizeable by behavioral methods taking an instance of different abstract/interface type (usually using the command pattern) which delegates/uses the given instance)

Memento(recognizeable by behavioral methods which internally changes the state of thewholeinstance)

Observer (or Publish/Subscribe)(recognizeable by behavioral methods which invokes a method on an instance ofanotherabstract/interface type, depending on own state)

State(recognizeable by behavioral methods which changes its behaviour depending on the instance's state which can be controlled externally)

Strategy(recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of adifferentabstract/interface type which has beenpassed-inas method argument into the strategy implementation)

Template method(recognizeable by behavioral methods which already have a "default" behaviour definied by an abstract type)

Visitor(recognizeable by twodifferentabstract/interface types which has methods definied which takes each theotherabstract/interface type; the one actually calls the method of the other and the other executes the desired strategy on it)


(全文完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值