2014-12-02 11:02:21 +11:00
# Design pattern samples in Java.
2014-08-09 20:44:21 +03:00
2015-08-11 19:33:28 +00:00
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2015-06-29 22:19:10 +03:00
 [](https://coveralls.io/r/iluwatar/java-design-patterns?branch=master) < a href = "https://scan.coverity.com/projects/5634" >
< img alt = "Coverity Scan Build Status"
src="https://scan.coverity.com/projects/5634/badge.svg"/>
< / a >
2014-08-30 16:15:26 +03:00
2015-08-11 12:10:58 +05:30
< a name = "top" / >
2015-08-11 11:58:02 +05:30
- < a href = " #introduction " > Introduction</ a >
- < a href = " #list -of-design-patterns" > List of Design Patterns</ a >
- < a href = " #creational -patterns" > Creational Patterns</ a >
- < a href = " #structural -patterns" > Structural Patterns</ a >
- < a href = " #behavioral -patterns" > Behavioral Patterns</ a >
2015-08-11 12:10:58 +05:30
- < a href = " #concurrency -patterns" > Concurrency Patterns</ a >
2015-08-11 11:58:02 +05:30
- < a href = " #presentation -tier-patterns" > Presentation Tier Patterns</ a >
- < a href = " #business -tier-patterns" > Business Tier Patterns</ a >
- < a href = " #architectural -patterns" > Architectural Patterns</ a >
- < a href = " #integration -patterns" > Integration Patterns</ a >
- < a href = " #idioms " > Idioms</ a >
- < a href = " #faq " > Frequently Asked Questions</ a >
- < a href = " #how -to-contribute" > How to contribute</ a >
- < a href = " #versioning " > Versioning</ a >
- < a href = " #credits " > Credits</ a >
- < a href = " #license " > License</ a >
## <a name="introduction">Introduction</a>
2015-03-31 19:43:35 +03:00
2015-08-07 10:29:09 -03:00
Design patterns are formalized best practices that the programmer can use to
solve common problems when designing an application or system.
2015-03-31 19:43:35 +03:00
2015-08-07 10:29:09 -03:00
Design patterns can speed up the development process by providing tested, proven
development paradigms.
2015-03-31 19:43:35 +03:00
2015-08-07 10:29:09 -03:00
Reusing design patterns helps to prevent subtle issues that can cause major
problems, and it also improves code readability for coders and architects who
are familiar with the patterns.
2015-03-31 19:43:35 +03:00
2015-08-11 12:10:58 +05:30
## <a name="list-of-design-patterns">List of Design Patterns</a> [↑](#top)
2014-12-02 11:02:21 +11:00
2015-08-11 12:10:58 +05:30
### <a name="creational-patterns">Creational Patterns</a> [↑](#top)
2015-03-16 11:44:57 +02:00
2015-08-07 10:29:09 -03:00
Creational design patterns abstract the instantiation process. They help make a
system independent of how its objects are created, composed, and represented.
2015-03-16 11:55:42 +02:00
2015-03-16 11:47:28 +02:00
* [Abstract Factory ](#abstract-factory )
* [Builder ](#builder )
* [Factory Method ](#factory-method )
* [Prototype ](#prototype )
2015-03-29 23:03:06 +03:00
* [Property ](#property )
2015-03-16 11:47:28 +02:00
* [Singleton ](#singleton )
2015-08-07 16:17:47 +03:00
* [Step Builder ](#step-builder )
2015-05-16 00:35:32 +03:00
* [Multiton ](#multiton )
2015-05-24 22:39:33 +03:00
* [Object Pool ](#object-pool )
2015-03-29 23:03:06 +03:00
2015-08-11 12:10:58 +05:30
### <a name="structural-patterns">Structural Patterns</a> [↑](#top)
2015-03-16 11:44:57 +02:00
2015-08-07 10:29:09 -03:00
Structural patterns are concerned with how classes and objects are composed to
form larger structures.
2015-03-16 11:55:42 +02:00
2015-03-16 11:47:28 +02:00
* [Adapter ](#adapter )
* [Bridge ](#bridge )
* [Composite ](#composite )
* [Decorator ](#decorator )
* [Facade ](#facade )
* [Flyweight ](#flyweight )
* [Proxy ](#proxy )
* [Service Locator ](#service-locator )
* [Servant ](#servant )
* [Event Aggregator ](#event-aggregator )
2015-03-16 11:44:57 +02:00
2015-08-11 12:10:58 +05:30
### <a name="behavioral-patterns">Behavioral Patterns</a> [↑](#top)
2015-03-16 11:47:28 +02:00
2015-08-07 10:29:09 -03:00
Behavioral patterns are concerned with algorithms and the assignment of
responsibilities between objects.
2015-03-16 11:55:42 +02:00
2015-03-16 11:47:28 +02:00
* [Chain of responsibility ](#chain-of-responsibility )
* [Command ](#command )
* [Interpreter ](#interpreter )
* [Iterator ](#iterator )
* [Mediator ](#mediator )
* [Memento ](#memento )
* [Observer ](#observer )
* [State ](#state )
* [Strategy ](#strategy )
* [Template method ](#template-method )
* [Visitor ](#visitor )
* [Null Object ](#null-object )
2015-04-09 22:24:30 +03:00
* [Intercepting Filter ](#intercepting-filter )
2015-04-23 22:58:34 +03:00
* [Specification ](#specification )
2015-05-25 19:29:24 +03:00
* [Dependency Injection ](#dependency-injection )
2014-12-02 11:02:21 +11:00
2015-08-11 12:10:58 +05:30
### <a name="concurrency-patterns">Concurrency Patterns</a> [↑](#top)
2015-04-24 22:56:08 +03:00
2015-08-07 10:29:09 -03:00
Concurrency patterns are those types of design patterns that deal with the
multi-threaded programming paradigm.
2015-04-24 22:56:08 +03:00
* [Double Checked Locking ](#double-checked-locking )
2015-05-17 22:04:07 +03:00
* [Thread Pool ](#thread-pool )
2015-07-22 23:18:07 +03:00
* [Async Method Invocation ](#async-method-invocation )
2015-07-25 15:58:12 +05:30
* [Half-Sync/Half-Async ](#half-sync-half-async )
2015-04-24 22:56:08 +03:00
2015-08-11 12:10:58 +05:30
### <a name="presentation-tier-patterns">Presentation Tier Patterns</a> [↑](#top)
2015-04-03 18:22:46 +08:00
2015-08-07 10:29:09 -03:00
Presentation Tier patterns are the top-most level of the application, this is
concerned with translating tasks and results to something the user can
understand.
2015-04-03 18:22:46 +08:00
2015-05-03 00:00:43 +03:00
* [Model-View-Controller ](#model-view-controller )
2015-04-04 21:12:43 +03:00
* [Model-View-Presenter ](#model-view-presenter )
2015-05-03 23:44:22 +03:00
* [Flux ](#flux )
2015-07-19 14:17:01 +03:00
* [Front Controller ](#front-controller )
2015-04-03 18:27:01 +08:00
2015-08-11 12:10:58 +05:30
### <a name="business-tier-patterns">Business Tier Patterns</a> [↑](#top)
2015-07-23 00:16:32 +03:00
* [Business Delegate ](#business-delegate )
2015-08-11 12:10:58 +05:30
### <a name="architectural-patterns">Architectural Patterns</a> [↑](#top)
2015-04-15 14:54:02 -05:00
2015-08-07 10:29:09 -03:00
An architectural pattern is a general, reusable solution to a commonly occurring
problem in software architecture within a given context.
2015-04-15 14:54:02 -05:00
* [Data Access Object ](#dao )
2015-04-15 23:18:38 +03:00
* [Service Layer ](#service-layer )
2015-07-13 23:30:25 +03:00
* [Naked Objects ](#naked-objects )
2015-07-22 01:15:30 +03:00
* [Repository ](#repository )
2015-04-15 14:54:02 -05:00
2015-08-11 12:10:58 +05:30
### <a name="integration-patterns">Integration Patterns</a> [↑](#top)
2015-04-29 22:40:03 +03:00
2015-08-07 10:29:09 -03:00
Integration patterns are concerned with how software applications communicate
and exchange data.
2015-04-29 22:40:03 +03:00
* [Tolerant Reader ](#tolerant-reader )
2015-08-11 12:10:58 +05:30
### <a name="idioms">Idioms</a> [↑](#top)
2015-04-05 14:20:03 +03:00
2015-08-07 10:29:09 -03:00
A programming idiom is a means of expressing a recurring construct in one or
more programming languages. Generally speaking, a programming idiom is an
expression of a simple task, algorithm, or data structure that is not a built-in
feature in the programming language being used, or, conversely, the use of an
unusual or notable feature that is built into a programming language. What
distinguishes idioms from patterns is generally the size, the idioms tend to be
something small while the patterns are larger.
2015-04-05 14:20:03 +03:00
* [Execute Around ](#execute-around )
2015-04-05 18:03:16 +03:00
* [Poison Pill ](#poison-pill )
2015-04-05 18:04:31 +03:00
* [Callback ](#callback )
2015-04-10 20:24:16 +03:00
* [Lazy Loading ](#lazy-loading )
2015-05-09 19:51:33 +03:00
* [Double Dispatch ](#double-dispatch )
2015-05-16 22:23:45 +03:00
* [Resource Acquisition Is Initialization ](#resource-acquisition-is-initialization )
2015-05-24 12:53:16 +03:00
* [Private Class Data ](#private-class-data )
2015-04-03 20:24:08 +08:00
2015-08-11 12:10:58 +05:30
# <a name="faq">Frequently asked questions</a> [↑](#top)
2014-09-06 23:31:56 +03:00
2015-03-21 15:41:33 +02:00
**< a id = "Q1" > Q: What is the difference between State and Strategy patterns?< / a > **
2014-09-06 23:33:48 +03:00
2015-08-07 10:29:09 -03:00
While the implementation is similar they solve different problems. The State
pattern deals with what state an object is in - it encapsulates state-dependent
behavior.
The Strategy pattern deals with how an object performs a certain task - it
encapsulates an algorithm.
2014-09-06 23:31:56 +03:00
2015-03-21 15:41:33 +02:00
**< a id = "Q2" > Q: What is the difference between Strategy and Template Method patterns?< / a > **
2014-09-06 23:33:48 +03:00
2015-08-07 10:29:09 -03:00
In Template Method the algorithm is chosen at compile time via inheritance.
With Strategy pattern the algorithm is chosen at runtime via composition.
2014-09-06 23:42:22 +03:00
2015-03-21 15:41:33 +02:00
**< a id = "Q3" > Q: What is the difference between Proxy and Decorator patterns?< / a > **
2014-09-06 23:42:22 +03:00
2015-08-07 10:29:09 -03:00
The difference is the intent of the patterns. While Proxy controls access to
the object Decorator is used to add responsibilities to the object.
2014-09-07 15:40:21 +03:00
2015-04-09 23:00:54 +03:00
**< a id = "Q4" > Q: What is the difference between Chain of Responsibility and Intercepting Filter patterns?< / a > **
2015-08-07 10:29:09 -03:00
While the implementations look similar there are differences. The Chain of
Responsibility forms a chain of request processors and the processors are then
executed one by one until the correct processor is found. In Intercepting
Filter the chain is constructed from filters and the whole chain is always
executed.
2014-09-07 15:40:21 +03:00
2015-05-26 22:42:21 +03:00
**< a id = "Q5" > Q: What is the difference between Visitor and Double Dispatch patterns?< / a > **
2015-08-07 10:29:09 -03:00
The Visitor pattern is a means of adding a new operation to existing classes.
Double dispatch is a means of dispatching function calls with respect to two
polymorphic types, rather than a single polymorphic type, which is what
languages like C++ and Java _do not_ support directly.
2015-05-26 22:42:21 +03:00
2015-06-23 22:42:16 +03:00
**< a id = "Q6" > Q: What are the differences between Flyweight and Object Pool patterns?< / a > **
They differ in the way they are used.
2015-08-07 10:29:09 -03:00
Pooled objects can simultaneously be used by a single "client" only. For that,
a pooled object must be checked out from the pool, then it can be used by a
client, and then the client must return the object back to the pool. Multiple
instances of identical objects may exist, up to the maximal capacity of the
pool.
2015-06-23 22:42:16 +03:00
2015-08-07 10:29:09 -03:00
In contrast, a Flyweight object is singleton, and it can be used simultaneously
by multiple clients.
2015-06-23 22:42:16 +03:00
2015-08-07 10:29:09 -03:00
As for concurrent access, pooled objects can be mutable and they usually don't
need to be thread safe, as typically, only one thread is going to use a
specific instance at the same time. Flyweight must either be immutable (the
best option), or implement thread safety.
2015-06-23 22:42:16 +03:00
2015-08-07 10:29:09 -03:00
As for performance and scalability, pools can become bottlenecks, if all the
pooled objects are in use and more clients need them, threads will become
blocked waiting for available object from the pool. This is not the case with
Flyweight.
2015-06-23 22:42:16 +03:00
2015-05-26 22:42:21 +03:00
2014-09-07 15:40:21 +03:00
2015-08-11 12:10:58 +05:30
# <a name="how-to-contribute">How to contribute</a> [↑](#top)
2014-09-07 15:40:21 +03:00
2015-07-27 21:00:30 +03:00
**To work on a new pattern** you need to do the following steps:
2014-09-07 15:41:46 +03:00
2015-08-08 10:27:50 -03:00
1. If there is no issue for the new pattern yet, raise new issue. Comment on
the issue that you are working on it so that others don't start work on the
same thing.
2015-07-27 21:00:30 +03:00
2. Fork the repository.
2015-08-08 10:27:50 -03:00
3. Implement the code changes in your fork. Remember to add sufficient comments
documenting the implementation. Reference the issue id e.g. #52 in your
commit messages.
2015-07-27 21:00:30 +03:00
4. Create a simple class diagram from your example code.
5. Add description of the pattern in README.md and link to the class diagram.
6. Create a pull request.
2014-09-07 15:56:09 +03:00
2015-07-27 21:00:30 +03:00
**To work on one of the non-pattern issues** you need to do the following steps:
2015-04-04 22:30:55 +03:00
2015-07-27 21:00:30 +03:00
1. Check that the issue has "help wanted" badge
2. Comment on the issue that you are working on it
3. Fork the repository.
2015-08-08 10:27:50 -03:00
4. Implement the code changes in your fork. Remember to add sufficient comments
documenting the implementation. Reference the issue id e.g. #52 in your
commit messages.
2015-07-27 21:00:30 +03:00
5. Create a pull request.
2015-04-04 22:30:55 +03:00
2015-04-04 21:30:16 +03:00
**For creating/editing UML diagrams** you need [ObjectAid UML Explorer for Eclipse ](http://www.objectaid.com/home ).
2014-10-19 13:24:16 +03:00
2015-04-05 19:18:09 +03:00
**For inspiration** check out the following sources:
* there is a good list of design patterns at [Wikipedia ](http://en.wikipedia.org/wiki/Software_design_pattern )
* Martin Fowler's [Catalog of Patterns of Enterprise Application Architecture ](http://martinfowler.com/eaaCatalog/ )
* [pattern language for microservices ](http://microservices.io/patterns/index.html )
* Microsoft's [Cloud Design Patterns ](http://download.microsoft.com/download/B/B/6/BB69622C-AB5D-4D5F-9A12-B81B952C1169/CloudDesignPatternsBook-PDF.pdf )
2014-10-27 08:28:22 +02:00
2015-08-07 10:29:09 -03:00
**Links to patterns applied in real world applications** are welcome. The links
should be added to the corresponding section of the `README.md` .
2014-09-07 15:56:09 +03:00
2015-08-11 12:10:58 +05:30
# <a name="versioning">Versioning</a> [↑](#top)
2015-06-24 20:18:05 +03:00
2015-08-07 10:29:09 -03:00
Java-design-patterns project uses [semantic versioning ](http://semver.org/ )
scheme. However, version numbers in this project do not signify binary releases
(since we don't make any) but rather milestones achieved on the roadmap. In
other words, version numbers are used only for project planning sake.
2015-06-24 20:18:05 +03:00
2015-08-11 12:10:58 +05:30
# <a name="credits">Credits</a> [↑](#top)
2014-09-07 19:18:43 +03:00
2014-09-07 19:20:16 +03:00
* [Design Patterns: Elements of Reusable Object-Oriented Software ](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612 )
* [Effective Java (2nd Edition) ](http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683 )
2014-11-12 10:47:41 +00:00
* [Java Generics and Collections ](http://www.amazon.com/Java-Generics-Collections-Maurice-Naftalin/dp/0596527756/ )
2015-08-13 23:54:40 +02:00
* [Let's Modify the Objects-First Approach into Design-Patterns-First ](http://edu.pecinovsky.cz/papers/2006_ITiCSE_Design_Patterns_First.pdf )
2015-03-08 22:07:45 +02:00
* [Pattern Languages of Program Design ](http://www.amazon.com/Pattern-Languages-Program-Design-Coplien/dp/0201607344/ref=sr_1_1 )
2015-03-13 22:07:24 +02:00
* [Martin Fowler - Event Aggregator ](http://martinfowler.com/eaaDev/EventAggregator.html )
2015-04-03 20:55:13 +08:00
* [TutorialsPoint - Intercepting Filter ](http://www.tutorialspoint.com/design_pattern/intercepting_filter_pattern.htm )
2015-04-04 21:25:10 +03:00
* [Presentation Tier Patterns ](http://www.javagyan.com/tutorials/corej2eepatterns/presentation-tier-patterns )
2015-03-29 22:03:07 +03:00
* [Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions ](http://www.amazon.com/Functional-Programming-Java-Harnessing-Expressions/dp/1937785467/ref=sr_1_1 )
2015-04-15 23:00:32 +03:00
* [Martin Fowler - Service Layer ](http://martinfowler.com/eaaCatalog/serviceLayer.html )
2015-04-23 22:58:34 +03:00
* [Martin Fowler - Specifications ](http://martinfowler.com/apsupp/spec.pdf )
2015-04-29 22:43:15 +03:00
* [Martin Fowler - Tolerant Reader ](http://martinfowler.com/bliki/TolerantReader.html )
2015-05-03 23:56:07 +03:00
* [Trygve Reenskaug - Model-view-controller ](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller )
2015-05-03 23:58:23 +03:00
* [Flux - Application architecture for building user interfaces ](http://facebook.github.io/flux/ )
2015-07-13 23:30:25 +03:00
* [Richard Pawson - Naked Objects ](http://downloads.nakedobjects.net/resources/Pawson%20thesis.pdf )
2015-07-19 14:19:05 +03:00
* [Patterns of Enterprise Application Architecture ](http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420 )
2015-07-22 01:17:42 +03:00
* [Spring Data ](http://www.amazon.com/Spring-Data-Mark-Pollack/dp/1449323952/ref=sr_1_1 )
2015-07-23 00:17:51 +03:00
* [J2EE Design Patterns ](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2 )
2015-08-10 10:20:13 +03:00
* [Marco Castigliego - Step Builder ](http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html )
2015-08-12 11:33:49 +05:30
* [Douglas C. Schmidt and Charles D. Cranor - Half Sync/Half Async ](http://www.cs.wustl.edu/~schmidt/PDF/PLoP-95.pdf )
* [Pattern Oriented Software Architecture Vol I-V ](http://www.amazon.com/Pattern-Oriented-Software-Architecture-Volume-Patterns/dp/0471958697 )
2014-09-07 19:18:43 +03:00
2015-04-04 21:37:36 +08:00
2015-08-11 12:10:58 +05:30
# <a name="license">License</a> [↑](#top)
2014-09-07 15:56:09 +03:00
2015-04-03 22:27:32 +08:00
This project is licensed under the terms of the MIT license.