1. Definition
    1. Solutions to recurring problems
    2. Guidelines on how to tackle certain problems
    3. Beware: Not a silver bullet, don't force them
  2. Creational
    1. Definition
      1. Focused towards how to instantiate objects
    2. Types
      1. Simple Factory
        1. Definition
          1. Simply generates an instance for client without exposing any instantiation logic to the client
        2. Useful When
          1. Creating an object is more than a few assignments and involves some logic, put it in a dedicated factory instead of repeating the same code everywhere
      2. Factory Method
        1. Definition
          1. It provides a way to delegate the instantiation logic to child classes
        2. Useful When
          1. There is some generic processing in a class but the required sub-class is dynamically decided at runtime. Or putting it in other words, when the client doesn't know what exact sub-class it might need.
      3. Abstract Factory
        1. Definition
          1. A factory of factories; a factory that groups the individual but related/dependent factories together without specifying their concrete classes
        2. Useful When
          1. There are interrelated dependencies with not-that-simple creation logic involved
      4. Builder
        1. Definition
          1. Allows you to create different flavors of an object while avoiding constructor pollution
        2. Useful When
          1. There could be several flavors of an object and to avoid constructor telescoping
          2. When there are a lot of steps involved in creation of an object
      5. Prototype
        1. Definition
          1. Create object based on an existing object through cloning
        2. Useful When
          1. An object is required that is similar to existing object or when the creation would be expensive as compared to cloning
      6. Singleton
        1. Definition
          1. Ensures that only one object of a particular class is ever created
          2. Actually considered an anti-pattern and overuse of it should be avoided
          3. Introduces a global state in your application and change to it in one place could affect in the other areas
          4. Makes your code tightly coupled
          5. Mocking the singleton could be difficult
        2. Useful When
          1. Only one object of a particular class is needed
  3. Structural
    1. Definition
      1. Concerned with object composition or in other words how the entities can use each other.
      2. They help in answering "How to build a software component?
    2. Types
      1. Adapter
        1. Definition
          1. Wrap an incompatible object in an adapter to make it compatible with another class
        2. Useful When
          1. Making the interface of an existing class to be used as another interface
      2. Bridge
        1. Definition
          1. Preferring composition over inheritance
          2. Implementation details are pushed from a hierarchy to another object with a separate hierarchy
        2. Useful When
          1. Decoupling an abstraction from its implementation so that the two can vary independently
      3. Composite
        1. Definition
          1. Lets clients treat the individual objects in a uniform manner
          2. Describes a group of objects that is treated the same way as a single instance of the same type of object
        2. Useful When
          1. Representing Part-Whole hierarchy
        3. Example
          1. A manager is a type of Employee
          2. A developer is a type of Employee
          3. A manager can manage a list of developers
          4. Composite pattern allows accessing both classes in the same way
      4. Decorator
        1. Definition
          1. Dynamically change the behavior of an object at run time by wrapping them in an object of a decorator class
        2. Useful When
          1. Add behaviors to an individual object without affecting the behavior of other objects from the same class
      5. Facade
        1. Definition
          1. Provides a simplified interface to a complex subsystem
        2. Useful When
          1. Wrap a complex class to provide a convenient interface
      6. Flyweight
        1. Definition
          1. Minimize memory usage or computational expenses by sharing as much as possible with similar objects
          2. Use a dictionary to store the unchanged/immutable object and reuse
        2. Useful When
          1. Need to create a large number of similar objects (10^5)
      7. Proxy
        1. Definition
          1. A class functioning as an interface to something else. A proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes
          2. Use of the proxy can simply be forwarding to the real object, or can provide additional logic
          3. AKA Surrogates, handles or wrappers
          4. Close to Adapters and Decorators
  4. Behavioral
    1. Definition
      1. Concerned with assignment of responsibilities between the objects
      2. Different from structural patterns
        1. Don't just specify the structure but also outline the patterns for message passing/communication between them
      3. They assist in answering "How to run a behavior in software component?"
    2. Types
      1. Chain of Responsibility
        1. Definition
          1. Building a chain of objects
          2. Request enters from one end and keeps going from object to object till it finds the suitable handler
        2. A source of command objects
        3. A series of processing objects
          1. Chained together
        4. Each processing objects execute the commands it can handle, and pass the rest to next
      2. Command
        1. Definition
          1. Encapsulate actions in objects
          2. Provide the means to decouple client from receiver
          3. Client asks Invoker to execute Command
          4. Invoker forward the request to Receiver
          5. Command object
          6. Encapsulates
          7. Parameter Values
          8. Object for the Method
          9. Method Name
          10. Knows how to
          11. execute()
          12. undo()
          13. redo()
        2. Useful When
          1. Implementing a transaction based system
          2. Maintaining a history of commands
      3. Iterator
        1. Definition
          1. A way to access the elements of an aggregate object without exposing its underlying representation
          2. No need to worry about different ways to iterate over a primitive array or ArrayList
        2. Commonly Used
          1. Iterator Interface
          2. for-each loop
      4. Mediator
        1. Definition
          1. Adds a third party object (called mediator) to control the interaction between two objects (called colleagues)
          2. Helps reduce the coupling between the classes communicating with each other
          3. Because now they don't need to have the knowledge of each other's implementation
      5. Memento
        1. Definition
          1. Capturing and storing the current state of an object in a manner that it can be restored later on in a smooth manner
          2. Provides the ability to restore an object to its previous state (undo via rollback)
        2. Components
          1. Originator
          2. The object for which the state is to be saved
          3. Memento
          4. The object that is going to maintain the state of originator
          5. Caretaker
          6. The object that keeps track of multiple memento
        3. Useful When
          1. Need to provide some sort of undo functionality
      6. Observer
        1. Definition
          1. Defines a dependency between objects so that whenever an object changes its state, all its dependents are notified
          2. Subject maintains a list of its dependents (observers)
          3. Subject notifies observers automatically of any state changes, by calling one of their methods
      7. Visitor
        1. Definition
          1. Visitee
          2. accept() a visitor
          3. uses a class-specific method from visitor
          4. Visitor
          5. visit() a visitee
          6. provides class-specific logic
          7. Separate operational logic from Visitee to Visitor
          8. Can do the same by having an inheritance hierarchy
          9. But then would have to modify the class to add new actions
          10. This pattern cam add new operations to Visitee objects without having to modify them
          11. Just create a new Visitor
        2. Useful When
          1. Perform an operation on a group of similar kind of objects
      8. Strategy
        1. Definition
          1. Allows you to switch the algorithm or strategy based on the situation
          2. Enables an algorithm's behavior to be selected at runtime
          3. AKA Policy Pattern
      9. State
        1. Definition
          1. Allows you change the behavior of a class when the state changes
          2. Implements a state machine in an object-oriented way
          3. A state machine is implemented by implementing each individual state as a derived class of the state pattern interface
          4. Implementing state transitions by invoking methods defined by the pattern's superclass
          5. Can be interpreted as a Strategy Pattern which is able to switch the current strategy through invocations of methods defined in the pattern's interface
      10. Template Method
        1. Definition
          1. Defines the skeleton of how a certain algorithm could be performed
          2. Defers the implementation of those steps to the children classes