Examples of GoF Design Patterns in Java's core libraries

I listed out some good examples of these Design Patterns in Java's core libraries.

Creational patterns

  • Creational patterns involve object instantiation and all provide a way to decouple a client from the objects it needs to instantiate.
  • This pattern can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation effectively to get the job done.

Abstract factory

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Builder

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Factory method

Define an interface for creating an object, but let subclasses decide which class
to instantiate. Factory Method lets a class defer instantiation to subclasses.

Prototype

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Singleton

Ensure a class only has one instance, and provide a global point of access to it.

Structural patterns

Structural pattern let's compose classes and objects into larger structures.

Adapter

Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Bridge 

Decouple an abstraction from its implementation so that the two can vary independently.
  • 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 usesthem. The java.util.Collections#newSetFromMap() and singletonXXX() methods however comes close.

Composite

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Decorator

Facade

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Flyweight

Use sharing to support large numbers of fine-grained objects efficiently.

Proxy

Provide a surrogate or placeholder for another object to control access to it.

Behavioral patterns

  • Behavioral pattern is concerned with how classes and objects interact and distribute responsibility.
  • These design patterns are all about Class's objects communication. Behavioral patterns are those patterns that are most specifically concerned with communication between objects

Chain of responsibility

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Command

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Interpreter

Iterator

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Mediator 

Memento 

Observer

State

Allow an object to alter its behaviour when its internal state changes. The object will appear to change its class.

Strategy

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.

Template method

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Visitor

References

Comments

  1. Great post with real world java core library examples.

    ReplyDelete

Post a Comment