Intent Monitor pattern is used to create thread-safe objects and prevent conflicts between threads in concurrent applications. Explanation In plain words Monitor pattern is used to enforce single-threaded access to data. Only one thread at a time is allowed to execute code within the monitor object. Wikipedia says In concurrent programming (also known as parallel programming), a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become false.
Intent The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. The application can register specific handlers for processing which are called by reactor on specific events. Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. Demultiplexing of service requests is performed by a synchronous event demultiplexer. Class diagram Applicability Use Reactor pattern when
Reader Writer Lock
Intent Regular lock does not distinguish the ‘read lock’ and ‘write lock’, as when access the data structure patterns consists of many threads reading the data, each thread will have to lock it which produces unnecessary serialization. The existence of reader-writer lock resolves this issue as it is well known as “multiple concurrent readers, single writer locks”, used to consist of multiple threads reading the data concurrently and allow only one thread to write or modify the data.
Thread Pool
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. Explanation Real-world example We have a large number of relatively short tasks at hand.
Data Locality
Intent Accelerate memory access by arranging data to take advantage of CPU caching. Modern CPUs have caches to speed up memory access. These can access memory adjacent to recently accessed memory much quicker. Take advantage of that to improve performance by increasing data locality keeping data in contiguous memory in the order that you process it. Class diagram Applicability Like most optimizations, the first guideline for using the Data Locality pattern is when you have a performance problem.
Dirty Flag
Also known as IsDirty pattern Intent To avoid expensive re-acquisition of resources. The resources retain their identity, are kept in some fast-access storage, and are re-used to avoid having to acquire them again. Class diagram Applicability Use the Dirty Flag pattern when Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead. Credits Design Patterns: Dirty Flag J2EE Design Patterns
Queue based load leveling
Intent Use a queue that acts as a buffer between a task and a service that it invokes in order to smooth intermittent heavy loads that may otherwise cause the service to fail or the task to time out. This pattern can help to minimize the impact of peaks in demand on availability and responsiveness for both the task and the service. Class diagram Applicability This pattern is ideally suited to any type of application that uses services that may be subject to overloading.
Spatial Partition
Intent As explained in the book Game Programming Patterns by Bob Nystrom, spatial partition pattern helps to efficiently locate objects by storing them in a data structure organized by their positions. Explanation Say, you are building a war game with hundreds, or maybe even thousands of players, who are clashing on the battle field. Each player’s position is getting updated every frame. The simple way to handle all interactions taking place on the field is to check each player’s position against every other player’s position:
Intent Ensure that a given client is not able to access service resources more than the assigned limit. Explanation Real-world example A young human and an old dwarf walk into a bar. They start ordering beers from the bartender. The bartender immediately sees that the young human shouldn’t consume too many drinks too fast and refuses to serve if enough time has not passed. For the old dwarf, the serving rate can be higher.