🤖📘🍻 Hooray! After 3 years of work, we've finally released a new ebook on design patterns! Check it out »

101 Design Patterns & Tips for Developers

Creational Patterns
Abstract Factory
The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes (more...)
The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations (more...)
Factory Method
The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate (more...)
Object Pool
Object pools are used to manage the object caching and it can offer a significant performance boost (more...)
The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself (more...)
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance (more...)
Structural Patterns
The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients (more...)
The Bridge pattern decouples an abstraction from its implementation, so that the two can vary independently (more...)
The Composite composes objects into tree structures and lets clients treat individual objects and compositions uniformly (more...)
The Decorator attaches additional responsibilities to an object dynamically. Decorator pattern suggests giving the client the ability to specify whatever combination of "features" is desired (more...)
The Facade defines a unified, higher level interface to a subsystem that makes it easier to use (more...)
The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost (more...)
Private Class Data
The private class data design pattern seeks to reduce exposure of attributes by limiting their visibility (more...)
The Proxy provides a surrogate or place holder to provide access to an object (more...)
Behavioral Patterns
Chain of Responsibility
Chain of Responsibility chains the receiving objects together, and then passes any request messages from object to object until it reaches an object capable of handling the message (more...)
The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parameterized with different requests (more...)
The Intepreter pattern defines a grammatical representation for a language and an interpreter to interpret the grammar (more...)
The Iterator provides ways to access elements of an aggregate object sequentially without exposing the underlying structure of the object. (more...)
The Mediator defines an object that controls how a set of objects interact. Loose coupling between colleague objects is achieved by having colleagues communicate with the Mediator, rather than with each other (more...)
The Memento captures and externalizes an object's internal state so that the object can later be restored to that state (more...)
Null Object
The Null Object encapsulates the absence of an object by providing a substitutable alternative that offers suitable default "do nothing" behavior (more...)
The Observer defines a one-to-many relationship so that when one object changes state, the others are notified and updated automatically (more...)
The State pattern allows an object to change its behavior when its internal state changes (more...)
The Strategy lets the algorithm vary independently from the clients that use it. It defines a family of algorithms, encapsulates each one, and makes them interchangeable (more...)
Template Method
The Template Method defines a skeleton of an algorithm in an operation, and defers some steps to subclasses (more...)
The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates (more...)
Composing Methods of Refactoring
Extract Method
If you have a code fragment that can be grouped together, turn the fragment into a method whose name explains the purpose of the method (more...)
Inline Method
If a method's body is just as clear as its name, put the method's body into the body of its callers and remove the method (more...)
Inline Temp
If you have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings, replace all references to that temp with the expression (more...)
Introduce Explaining Variable
If you have a complicated expression, put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose (more...)
Remove Assignments to Parameters
If the code assigns to a parameter, use a temporary variable instead (more...)
Replace Method with Method Object
If uou have a long method that uses local variables in such a way that you cannot apply Extract Method, turn the method into its own object so that all the local variables become fields on that object (more...)
Replace Temp with Query
If you are using a temporary variable to hold the result of an expression, extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods (more...)
Split Temporary Variable
If you have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable, make a separate temporary variable for each assignment (more...)
Substitute Algorithm
If you want to replace an algorithm with one that is clearer, replace the body of the method with the new algorithm (more...)
Moving Features Between Objects
Extract Class
If you have one class doing work that should be done by two, create a new class and move the relevant fields and methods from the old class into the new class (more...)
Hide Delegate
If a client is calling a delegate class of an object, create methods on the server to hide the delegate (more...)
Inline Class
If a class isn't doing very much, move all its features into another class and delete it (more...)
Introduce Foreign Method
If a server class you are using needs an additional method, but you can't modify the class, create a method in the client class with an instance of the server class as its first argument (more...)
Introduce Local Extension
If a server class you are using needs several additional methods, but you can't modify the class, create a new class that contains these extra methods. Make this extension class a subclass or a wrapper of the original (more...)
Move Field
If a field is, or will be, used by another class more than the class on which it is defined, create a new field in the target class, and change all its users (more...)
Move Method
If a method is, or will be, using or used by more features of another class than the class on which it is defined, create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether (more...)
Remove Middle Man
If a class is doing too much simple delegation, get the client to call the delegate directly (more...)
Organizing Data
Change Bidirectional Association to Unidirectional
If you have a two-way association but one class no longer needs features from the other, drop the unneeded end of the association (more...)
Change Reference to Value
If you have a reference object that is small, immutable, and awkward to manage, turn it into a value object (more...)
Change Unidirectional Association to Bidirectional
If you have two classes that need to use each other's features, but there is only a one-way link, add back pointers, and change modifiers to update both sets (more...)
Change Value to Reference
If you have a class with many equal instances that you want to replace with a single object, turn the object into a reference object (more...)
Duplicate Observed Data
If you have domain data available only in a GUI control, and domain methods need access, copy the data to a domain object. Set up an observer to synchronize the two pieces of data (more...)
Encapsulate Collection
If a method returns a collection, make it return a read-only view and provide add/remove methods (more...)
Encapsulate Field
If there is a public field, make it private and provide accessors (more...)
Replace Array with Object
If you have an array in which certain elements mean different things, replace the array with an object that has a field for each element (more...)
Replace Data Value with Object
If you have a data item that needs additional data or behavior, turn the data item into an object (more...)
Replace Magic Number with Symbolic Constant
If you have a literal number with a particular meaning, create a constant, name it after the meaning, and replace the number with it (more...)
Replace Record with Data Class
If you need to interface with a record structure in a traditional programming environment, make a dumb data object for the record (more...)
Replace Subclass with Fields
If you have subclasses that vary only in methods that return constant data, change the methods to superclass fields and eliminate the subclasses (more...)
Replace Type Code with Class
If a class has a numeric type code that does not affect its behavior, replace the number with a new class (more...)
Replace Type Code with State/Strategy
If you have a type code that affects the behavior of a class, but you cannot use subclassing, replace the type code with a state object (more...)
Replace Type Code with Subclasses
If you have an immutable type code that affects the behavior of a class, replace the type code with subclasses (more...)
Introduce Explaining Variable
If you have a complicated expression, put the result of the expression, or parts of the expression, in a temporary variable with a name that explains the purpose (more...)
Remove Assignments to Parameters
If the code assigns to a parameter, use a temporary variable instead (more...)
Self Encapsulate Field
If you are accessing a field directly, but the coupling to the field is becoming awkward, create getting and setting methods for the field and use only those to access the field (more...)
Simplifying Conditional Expressions
Consolidate Conditional Expression
If you have a sequence of conditional tests with the same result, combine them into a single conditional expression and extract it (more...)
Consolidate Duplicate Conditional Fragments
If the same fragment of code is in all branches of a conditional expression, move it outside of the expression (more...)
Decompose Conditional
If you have a complicated conditional (if-then-else) statement, extract methods from the condition, then part, and else parts (more...)
Introduce Assertion
If a section of code assumes something about the state of the program, make the assumption explicit with an assertion (more...)
Introduce Null Object
If you have repeated checks for a null value, replace the null value with a null object (more...)
Remove Control Flag
If you have a variable that is acting as a control flag for a series of boolean expressions, use a break or return instead (more...)
Replace Conditional with Polymorphism
If you have a conditional that chooses different behavior depending on the type of an object, move each leg of the conditional to an overriding method in a subclass. Make the original method abstract (more...)
Replace Nested Conditional with Guard Clauses
If a method has conditional behavior that does not make clear the normal path of execution, use guard clauses for all the special cases (more...)
Making Method Calls Simpler
Add Parameter
If a method needs more information from its caller, add a parameter for an object that can pass on this information (more...)
Encapsulate Downcast
If a method returns an object that needs to be downcasted by its callers, move the downcast to within the method (more...)
Hide Method
If a method is not used by any other class, make the method private (more...)
Introduce Parameter Object
If you have a group of parameters that naturally go together, replace them with an object (more...)
Hide Delegate
If a client is calling a delegate class of an object, create methods on the server to hide the delegate (more...)
Parameterize Method
If several methods do similar things but with different values contained in the method body, create one method that uses a parameter for the different values (more...)
Preserve Whole Object
If you are getting several values from an object and passing these values as parameters in a method call, send the whole object instead (more...)
Remove Parameter
If a parameter is no longer used by the method body, remove it (more...)
Remove Setting Method
A field should be set at creation time and never altered. Remove any setting method for that field (more...)
Rename Method
If the name of a method does not reveal its purpose, change the name of the method (more...)
Replace Constructor with Factory Method
If you want to do more than simple construction when you create an object, replace the constructor with a factory method (more...)
Replace Error Code with Exception
If a method returns a special code to indicate an error, throw an exception instead (more...)
Replace Exception with Test
If you are throwing a checked exception on a condition the caller could have checked first, change the caller to make the test first (more...)
Replace Parameter with Explicit Methods
If you have a method that runs different code depending on the values of an enumerated parameter, create a separate method for each value of the parameter (more...)
Replace Parameter with Method
If an object invokes a method, then passes the result as a parameter for a method. The receiver can also invoke this method, remove the parameter and let the receiver invoke the method (more...)
Separate Query from Modifier
If you have a method that returns a value but also changes the state of an object, create two methods, one for the query and one for the modification (more...)
Dealing with Generalization
Collapse Hierarchy
If a superclass and subclass are not very different, merge them together (more...)
Extract Interface
If several clients use the same subset of a class's interface, or two classes have part of their interfaces in common, extract the subset into an interface (more...)
Extract Subclass
If a class has features that are used only in some instances, create a subclass for that subset of features (more...)
Replace Array with Object
If you have an array in which certain elements mean different things, replace the array with an object that has a field for each element (more...)
Replace Data Value with Object
If you have a data item that needs additional data or behavior, turn the data item into an object (more...)
Extract Superclass
If you have two classes with similar features, create a superclass and move the common features to the superclass (more...)
Form Template Method
If you have two methods in subclasses that perform similar steps in the same order, yet the steps are different, get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up (more...)
Pull Up Constructor Body
If you have constructors on subclasses with mostly identical bodies, create a superclass constructor; call this from the subclass methods (more...)
Pull Up Field
If two subclasses have the same field, move the field to the superclass (more...)
Pull Up Method
If you have methods with identical results on subclasses, move them to the superclass (more...)
Push Down Field
If a field is used only by some subclasses, move the field to those subclasses (more...)
Push Down Method
If behavior on a superclass is relevant only for some of its subclasses, move it to those subclasses (more...)
Replace Delegation with Inheritance
If you're using delegation and are often writing many simple delegations for the entire interface, make the delegating class a subclass of the delegate (more...)
Replace Inheritance with Delegation
If a subclass uses only part of a superclasses interface or does not want to inherit data, create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing (more...)
Big Refactorings
Convert Procedural Design to Objects
If you have code written in a procedural style, turn the data records into objects, break up the behavior, and move the behavior to the objects (more...)
Extract Hierarchy
If you have a class that is doing too much work, at least in part through many conditional statements, create a hierarchy of classes in which each subclass represents a special case (more...)
Tease Apart Inheritance
If you have an inheritance hierarchy that is doing two jobs at once, create two hierarchies and use delegation to invoke one from the other (more...)