README.md 6.1 KB
Newer Older
M
Markus 已提交
1 2 3 4 5
<!-- the line below needs to be an empty line C: (its because kramdown isnt
     that smart and dearly wants an empty line before a heading to be able to
     display it as such, e.g. website) -->

# Design pattern samples in Java
I
Ilkka Seppälä 已提交
6

T
The Gitter Badger 已提交
7 8
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

9
[![Build status](https://travis-ci.org/iluwatar/java-design-patterns.svg?branch=master)](https://travis-ci.org/iluwatar/java-design-patterns) [![Coverage Status](https://coveralls.io/repos/iluwatar/java-design-patterns/badge.svg?branch=master)](https://coveralls.io/r/iluwatar/java-design-patterns?branch=master) <a href="https://scan.coverity.com/projects/5634">
I
Ilkka Seppälä 已提交
10 11 12
  <img alt="Coverity Scan Build Status"
       src="https://scan.coverity.com/projects/5634/badge.svg"/>
</a>
I
Ilkka Seppälä 已提交
13

M
Markus 已提交
14 15 16 17

<a name="top"/>

# <a name="toc">Table of Contents</a>
18
 - <a href="#introduction">Introduction</a>
19
 - <a href="#contribute">How to contribute</a>
20 21 22
 - <a href="#faq">Frequently Asked Questions</a>
 - <a href="#credits">Credits</a>
 - <a href="#license">License</a>
M
Markus 已提交
23

24

25
# <a name="introduction">Introduction</a> [&#8593;](#top)
26

27 28
Design patterns are formalized best practices that the programmer can use to
solve common problems when designing an application or system.
29

30 31
Design patterns can speed up the development process by providing tested, proven
development paradigms.
32

33 34 35
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.
36

J
joshzambales 已提交
37

38 39 40 41 42
# <a name="contribute">How to contribute</a> [&#8593;](#top)

If you are willing to contribute to the project you will find the relevant information in our [developer wiki](https://github.com/iluwatar/java-design-patterns/wiki).


43
# <a name="faq">Frequently asked questions</a> [&#8593;](#top)
44

45
**<a id="Q1">Q: What is the difference between State and Strategy patterns?</a>**
I
Ilkka Seppälä 已提交
46

47 48 49 50 51
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.
52

53
**<a id="Q2">Q: What is the difference between Strategy and Template Method patterns?</a>**
I
Ilkka Seppälä 已提交
54

55 56
In Template Method the algorithm is chosen at compile time via inheritance.
With Strategy pattern the algorithm is chosen at runtime via composition.
I
Ilkka Seppälä 已提交
57

58
**<a id="Q3">Q: What is the difference between Proxy and Decorator patterns?</a>**
I
Ilkka Seppälä 已提交
59

60 61
The difference is the intent of the patterns. While Proxy controls access to
the object Decorator is used to add responsibilities to the object.
62

I
Ilkka Seppala 已提交
63 64
**<a id="Q4">Q: What is the difference between Chain of Responsibility and Intercepting Filter patterns?</a>**

65 66 67 68 69
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.
70

71 72
**<a id="Q5">Q: What is the difference between Visitor and Double Dispatch patterns?</a>**

73 74 75 76
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.
77

78 79 80 81
**<a id="Q6">Q: What are the differences between Flyweight and Object Pool patterns?</a>**

They differ in the way they are used.

82 83 84 85 86
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.
87

88 89
In contrast, a Flyweight object is singleton, and it can be used simultaneously
by multiple clients.
90

91 92 93 94
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.
95

96 97 98 99
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.
100

101

102
# <a name="credits">Credits</a> [&#8593;](#top)
I
Ilkka Seppälä 已提交
103

I
Ilkka Seppälä 已提交
104 105
* [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)
106
* [Java Generics and Collections](http://www.amazon.com/Java-Generics-Collections-Maurice-Naftalin/dp/0596527756/)
107
* [Let's Modify the Objects-First Approach into Design-Patterns-First](http://edu.pecinovsky.cz/papers/2006_ITiCSE_Design_Patterns_First.pdf)
108
* [Pattern Languages of Program Design](http://www.amazon.com/Pattern-Languages-Program-Design-Coplien/dp/0201607344/ref=sr_1_1)
I
Ilkka Seppala 已提交
109
* [Presentation Tier Patterns](http://www.javagyan.com/tutorials/corej2eepatterns/presentation-tier-patterns)
I
Ilkka Seppala 已提交
110
* [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)
I
Ilkka Seppala 已提交
111
* [Patterns of Enterprise Application Architecture](http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420)
I
Ilkka Seppala 已提交
112
* [Spring Data](http://www.amazon.com/Spring-Data-Mark-Pollack/dp/1449323952/ref=sr_1_1)
I
Ilkka Seppala 已提交
113
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)
114
* [Pattern Oriented Software Architecture Vol I-V](http://www.amazon.com/Pattern-Oriented-Software-Architecture-Volume-Patterns/dp/0471958697)
I
Ilkka Seppälä 已提交
115

116

117
# <a name="license">License</a> [&#8593;](#top)
I
Ilkka Seppälä 已提交
118

J
joshzambales 已提交
119
This project is licensed under the terms of the MIT license.