Also known as Given/When/Then Intent Arrange/Act/Assert (AAA) is a pattern for organizing unit tests. It breaks tests down into three clear and distinct steps: Arrange: Perform the setup and initialization required for the test. Act: Take action(s) required for the test. Assert: Verify the outcome(s) of the test. Explanation This pattern has several significant benefits. It creates a clear separation between a test’s setup, operations, and results. This structure makes the code easier to read and understand.
Intent Callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. Explanation Real world example We need to be notified after executing task has finished. We pass a callback method for the executor and wait for it to call back on us. In plain words Callback is a method passed to the executor which will be called at defined moment.
Collecting Parameter
Name Collecting Parameter Intent To store the collaborative result of numerous methods within a collection. Explanation Real-world example Within a large corporate building, there exists a global printer queue that is a collection of all the printing jobs that are currently pending. Various floors contain different models of printers, each having a different printing policy. We must construct a program that can continually add appropriate printing jobs to a collection, which is called the collecting parameter.
Also known as Composition pattern Intent The functional pattern representing a style of organizing libraries centered around the idea of combining functions. Putting it simply, there is some type T, some functions for constructing “primitive” values of type T, and some “combinators” which can combine values of type T in various ways to build up more complex values of type T. Explanation Real world example In computer science, combinatory logic is used as a simplified model of computation, used in computability theory and proof theory.
Double Checked Locking
Intent Reduce the overhead of acquiring a lock by first testing the locking criterion (the “lock hint”) without actually acquiring the lock. Only if the locking criterion check indicates that locking is required does the actual locking logic proceed. Class diagram Applicability Use the Double Checked Locking pattern when there is a concurrent access in object creation, e.g. singleton, where you want to create single instance of the same class and checking if it’s null or not maybe not be enough when there are two or more threads that checks if instance is null or not.
Double Dispatch
Intent Double Dispatch pattern is a way to create maintainable dynamic behavior based on receiver and parameter types. Class diagram Applicability Use the Double Dispatch pattern when the dynamic behavior is not defined only based on receiving object’s type but also on the receiving method’s parameter type. Real world examples ObjectOutputStream
Execute Around
Intent Execute Around idiom frees the user from certain actions that should always be executed before and after the business method. A good example of this is resource allocation and deallocation leaving the user to specify only what to do with the resource. Explanation Real-world example A class needs to be provided for writing text strings to files. To make it easy for the user, the service class opens and closes the file automatically.
Lazy Loading
Intent Lazy loading is a design pattern commonly used to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program’s operation if properly and appropriately used. Class diagram Applicability Use the Lazy Loading idiom when eager loading is expensive or the object to be loaded might not be needed at all Real world examples JPA annotations @OneToOne, @OneToMany, @ManyToOne, @ManyToMany and fetch = FetchType.
Mute Idiom
Intent Provide a template to suppress any exceptions that either are declared but cannot occur or should only be logged; while executing some business logic. The template removes the need to write repeated try-catch blocks. Class diagram Applicability Use this idiom when an API declares some exception but can never throw that exception eg. ByteArrayOutputStream bulk write method. you need to suppress some exception just by logging it, such as closing a resource.