Git Product home page Git Product logo

java-design-patterns's Introduction

Design pattern samples in Java.

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

Build status Coverage Status Coverity Scan Build Status

- Introduction - List of Design Patterns - Creational Patterns - Structural Patterns - Behavioral Patterns - Concurrency Patterns - Presentation Tier Patterns - Business Tier Patterns - Architectural Patterns - Integration Patterns - Idioms - Frequently Asked Questions - How to contribute - Versioning - Credits - License

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.

Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented.

Structural patterns are concerned with how classes and objects are composed to form larger structures.

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.

Concurrency patterns are those types of design patterns that deal with the multi-threaded programming paradigm.

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.

An architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context.

Integration patterns are concerned with how software applications communicate and exchange data.

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.

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. 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.
  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.

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 README.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.

java-design-patterns's People

Contributors

alwayswithme avatar annemsujan avatar dmitz avatar gitter-badger avatar iluwatar avatar intfloat avatar joningiwork avatar joshzambales avatar llitfkitfk avatar lordmat0 avatar mattbdean avatar mbirkenkamp avatar mingliangfeng avatar mortezaadi avatar msaifasif avatar noragami avatar npathai avatar ruslanpa avatar sideris avatar vehpsr avatar yusufaytas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.