Home / Design Pattern
Design Patterns in Java
Design patterns are proven, reusable solutions to common coding problems. There are 23 classic patterns in 3 groups. Click any pattern to open its own page, or use the menu on the left.
Creational Patterns
Creational patterns are about creating objects. They hide how objects are made, so your code stays flexible and easy to change. There are 5 creational patterns.
SingletonSingleton makes sure a class has only one object.Factory MethodA parent class has a method that creates an object.Abstract FactoryA factory that creates a whole family of related objects.BuilderBuilder creates a big object step by step.PrototypeInstead of creating a new object from zero, you copy (clone) an existing object.
Structural Patterns
Structural patterns are about how classes and objects are joined together to make bigger structures. They keep the structure flexible and simple. There are 7 structural patterns.
AdapterAdapter sits between two classes that do not fit together and translates one interface into the other.BridgeBridge splits a class into two parts, what it is and how it works, so both can change on their own.CompositeComposite lets you treat one object and a group of objects in the same way.DecoratorDecorator adds new features to an object by wrapping it, without changing its class.FacadeFacade gives one simple class in front of a complex system.FlyweightFlyweight saves memory by sharing objects that are the same, instead of creating thousands of copies.ProxyProxy is a stand-in object that controls access to the real object.
Behavioral Patterns
Behavioral patterns are about how objects talk to each other and share work. They make communication clear and loosely connected. There are 11 behavioral patterns.
Chain of ResponsibilityA request moves along a chain of handlers.CommandCommand turns a request into an object.InterpreterInterpreter defines a small language (grammar) and a way to read and calculate sentences of that language.IteratorIterator gives a standard way to go through the items of a collection one by one, without showing how the collection is built inside.MediatorMediator is a middle object.MementoMemento saves a snapshot of an object's state so you can go back to it later.ObserverWhen one object changes, all objects that subscribed to it are told automatically.StateState lets an object change its behavior when its internal state changes.StrategyStrategy keeps several ways (algorithms) to do one job in separate classes.Template MethodA parent class fixes the steps of an algorithm.VisitorVisitor lets you add a new operation to many classes without changing those classes.