Browse and view all of the patterns on our Website: iluwatar.github.io/java-design-patterns/
Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.
Design patterns can speed up the development process by providing tested, proven development paradigms.
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.
Q: What is the difference between State and Strategy patterns?
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.
Q: What is the difference between Strategy and Template Method patterns?
In Template Method the algorithm is chosen at compile time via inheritance. With Strategy pattern the algorithm is chosen at runtime via composition.
Q: What is the difference between Proxy and Decorator patterns?
The difference is the intent of the patterns. While Proxy controls access to the object Decorator is used to add responsibilities to the object.
Q: What is the difference between Chain of Responsibility and Intercepting Filter patterns?
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.
Q: What is the difference between Visitor and Double Dispatch patterns?
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.
Q: What are the differences between Flyweight and Object Pool patterns?
They differ in the way they are used.
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.
In contrast, a Flyweight object is singleton, and it can be used simultaneously by multiple clients.
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.
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.
To work on a new pattern you need to do the following steps:
- 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.
- Fork the repository.
- Create a new folder for the pattern. The rough structure of the new folder would be as follows:
- etc (every resource related to the pattern, like diagrams)
- src (the source code of the pattern)
- index.md (the description of the pattern)
- pom.xml (the maven pom.xml)
- 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.
- Format the code according to Google Java Style Guide
- Create a simple class diagram from your example code and put it inside of the etc folder.
- Add description of the pattern in index.md and link to the class diagram. (Attention, all internal links must be relative to the pattern subdirectory, else the links dont link properly on the website)
- Create a pull request.
Structure of the index.md file
--- # this is so called 'Yaml Front Matter', read up on it here: http://jekyllrb.com/docs/frontmatter/
layout: pattern # layout must allways be pattern
title: Best Pattern Ever # the properly formatted title
folder: best-pattern-ever # the folder name in which this pattern lies
permalink: /patterns/best-pattern-ever/ # the permalink to the pattern, to keep this uniform please stick to /patterns/FOLDER/
# both categories and tags are Yaml Lists
# you can either just pick one or write a list with '-'s
# usable categories and tags are listed here: https://github.com/iluwatar/java-design-patterns/blob/gh-pages/_config.yml
categories: creational # categories of the pattern
tags: # tags of the pattern
- best
- ever
- awesome
---
**Intent:** Makes your code awesome
![alt text](./etc/best_pattern.png "Best Pattern Ever")
**Applicability:** Use the Best Pattern Ever pattern when
* you want to be the best
* you need to ...
**Real world examples:**
* [Nowhere](http://no.where.com)
To add a new category or tag you need to edit the _config.yml file of the gh-pages branch. In there you should find 2 yaml lists with the respective names 'category-list' and 'tag-list'
To work on one of the non-pattern issues you need to do the following steps:
- Check that the issue has "help wanted" badge
- Comment on the issue that you are working on it
- Fork the repository.
- 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.
- Create a pull request.
For creating/editing UML diagrams you need ObjectAid UML Explorer for Eclipse.
For inspiration check out the following sources:
- there is a good list of design patterns at Wikipedia
- Martin Fowler's Catalog of Patterns of Enterprise Application Architecture
- pattern language for microservices
- Microsoft's Cloud Design Patterns
Links to patterns applied in real world applications are welcome. The links
should be added to the corresponding section of the index.md
.
Java-design-patterns project uses semantic versioning 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.
- Design Patterns: Elements of Reusable Object-Oriented Software
- Effective Java (2nd Edition)
- Java Generics and Collections
- Let's Modify the Objects-First Approach into Design-Patterns-First
- Pattern Languages of Program Design
- Martin Fowler - Event Aggregator
- TutorialsPoint - Intercepting Filter
- Presentation Tier Patterns
- Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions
- Martin Fowler - Service Layer
- Martin Fowler - Specifications
- Martin Fowler - Tolerant Reader
- Trygve Reenskaug - Model-view-controller
- Flux - Application architecture for building user interfaces
- Richard Pawson - Naked Objects
- Patterns of Enterprise Application Architecture
- Spring Data
- J2EE Design Patterns
- Marco Castigliego - Step Builder
- Douglas C. Schmidt and Charles D. Cranor - Half Sync/Half Async
- Pattern Oriented Software Architecture Vol I-V
This project is licensed under the terms of the MIT license.