zhanghui026 / 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

Table of Contents

Introduction

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.

How to contribute

If you are willing to contribute to the project you will find the relevant information in our developer wiki.

Frequently asked questions

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.

Credits

License

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%