Skip to content

Commit 0a42771

Browse files
author
daniel-bryla
committed
* 'master' of https://github.com/iluwatar/java-design-patterns: (27 commits) Remove use of coveralls-maven-plugin (sonarqube.com covers this) Add SonarQube.com badge Fix environment variable Add Travis instructions for SonarQube.com analysis Adds more criticism to Singleton pattern. Event Based Asynchronous pattern: Add missing license header and puml diagram Changed config to non-interactive Moved config into a separate dir Unused import removed. End process logic clause has been corrected. Caching pattern: Documentation and diagram Fixes iluwatar#437. Adds criticism to Singleton pattern. Alter JUnit tests to run in lesser time. Updated version snapshot to 1.14.0 Changes based on review feedback. Closes iluwatar#436. Adds criticism to service locator pattern. Caching pattern: Implementation of Cache-Aside pattern Caching pattern: Style fix for null check Caching pattern: Refactor LRU cache to avoid NPE and unnecessary cache lookup Caching pattern: Refactor shutdown hook to use method reference ...
2 parents e138163 + 124fd33 commit 0a42771

File tree

35 files changed

+1336
-185
lines changed

35 files changed

+1336
-185
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ env:
66
global:
77
- GH_REF: github.com/iluwatar/java-design-patterns.git
88
- secure: LxTDuNS/rBWIvKkaEqr79ImZAe48mCdoYCF41coxNXgNoippo4GIBArknqtv+XvdkiuRZ1yGyj6pn8GU33c/yn+krddTUkVCwTbVatbalW5jhQjDbHYym/JcxaK9ZS/3JTeGcWrBgiPqHEEDhCf26vPZsXoMSeVCEORVKTp1BSg=
9+
- secure: "eoWlW9GyTJY04P8K3pxayXwU9/hmptQg/LfirispQkV9YvmziCfSzXnatnBhNfud98sCzY8BScXnb+OWLTnjLKpId4rtEqb0aJ40Jc32cUKzgzFAUn7cNcDAbUIfyPAGVqyQqfj/11wYSADwWMMOPlW97ExUtoyiH2WenXuRHso="
910

1011
before_install:
1112
- export DISPLAY=:99.0
@@ -16,7 +17,7 @@ install:
1617
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V -e
1718

1819
after_success:
19-
- mvn clean test jacoco:report coveralls:report
20+
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.host.url=https://sonarqube.com -Dsonar.login=$SONAR_TOKEN
2021
- bash update-ghpages.sh
2122

2223
# use latest java version available instead of travis default

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# Design patterns implemented in Java
66

