visitor


layout: pattern
title: Visitor
folder: visitor
permalink: /patterns/visitor/
categories: Behavioral
tags:

– Gang of Four

Intent

Represent an operation to be performed on the elements of an object
structure. Visitor lets you define a new operation without changing the classes
of the elements on which it operates.

Class diagram

alt text

Applicability

Use the Visitor pattern when

  • An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes
  • Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid “polluting” their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them
  • The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it’s probably better to define the operations in those classes

Real world examples

Credits

value-object


layout: pattern
title: Value Object
folder: value-object
permalink: /patterns/value-object/
categories: Creational
tags:

– Instantiation

Intent

Provide objects which follow value semantics rather than reference semantics.
This means value objects’ equality are not based on identity. Two value objects are
equal when they have the same value, not necessarily being the same object.

Class diagram

alt text

Applicability

Use the Value Object when

  • You need to measure the objects’ equality based on the objects’ value

Real world examples

Credits

update-method


layout: pattern
title: Update Method
folder: update-method
permalink: /patterns/update-method/
categories: Behavioral
tags:

– Game programming

Intent

Update method pattern simulates a collection of independent objects by telling each to process one frame of behavior at a time.

Explanation

The game world maintains a collection of objects. Each object implements an update method that simulates one frame of the object’s behavior. Each frame, the game updates every object in the collection.

To learn more about how the game loop runs and when the update methods are invoked, please refer to Game Loop Pattern.

Class diagram

alt text

Applicability

If the Game Loop pattern is the best thing since sliced bread, then the Update Method pattern is its butter. A wide swath of games featuring live entities that the player interacts with use this pattern in some form or other. If the game has space marines, dragons, Martians, ghosts, or athletes, there’s a good chance it uses this pattern.

However, if the game is more abstract and the moving pieces are less like living actors and more like pieces on a chessboard, this pattern is often a poor fit. In a game like chess, you don’t need to simulate all of the pieces concurrently, and you probably don’t need to tell the pawns to update themselves every frame.

Update methods work well when:

  • Your game has a number of objects or systems that need to run simultaneously.
  • Each object’s behavior is mostly independent of the others.
  • The objects need to be simulated over time.

Credits

unit-of-work


layout: pattern
title: Unit Of Work
folder: unit-of-work
permalink: /patterns/unit-of-work/

categories: Architectural
tags:

– Data access

Intent

When a business transaction is completed, all the these updates are sent as one
big unit of work to be persisted in a database in one go so as to minimize database trips.

Class diagram

alt text

Applicability

Use the Unit Of Work pattern when

  • To optimize the time taken for database transactions.
  • To send changes to database as a unit of work which ensures atomicity of the transaction.
  • To reduce number of database calls.

Credits

typeobjectpattern


layout: pattern
title: Type-Object
folder: typeobjectpattern
permalink: /patterns/typeobjectpattern/
categories: Behavioral
tags:

  • Game programming

– Extensibility

Intent

As explained in the book Game Programming Patterns by Robert Nystrom, type object pattern helps in

Allowing flexible creation of new “classes” by creating a single class, each instance of which represents a different type of object

Explanation

Say, we are working on a game which has a hero and many monsters which are going to attack the hero. These monsters have certain attributes like attack, points etc. and come in different ‘breeds’ like zombie or ogres. The obvious answer is to have a base Monster class which has some fields and methods, which may be overriden by subclasses like the Zombie or Ogre class. But as we continue to build the game, there may be more and more breeds of monsters added and certain attributes may need to be changed in the existing monsters too. The OOP solution of inheriting from the base class would not be an efficient method in this case.
Using the type-object pattern, instead of creating many classes inheriting from a base class, we have 1 class with a field which represents the ‘type’ of object. This makes the code cleaner and object instantiation also becomes as easy as parsing a json file with the object properties.

Class diagram

alt text

Applicability

This pattern can be used when:

  • We don’t know what types we will need up front.
  • We want to be able to modify or add new types without having to recompile or change code.
  • Only difference between the different ‘types’ of objects is the data, not the behaviour.

Credits

twin


layout: pattern
title: Twin
folder: twin
permalink: /patterns/twin/
categories: Structural
tags:

– Extensibility

Intent

Twin pattern is a design pattern which provides a standard solution to simulate multiple
inheritance in java

Class diagram

alt text

Applicability

Use the Twin idiom when

  • To simulate multiple inheritance in a language that does not support this feature.
  • To avoid certain problems of multiple inheritance such as name clashes.

Credits

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.