美文网首页
JDK源码中用到的设计模式

JDK源码中用到的设计模式

作者: huapro | 来源:发表于2019-05-25 17:58 被阅读0次

Creational patterns

Abstract factory (recognizeable by creational methods returning an abstract/interface type)

Builder (recognizeable by creational methods returning the instance itself)

Factory method (recognizeable by creational methods returning a concrete type)

Prototype (recognizeable by creational methods returning a different instance of itself with the same properties)

Singleton (recognizeable by creational methods returning the same instance (usually of itself) everytime)


Structural patterns

Adapter (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own/another abstract/interface type which decorates/overrides the given instance)

Bridge (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own abstract/interface type which delegates/uses the given instance)

  • None comes to mind yet. A fictive example would be new LinkedHashMap(LinkedHashSet<K>, List<V>) which returns an unmodifiable linked map which doesn’t clone the items, but uses them. The java.util.Collections#newSetFromMap()and singletonXXX() methods however comes close.

Composite (recognizeable by behavioral methods taking an instance of same abstract/interface type)

Decorator (recognizeable by creational methods taking an instance of same abstract/interface type)

Facade (recognizeable by behavioral methods which internally uses instances of different independent 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 turn delegates/uses adifferent implementation 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 in another implementation ofsame abstract/interface type in a queue)

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

Interpreter (recognizeable by behavioral methods returning a structurally different 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 a different type 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 the whole instance)

Observer (or Publish/Subscribe) (recognizeable by behavioral methods which invokes a method on an instance of anotherabstract/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 a differentabstract/interface type which has been passed-in as 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 two different abstract/interface types which has methods definied which takes each the other abstract/interface type; the one actually calls the method of the other and the other executes the desired strategy on it)

相关文章

  • JDK源码中用到的设计模式

    Creational patterns Abstract factory (recognizeable by cr...

  • JDK源码中的设计模式

    一、结构性模式: 1、适配器模式: 常用于将一个新接口适配旧接口 在我们业务代码中经常有新旧接口适配需求,可以采用...

  • JDK源码设计模式

    创建型模式 抽象工厂模式(通过创建的方法返回工厂本身, 可以依次创建另一个抽象/接口类型)javax.xml.pa...

  • Spring学习(五)AOP

    SpringAop使用到了了动态代理模式(有关设计模式见设计模式章节)。JDK动态代理代理的目标类必须要实现接口。...

  • 设计模式(一):基础

    参考: 网络blogJDK里的设计模式-coolshellstackoverflow-java JDK 类库中用到...

  • 你知道吗?Spring中都用到了那些设计模式?面试的时候慌了吗?

    JDK 中用到了那些设计模式?Spring 中用到了那些设计模式?这两个问题,在面试中比较常见。我在网上搜索了一下...

  • Spring 设计模式介绍

    JDK 中用到了那些设计模式?Spring 中用到了那些设计模式?这两个问题,在面试中比较常见。我在网上搜索了一下...

  • 「Java面试必会」单例模式

    JDK中源代码中有很多很好的设计模式,我们平时在工作中很少去看JDK源代码,更多的是去看一些框架的源码,比如说Sp...

  • 单例模式

    单例模式:jdk中的getRuntime();工厂方法模式:ThreadPoolExcutor用到ThreadFa...

  • JDK中的设计模式

    JDK中设计模式 本文主要是归纳了JDK中所包含的设计模式,包括作用和其设计类图。 首先来个总结,具体的某个模式可...

网友评论

      本文标题:JDK源码中用到的设计模式

      本文链接:https://www.haomeiwen.com/subject/yfuzzqtx.html