77
[![Build status](https://travis-ci.org/iluwatar/java-design-patterns.svg?branch=master)](https://travis-ci.org/iluwatar/java-design-patterns)
8-
[![Coverage Status](https://coveralls.io/repos/iluwatar/java-design-patterns/badge.svg?branch=master)](https://coveralls.io/r/iluwatar/java-design-patterns?branch=master)
98
[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
109
[![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)
10+
[![Quality Gate](https://sonarqube.com/api/badges/gate?key=com.iluwatar%3Ajava-design-patterns)](https://sonarqube.com/dashboard/index/com.iluwatar%3Ajava-design-patterns)
1111

1212
# Introduction
1313

async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callba
5757

5858
@Override
5959
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException {
60-
if (asyncResult.isCompleted()) {
61-
return asyncResult.getValue();
62-
} else {
60+
if (!asyncResult.isCompleted()) {
6361
asyncResult.await();
64-
return asyncResult.getValue();
6562
}
63+
return asyncResult.getValue();
6664
}
6765

6866
/**

async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.concurrent.ExecutionException;
3232

3333
import static org.junit.Assert.*;
34-
import static org.mockito.Matchers.eq;
3534
import static org.mockito.Mockito.*;
3635
import static org.mockito.internal.verification.VerificationModeFactory.times;
3736

caching/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ Use the Caching pattern(s) when
2727

2828
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
2929
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
30+
* [Cache-Aside](https://msdn.microsoft.com/en-us/library/dn589799.aspx)

caching/etc/caching.png

57.2 KB
Loading

caching/etc/caching.ucls

Lines changed: 87 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,90 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3-
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4-
<class id="1" language="java" name="main.java.com.wssia.caching.App" project="CachingPatterns"
5-
file="/CachingPatterns/src/main/java/com/wssia/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
6-
<position height="-1" width="-1" x="249" y="150"/>
7-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8-
sort-features="false" accessors="true" visibility="true">
9-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10-
<operations public="true" package="true" protected="true" private="true" static="true"/>
11-
</display>
2+
<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
3+
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
4+
router="FAN">
5+
<class id="1" language="java" name="com.iluwatar.caching.CacheStore" project="caching"
6+
file="/caching/src/main/java/com/iluwatar/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
7+
<position height="-1" width="-1" x="322" y="444"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
11+
<operations public="true" package="true" protected="true" private="true" static="true"/>
12+
</display>
13+
</class>
14+
<class id="2" language="java" name="com.iluwatar.caching.LruCache" project="caching"
15+
file="/caching/src/main/java/com/iluwatar/caching/LruCache.java" binary="false" corner="BOTTOM_RIGHT">
16+
<position height="-1" width="-1" x="700" y="446"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</class>
23+
<enumeration id="3" language="java" name="com.iluwatar.caching.CachingPolicy" project="caching"
24+
file="/caching/src/main/java/com/iluwatar/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
25+
<position height="-1" width="-1" x="943" y="122"/>
26+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
27+
sort-features="false" accessors="true" visibility="true">
28+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
29+
<operations public="true" package="true" protected="true" private="true" static="true"/>
30+
</display>
31+
</enumeration>
32+
<class id="4" language="java" name="com.iluwatar.caching.DbManager" project="caching"
33+
file="/caching/src/main/java/com/iluwatar/caching/DbManager.java" binary="false" corner="BOTTOM_RIGHT">
34+
<position height="-1" width="-1" x="1011" y="426"/>
35+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
36+
sort-features="false" accessors="true" visibility="true">
37+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
38+
<operations public="true" package="true" protected="true" private="true" static="true"/>
39+
</display>
40+
</class>
41+
<class id="5" language="java" name="com.iluwatar.caching.App" project="caching"
42+
file="/caching/src/main/java/com/iluwatar/caching/App.java" binary="false" corner="BOTTOM_RIGHT">
43+
<position height="-1" width="-1" x="304" y="95"/>
44+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
45+
sort-features="false" accessors="true" visibility="true">
46+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
47+
<operations public="true" package="true" protected="true" private="true" static="true"/>
48+
</display>
49+
</class>
50+
<class id="6" language="java" name="com.iluwatar.caching.AppManager" project="caching"
51+
file="/caching/src/main/java/com/iluwatar/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
52+
<position height="-1" width="-1" x="604" y="122"/>
53+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
54+
sort-features="false" accessors="true" visibility="true">
55+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
56+
<operations public="true" package="true" protected="true" private="true" static="true"/>
57+
</display>
58+
</class>
59+
<class id="7" language="java" name="com.iluwatar.caching.UserAccount" project="caching"
60+
file="/caching/src/main/java/com/iluwatar/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
61+
<position height="-1" width="-1" x="1161" y="139"/>
62+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
63+
sort-features="false" accessors="true" visibility="true">
64+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
65+
<operations public="true" package="true" protected="true" private="true" static="true"/>
66+
</display>
1267
</class>
13-
<class id="2" language="java" name="main.java.com.wssia.caching.AppManager" project="CachingPatterns"
14-
file="/CachingPatterns/src/main/java/com/wssia/caching/AppManager.java" binary="false" corner="BOTTOM_RIGHT">
15-
<position height="-1" width="-1" x="502" y="163"/>
16-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17-
sort-features="false" accessors="true" visibility="true">
18-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19-
<operations public="true" package="true" protected="true" private="true" static="true"/>
20-
</display>
21-
</class>
22-
<class id="3" language="java" name="main.java.com.wssia.caching.CacheStore" project="CachingPatterns"
23-
file="/CachingPatterns/src/main/java/com/wssia/caching/CacheStore.java" binary="false" corner="BOTTOM_RIGHT">
24-
<position height="-1" width="-1" x="537" y="436"/>
25-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26-
sort-features="false" accessors="true" visibility="true">
27-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28-
<operations public="true" package="true" protected="true" private="true" static="true"/>
29-
</display>
30-
</class>
31-
<enumeration id="4" language="java" name="main.java.com.wssia.caching.CachingPolicy" project="CachingPatterns"
32-
file="/CachingPatterns/src/main/java/com/wssia/caching/CachingPolicy.java" binary="false" corner="BOTTOM_RIGHT">
33-
<position height="-1" width="-1" x="789" y="162"/>
34-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
35-
sort-features="false" accessors="true" visibility="true">
36-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
37-
<operations public="true" package="true" protected="true" private="true" static="true"/>
38-
</display>
39-
</enumeration>
40-
<class id="5" language="java" name="main.java.com.wssia.caching.DBManager" project="CachingPatterns"
41-
file="/CachingPatterns/src/main/java/com/wssia/caching/DBManager.java" binary="false" corner="BOTTOM_RIGHT">
42-
<position height="-1" width="-1" x="1137" y="134"/>
43-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
44-
sort-features="false" accessors="true" visibility="true">
45-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
46-
<operations public="true" package="true" protected="true" private="true" static="true"/>
47-
</display>
48-
</class>
49-
<class id="6" language="java" name="main.java.com.wssia.caching.LRUCache" project="CachingPatterns"
50-
file="/CachingPatterns/src/main/java/com/wssia/caching/LRUCache.java" binary="false" corner="BOTTOM_RIGHT">
51-
<position height="-1" width="-1" x="884" y="435"/>
52-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
53-
sort-features="false" accessors="true" visibility="true">
54-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
55-
<operations public="true" package="true" protected="true" private="true" static="true"/>
56-
</display>
57-
</class>
58-
<class id="7" language="java" name="main.java.com.wssia.caching.UserAccount" project="CachingPatterns"
59-
file="/CachingPatterns/src/main/java/com/wssia/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
60-
<position height="-1" width="-1" x="1140" y="405"/>
61-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
62-
sort-features="false" accessors="true" visibility="true">
63-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
64-
<operations public="true" package="true" protected="true" private="true" static="true"/>
65-
</display>
66-
</class>
67-
<class id="8" language="java" name="test.java.com.wssia.caching.AppTest" project="CachingPatterns"
68-
file="/CachingPatterns/src/test/java/com/wssia/caching/AppTest.java" binary="false" corner="BOTTOM_RIGHT">
69-
<position height="-1" width="-1" x="251" y="374"/>
70-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
71-
sort-features="false" accessors="true" visibility="true">
72-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
73-
<operations public="true" package="true" protected="true" private="true" static="true"/>
74-
</display>
75-
</class>
76-
<association id="9">
77-
<end type="SOURCE" refId="2" navigable="false">
78-
<attribute id="10" name="cachingPolicy"/>
79-
<multiplicity id="11" minimum="0" maximum="1"/>
80-
</end>
81-
<end type="TARGET" refId="4" navigable="true"/>
82-
<display labels="true" multiplicity="true"/>
83-
</association>
84-
<association id="12">
85-
<end type="SOURCE" refId="8" navigable="false">
86-
<attribute id="13" name="app"/>
87-
<multiplicity id="14" minimum="0" maximum="1"/>
88-
</end>
89-
<end type="TARGET" refId="1" navigable="true"/>
90-
<display labels="true" multiplicity="true"/>
91-
</association>
92-
<association id="15">
93-
<end type="SOURCE" refId="3" navigable="false">
94-
<attribute id="16" name="cache"/>
95-
<multiplicity id="17" minimum="0" maximum="1"/>
96-
</end>
97-
<end type="TARGET" refId="6" navigable="true"/>
98-
<display labels="true" multiplicity="true"/>
99-
</association>
100-
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
101-
sort-features="false" accessors="true" visibility="true">
102-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
103-
<operations public="true" package="true" protected="true" private="true" static="true"/>
104-
</classifier-display>
68+
<association id="8">
69+
<end type="SOURCE" refId="1" navigable="false">
70+
<attribute id="9" name="cache"/>
71+
<multiplicity id="10" minimum="0" maximum="1"/>
72+
</end>
73+
<end type="TARGET" refId="2" navigable="true"/>
74+
<display labels="true" multiplicity="true"/>
75+
</association>
76+
<association id="11">
77+
<end type="SOURCE" refId="6" navigable="false">
78+
<attribute id="12" name="cachingPolicy"/>
79+
<multiplicity id="13" minimum="0" maximum="1"/>
80+
</end>
81+
<end type="TARGET" refId="3" navigable="true"/>
82+
<display labels="true" multiplicity="true"/>
83+
</association>
84+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
85+
sort-features="false" accessors="true" visibility="true">
86+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
87+
<operations public="true" package="true" protected="true" private="true" static="true"/>
88+
</classifier-display>
10589
<association-display labels="true" multiplicity="true"/>
106-
</class-diagram>
90+
</class-diagram>

caching/src/main/java/com/iluwatar/caching/App.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@
2929
*
3030
* The Caching pattern describes how to avoid expensive re-acquisition of resources by not releasing
3131
* the resources immediately after their use. The resources retain their identity, are kept in some
32-
* fast-access storage, and are re-used to avoid having to acquire them again. There are three main
33-
* caching strategies/techniques in this pattern; each with their own pros and cons. They are:
32+
* fast-access storage, and are re-used to avoid having to acquire them again. There are four main
33+
* caching strategies/techniques in this pattern; each with their own pros and cons. They are;
3434
* <code>write-through</code> which writes data to the cache and DB in a single transaction,
35-
* <code>write-around</code> which writes data immediately into the DB instead of the cache, and
35+
* <code>write-around</code> which writes data immediately into the DB instead of the cache,
3636
* <code>write-behind</code> which writes data into the cache initially whilst the data is only
37-
* written into the DB when the cache is full. The <code>read-through</code> strategy is also
38-
* included in the mentioned three strategies -- returns data from the cache to the caller <b>if</b>
39-
* it exists <b>else</b> queries from DB and stores it into the cache for future use. These
40-
* strategies determine when the data in the cache should be written back to the backing store (i.e.
41-
* Database) and help keep both data sources synchronized/up-to-date. This pattern can improve
42-
* performance and also helps to maintain consistency between data held in the cache and the data in
43-
* the underlying data store.
37+
* written into the DB when the cache is full, and <code>cache-aside</code> which pushes the
38+
* responsibility of keeping the data synchronized in both data sources to the application itself.
39+
* The <code>read-through</code> strategy is also included in the mentioned four strategies --
40+
* returns data from the cache to the caller <b>if</b> it exists <b>else</b> queries from DB and
41+
* stores it into the cache for future use. These strategies determine when the data in the cache
42+
* should be written back to the backing store (i.e. Database) and help keep both data sources
43+
* synchronized/up-to-date. This pattern can improve performance and also helps to maintain
44+
* consistency between data held in the cache and the data in the underlying data store.
4445
* <p>
4546
* In this example, the user account ({@link UserAccount}) entity is used as the underlying
4647
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
47-
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The three
48+
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
4849
* strategies are individually tested. The testing of the cache is restricted towards saving and
4950
* querying of user accounts from the underlying data store ( {@link DbManager}). The main class (
5051
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
@@ -80,6 +81,7 @@ public static void main(String[] args) {
8081
app.useReadAndWriteThroughStrategy();
8182
app.useReadThroughAndWriteAroundStrategy();
8283
app.useReadThroughAndWriteBehindStrategy();
84+
app.useCacheAsideStategy();
8385
}
8486

8587
/**
@@ -142,4 +144,26 @@ public void useReadThroughAndWriteBehindStrategy() {
142144
AppManager.find("004");
143145
LOGGER.info(AppManager.printCacheContent());
144146
}
147+
148+
/**
149+
* Cache-Aside
150+
*/
151+
public void useCacheAsideStategy() {
152+
System.out.println("# CachingPolicy.ASIDE");
153+
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
154+
System.out.println(AppManager.printCacheContent());
155+
156+
UserAccount userAccount3 = new UserAccount("003", "Adam", "He likes food.");
157+
UserAccount userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
158+
UserAccount userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
159+
AppManager.save(userAccount3);
160+
AppManager.save(userAccount4);
161+
AppManager.save(userAccount5);
162+
163+
System.out.println(AppManager.printCacheContent());
164+
AppManager.find("003");
165+
System.out.println(AppManager.printCacheContent());
166+
AppManager.find("004");
167+
System.out.println(AppManager.printCacheContent());
168+
}
145169
}

0 commit comments

Comments
 (0)