Design Patterns



Factory - done
Abstract Factory - done
Singleton - done

Filter / Criteria
Business Delegate
Composite Entity
Data Access Object (DAO)
Front Controller
Intercepting Filter
Service Locator
Transfer Object Pattern

Chain of responsibility
Null Object - default behavior

Inversion of Control / Dependency Injection
Producer / Consumer

Active Object
Monostate Object
Abstract Server Pattern

The Dependency Inversion Principle states that:

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should not depend upon details. Details should depend upon abstractions.
// DRY: Do not Repeat Yourself
// KISS: Keep It Simple Stupid
// YAGNI: You ain't gonna need it (do not build the stuff that you do not need right away)

// SOLID: 
// 1. single responsibility (SRP)
// 2. open-closed (OCP)
// 3. Liskov substitution
// 4. interface segregation
// 5. dependency inversion

// single responsibility (SRP): a class should have only a single responsibility
// open-closed (OCP): software entities (classes, modules, functions, etc.) should 
      be open for extension but closed for modification. 
// Liskov substitution (LSP): “objects in a program should be replaceable with instances of their
      subtypes without altering the correctness of that program.” See also design by contract.
// interface segregation (ISP): many client-specific interfaces are better than one general-purpose interface
// dependency inversion (DIP): one should “Depend upon Abstractions. Do not depend upon concretions.

// Separation of concerns. Divide your application into distinct features with as little overlap 
// in functionality as possible. The important factor is minimization of interaction points to achieve
// high cohesion and low coupling. However, separating functionality at the wrong boundaries can 
// result in high coupling and complexity between features even though the contained functionality
// within a feature does not significantly overlap.

// Principle of Least Knowledge (also known as the Law of Demeter or LoD). A component or 
// object should not know about internal details of other components or objects. 

// Minimize upfront design. Only design what is necessary. In some cases, you may require upfront 
// comprehensive design and testing if the cost of development or a failure in the design is very high. 
// In other cases, especially for agile development, you can avoid big design upfront (BDUF). 
// If your application requirements are unclear, or if there is a possibility of the design evolving over 
// time, avoid making a large design effort prematurely. This principle is sometimes known 
// as YAGNI ("You ain’t gonna need it").

// Behavioral, Structural, Creational

// Behavioral: Chain of Responsibility, Iterator, State, Strategy, Observer, Visitor, Template Method
// Command, Memento, Mediator

// Chain of Responsibility: A way of passing a request between a chain of objects.  Exception handling, 
// logging.  This pattern is involved when there is a chain, or a complex process is involved. 
// (javax.servlet.Filter#doFilter, Exception Handling)

// Iterator: Sequentially access the elements of a collection.  Iterating around collections.  
// (all implementation of java.util.Iterator)

// State: Alter an object's behavior when its state changes.  If we increase the speed (a state) 
// of a fan, its behavior change (it run faster).  

// Strategy: If we create an interface, and several classes that implement this interface,
// we can swap out these object.

// Observer: Available in Java.  Implements the Observer interface.  The subject extends 
// the Observerable class, and call observer.notify.  You cannot extend another class.

// Visitor: Defines a new operation to a class without change.  Consider the operation of a taxi 
// company.  When a person calls a taxi company, he is registered.  The company then dispatches
// a cab to the customer (accepting the visitor).  Upon entering the taxi, or visitor, the custom 
// is no longer in control of his or her own transportation, the taxi driver is.

// Template Method: Defer the exact steps of an algorithm to a subclass.  Template method 
// (this is basically anything that deal with abstract class).

// Command: (java.lang.Runnable).  Encapsulate a command request as an object.  Waiter 
// writes order and pass it on to the chef.  All implementations of java.lang.Runnable are 
// examples of the Command pattern.

// Memento: Capture and restore an object's internal state.  Undo / Redo operations.  Save 
// the state in a game (intermediate save).  Serialization can be used in Java.

// Mediator: Defines simplified communication between classes.  Air Traffic Controller.  
// Imagine all flights talking to each other to decide route and landing.

// Creational (how the class is created): Prototype

// Prototype: The Prototype pattern makes use of the clone() method of the Object class.  
// A fully initialized instance to be copied or cloned.

// Builder: Seperate object construction from its representation.  Used for creating complex 
// objects that have multiple child objects.  Even though the internal is complex, we provide 
// an easy interface for the main object to the outside world.  So, in a builder pattern, we 
// would have one object that is responsible for creating other objects.  For example, if you 
// consider a chess game as an object, that object would be responsible for instantiating the 
// player objects, and the various chess pieces.  To the outside world, the code just have to 
// create the chess game object, and the chess game object would create the other objects.

// Singleton: Java EE7 has a @Singleton annotation.  Examples: java.lang.RunTime.getRuntime(),
// java.lang.System.getSecurityManager()

// Factory:

// Structural: (how the class is composed, organized and used): Proxy, Decorator, Facade, Adapter
// Flyweight

// Flyweight: is primarily used to reduce the number of objects created and to decrease 
// memory footprint and increase performance.

What is the package that the Object class is in?

The Object class is in the java.lang package.

What are some singletons that exist in Java?

  1. java.lang.RunTime.getRuntime()
  2. java.lang.System.getSecurityManager()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License