trampoline


layout: pattern
title: Trampoline
folder: trampoline
permalink: /patterns/trampoline/
categories: Behavioral
tags:

– Performance

Intent

Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack
and to interleave the execution of functions without hard coding them together
It is possible by representing a computation in one of 2 states : done | more
(completed with result, or a reference to the reminder of the computation,
something like the way a java.util.Supplier does).

Explanation

Trampoline pattern allows to define recursive algorithms by iterative loop.

Class diagram

alt text

Applicability

Use the Trampoline pattern when

  • For implementing tail recursive function. This pattern allows to switch on a stackless operation.
  • For interleaving the execution of two or more functions on the same thread.

Known uses(real world examples)

  • Trampoline refers to using reflection to avoid using inner classes, for example in event listeners.
    The time overhead of a reflection call is traded for the space overhead of an inner class.
    Trampolines in Java usually involve the creation of a GenericListener to pass events to an outer class.

Tutorials

Credits

tolerant-reader


layout: pattern
title: Tolerant Reader
folder: tolerant-reader
permalink: /patterns/tolerant-reader/
categories: Integration
tags:

– Decoupling

Intent

Tolerant Reader is an integration pattern that helps creating
robust communication systems. The idea is to be as tolerant as possible when
reading data from another service. This way, when the communication schema
changes, the readers must not break.

Class diagram

alt text

Applicability

Use the Tolerant Reader pattern when

  • The communication schema can evolve and change and yet the receiving side should not break

Credits

tls


layout: pattern
title: Thread Local Storage
folder: tls
permalink: /patterns/tls/
categories: Idiom
tags:

– Performance

Intent

Securing variables global to a thread against being spoiled by other threads. That is needed if you use class variables or static variables in your Callable object or Runnable object that are not read-only.

Class diagram

alt text

Applicability

Use the Thread Local Storage in any of the following situations

  • When you use class variables in your Callable / Runnable object that are not read-only and you use the same Callable instance in more than one thread running in parallel.
  • When you use static variables in your Callable / Runnable object that are not read-only and more than one instances of the Callable / Runnable may run in parallel threads.

throttling


layout: pattern
title: Throttling
folder: throttling
permalink: /patterns/throttling/
categories: Behavioral
tags:

– Performance

Intent

Ensure that a given client is not able to access service resources more than the assigned limit.

Class diagram

alt text

Applicability

The Throttling pattern should be used:

  • When a service access needs to be restricted to not have high impacts on the performance of the service.
  • When multiple clients are consuming the same service resources and restriction has to be made according to the usage per client.

thread-pool


layout: pattern
title: Thread Pool
folder: thread-pool
permalink: /patterns/thread-pool/
categories: Concurrency
tags:

– Performance

Intent

It is often the case that tasks to be executed are short-lived and
the number of tasks is large. Creating a new thread for each task would make
the system spend more time creating and destroying the threads than executing
the actual tasks. Thread Pool solves this problem by reusing existing threads
and eliminating the latency of creating new threads.

Class diagram

alt text

Applicability

Use the Thread Pool pattern when

  • You have a large number of short-lived tasks to be executed in parallel

template-method


layout: pattern
title: Template method
folder: template-method
permalink: /patterns/template-method/
categories: Behavioral
tags:

– Gang of Four

Intent

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.

To make sure that subclasses don’t override the template method, the template method should be declared final.

Class diagram

alt text

Applicability

The Template Method pattern should be used

  • To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary
  • When common behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is good example of “refactoring to generalize” as described by Opdyke and Johnson. You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations
  • To control subclasses extensions. You can define a template method that calls “hook” operations at specific points, thereby permitting extensions only at those points

Tutorial

Real world examples

  • javax.servlet.GenericServlet.init:
    Method GenericServlet.init(ServletConfig config) calls the parameterless method GenericServlet.init() which is intended to be overridden in subclasses.
    Method GenericServlet.init(ServletConfig config) is the template method in this example.

Credits

subclass-sandbox


layout: pattern
title: Subclass Sandbox
folder: subclass-sandbox
permalink: /patterns/subclass-sandbox/
categories: Behavioral
tags:

– Game programming

Intent

The subclass sandbox pattern describes a basic idea, while not having a lot of detailed mechanics. You will need the pattern when you have several similar subclasses. If you have to make a tiny change, then change the base class, while all subclasses shouldn’t have to be touched. So the base class has to be able to provide all of the operations a derived class needs to perform.

Class diagram

alt text

Applicability

The Subclass Sandbox pattern is a very simple, common pattern lurking in lots of codebases, even outside of games. If you have a non-virtual protected method laying around, you’re probably already using something like this. Subclass Sandbox is a good fit when:

  • You have a base class with a number of derived classes.
  • The base class is able to provide all of the operations that a derived class may need to perform.
  • There is behavioral overlap in the subclasses and you want to make it easier to share code between them.
  • You want to minimize coupling between those derived classes and the rest of the program.

Credits

strategy


layout: pattern
title: Strategy
folder: strategy
permalink: /patterns/strategy/
categories: Behavioral
tags:

– Gang of Four

Also known as

Policy

Intent

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

Class diagram

alt text

Applicability

Use the Strategy pattern when

  • Many related classes differ only in their behavior. Strategies provide a way to configure a class either one of many behaviors
  • You need different variants of an algorithm. for example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms
  • An algorithm uses data that clients shouldn’t know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures
  • A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class

Tutorial

Credits

step-builder


layout: pattern
title: Step Builder
folder: step-builder
permalink: /patterns/step-builder/
categories: Creational
tags:

– Instantiation

Intent

An extension of the Builder pattern that fully guides the user through the creation of the object with no chances of confusion.
The user experience will be much more improved by the fact that he will only see the next step methods available, NO build method until is the right time to build the object.

Class diagram

alt text

Applicability

Use the Step Builder pattern when the algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled the construction process must allow different representations for the object that’s constructed when in the process of constructing the order is important.

Credits

state


layout: pattern
title: State
folder: state
permalink: /patterns/state/
categories: Behavioral
tags:

– Gang of Four

Also known as

Objects for States

Intent

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

Class diagram

alt text

Applicability

Use the State pattern in either of the following cases

  • An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state
  • Operations have large, multipart conditional statements that depend on the object’s state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object’s state as an object in its own right that can vary independently from other objects.

Real world examples

Credits

粤ICP备15017284号