orakle / java-design-patterns

Design pattern samples implemented in Java

Home Page:http://iluwatar.github.io/java-design-patterns/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Design pattern samples in Java

Join the chat at https://gitter.im/iluwatar/java-design-patterns

Build status Coverage Status Coverity Scan Build Status

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:

  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.
  2. Fork the repository.
  3. 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)
  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.
  5. Format the code according to Google Java Style Guide
  6. Create a simple class diagram from your example code and put it inside of the etc folder.
  7. 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)
  8. 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:

  1. Check that the issue has "help wanted" badge
  2. Comment on the issue that you are working on it
  3. Fork the repository.
  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.
  5. Create a pull request.

For creating/editing UML diagrams you need ObjectAid UML Explorer for Eclipse.

For inspiration check out the following sources:

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.

This project is licensed under the terms of the MIT license.

About

Design pattern samples implemented in Java

http://iluwatar.github.io/java-design-patterns/

License:MIT License


Languages

Language:Java 97.8%Language:HTML 1.7%Language:Gherkin 0.3%Language:CSS 0.2%Language:JavaScript 0.0%