Skip to content

Commit 6fe219d

Browse files
authored
Pr/1235 Review (iluwatar#1250)
* Add simple implementation for strangler pattern. * Add strangler pattern in pom.xml. * change package name * Revert "change package name" This reverts commit 430bd90. * Code review for strangler Delete final of method parameters. Add final to private members. Change package name. * Revert "Code review for strangler" This reverts commit d506356. * Revert "Revert "Code review for strangler"" This reverts commit c8fd65f. * Remove unnecessary files
1 parent 5192beb commit 6fe219d

19 files changed

Lines changed: 927 additions & 0 deletions

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
<module>combinator</module>
191191
<module>update-method</module>
192192
<module>leader-followers</module>
193+
<module>strangler</module>
193194
<module>arrange-act-assert</module>
194195
</modules>
195196

strangler/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
layout: pattern
3+
title: Strangler
4+
folder: strangler
5+
permalink: /patterns/strangler/
6+
categories: Structural
7+
tags:
8+
- Extensibility
9+
---
10+
11+
## Intent
12+
Incrementally migrate a legacy system by gradually replacing specific pieces of functionality
13+
with new applications and services. As features from the legacy system are replaced, the new
14+
system eventually covers all the old system's features and may has its own new features, then
15+
strangling the old system and allowing you to decommission it.
16+
17+
## Class diagram
18+
![alt text](./etc/strangler.png "Strangler")
19+
20+
## Applicability
21+
This strangler pattern is a safe way to phase one thing out for something better, cheaper, or
22+
more expandable. Especially when you want to update legacy system with new techniques and need
23+
continuously develop new features at the same time. Note that this pattern indeed need extra effort,
24+
so usually use it when the system is not so simple.
25+
26+
## Credits
27+
28+
* [Strangler pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/strangler#context-and-problem)
29+
* [Legacy Application Strangulation : Case Studies](https://paulhammant.com/2013/07/14/legacy-application-strangulation-case-studies/)
30+
31+

strangler/etc/strangler.png

76.1 KB
Loading

strangler/etc/strangler.puml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
@startuml
2+
3+
package com.iluwatar.strangler {
4+
class App {
5+
+ main(args : String[]) {static}
6+
}
7+
8+
class OldArithmetic {
9+
- LOGGER : Logger {static}
10+
- VERSION : String {static}
11+
- source : OldSource
12+
+ sum(nums : int...)
13+
+ mul(nums : int...)
14+
}
15+
16+
class HalfArithmetic {
17+
- LOGGER : Logger {static}
18+
- VERSION : String {static}
19+
- oldSource : OldSource
20+
- newSource : HalfSource
21+
+ sum(nums : int...)
22+
+ mul(nums : int...)
23+
+ ifHasZero(nums : int...)
24+
}
25+
26+
class NewArithmetic {
27+
- LOGGER : Logger {static}
28+
- VERSION : String {static}
29+
- source : NewSource
30+
+ sum(nums : int...)
31+
+ mul(nums : int...)
32+
+ ifHasZero(nums : int...)
33+
}
34+
35+
class OldSource {
36+
- LOGGER : Logger {static}
37+
- VERSION : String {static}
38+
+ accumulateSum(nums : int...)
39+
+ accumulateMul(nums : int...)
40+
}
41+
42+
class HalfSource {
43+
- LOGGER : Logger {static}
44+
- VERSION : String {static}
45+
+ accumulateSum(nums : int...)
46+
+ ifNonZero(nums : int...)
47+
}
48+
49+
class NewSource {
50+
- LOGGER : Logger {static}
51+
- VERSION : String {static}
52+
+ accumulateSum(nums : int...)
53+
+ accumulateMul(nums : int...)
54+
+ ifNonZero(nums : int...)
55+
}
56+
}
57+
OldArithmetic o--> OldSource
58+
HalfArithmetic o--> OldSource
59+
HalfArithmetic o--> HalfSource
60+
NewArithmetic o--> NewSource
61+
@enduml

strangler/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.23.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>strangler</artifactId>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.junit.jupiter</groupId>
41+
<artifactId>junit-jupiter-engine</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-assembly-plugin</artifactId>
50+
<executions>
51+
<execution>
52+
<configuration>
53+
<archive>
54+
<manifest>
55+
<mainClass>com.iluwatar.strangler.App</mainClass>
56+
</manifest>
57+
</archive>
58+
</configuration>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
66+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.strangler;
25+
26+
/**
27+
*
28+
* <p>The Strangler pattern is a software design pattern that incrementally migrate a legacy
29+
* system by gradually replacing specific pieces of functionality with new applications and
30+
* services. As features from the legacy system are replaced, the new system eventually
31+
* replaces all of the old system's features, strangling the old system and allowing you
32+
* to decommission it.</p>
33+
*
34+
* <p>This pattern is not only about updating but also enhancement.</p>
35+
*
36+
* <p>In this example, {@link OldArithmetic} indicates old system and its implementation depends
37+
* on its source ({@link OldSource}). Now we tend to update system with new techniques and
38+
* new features. In reality, the system may too complex, so usually need gradual migration.
39+
* {@link HalfArithmetic} indicates system in the process of migration, its implementation
40+
* depends on old one ({@link OldSource}) and under development one ({@link HalfSource}). The
41+
* {@link HalfSource} covers part of {@link OldSource} and add new functionality. You can release
42+
* this version system with new features, which also supports old version system functionalities.
43+
* After whole migration, the new system ({@link NewArithmetic}) only depends on new source
44+
* ({@link NewSource}).</p>
45+
*
46+
*/
47+
public class App {
48+
/**
49+
* Program entry point.
50+
* @param args command line args
51+
*/
52+
public static void main(final String[] args) {
53+
final var nums = new int[]{1, 2, 3, 4, 5};
54+
//Before migration
55+
final var oldSystem = new OldArithmetic(new OldSource());
56+
oldSystem.sum(nums);
57+
oldSystem.mul(nums);
58+
//In process of migration
59+
final var halfSystem = new HalfArithmetic(new HalfSource(), new OldSource());
60+
halfSystem.sum(nums);
61+
halfSystem.mul(nums);
62+
halfSystem.ifHasZero(nums);
63+
//After migration
64+
final var newSystem = new NewArithmetic(new NewSource());
65+
newSystem.sum(nums);
66+
newSystem.mul(nums);
67+
newSystem.ifHasZero(nums);
68+
}
69+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.strangler;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* System under migration. Depends on old version source ({@link OldSource}) and
31+
* developing one ({@link HalfSource}).
32+
*/
33+
public class HalfArithmetic {
34+
private static final Logger LOGGER = LoggerFactory.getLogger(HalfArithmetic.class);
35+
private static final String VERSION = "1.5";
36+
37+
private final HalfSource newSource;
38+
private final OldSource oldSource;
39+
40+
public HalfArithmetic(HalfSource newSource, OldSource oldSource) {
41+
this.newSource = newSource;
42+
this.oldSource = oldSource;
43+
}
44+
45+
/**
46+
* Accumulate sum.
47+
* @param nums numbers need to add together
48+
* @return accumulate sum
49+
*/
50+
public int sum(int... nums) {
51+
LOGGER.info("Arithmetic sum {}", VERSION);
52+
return newSource.accumulateSum(nums);
53+
}
54+
55+
/**
56+
* Accumulate multiplication.
57+
* @param nums numbers need to multiply together
58+
* @return accumulate multiplication
59+
*/
60+
public int mul(int... nums) {
61+
LOGGER.info("Arithmetic mul {}", VERSION);
62+
return oldSource.accumulateMul(nums);
63+
}
64+
65+
/**
66+
* Chech if has any zero.
67+
* @param nums numbers need to check
68+
* @return if has any zero, return true, else, return false
69+
*/
70+
public boolean ifHasZero(int... nums) {
71+
LOGGER.info("Arithmetic check zero {}", VERSION);
72+
return !newSource.ifNonZero(nums);
73+
}
74+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.strangler;
25+
26+
import java.util.Arrays;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
/**
31+
* Source under development. Replace part of old source and has added some new features.
32+
*/
33+
public class HalfSource {
34+
private static final Logger LOGGER = LoggerFactory.getLogger(HalfSource.class);
35+
private static final String VERSION = "1.5";
36+
37+
/**
38+
* Implement accumulate sum with new technique.
39+
* Replace old one in {@link OldSource}
40+
*/
41+
public int accumulateSum(int... nums) {
42+
LOGGER.info("Source module {}", VERSION);
43+
return Arrays.stream(nums).reduce(0, Integer::sum);
44+
}
45+
46+
/**
47+
* Check if all number is not zero.
48+
* New feature.
49+
*/
50+
public boolean ifNonZero(int... nums) {
51+
LOGGER.info("Source module {}", VERSION);
52+
return Arrays.stream(nums).allMatch(num -> num != 0);
53+
}
54+
}

0 commit comments

Comments
 (0)