Skip to content

Commit dd0ca2d

Browse files
authored
Merge pull request iluwatar#572 from 4lexis/master
iluwatar#567 Marker Interface pull request
2 parents ae1d9cf + f2e35ec commit dd0ca2d

13 files changed

+244
-0
lines changed

marker/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

marker/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: pattern
3+
title: Marker Interface
4+
folder: marker
5+
permalink: /patterns/marker/
6+
categories: Design
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
Using empty interfaces as markers to distinguish special treated objects.
14+
15+
![alt text](./etc/MarkerDiagram.png "Marker Interface")
16+
17+
## Applicability
18+
Use the Marker Interface pattern when
19+
20+
* you want to identify the special objects from normal objects (to treat them differently)
21+
* you want to mark that some object is available for certain sort of operations
22+
23+
## Real world examples
24+
25+
* [javase.8.docs.api.java.io.Serializable](https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html)
26+
* [javase.8.docs.api.java.lang.Cloneable](https://docs.oracle.com/javase/8/docs/api/java/lang/Cloneable.html)
27+
28+
## Credits
29+
30+
* [Effective Java 2nd Edition by Joshua Bloch](https://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683)

marker/etc/MarkerDiagram.png

6.31 KB
Loading

marker/etc/MarkerDiagram.ucls

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="Guard" project="marker" file="/marker/src/main/java/Guard.java" binary="false"
5+
corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="416" y="348"/>
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>
12+
</class>
13+
<interface id="2" language="java" name="Permission" project="marker" file="/marker/src/main/java/Permission.java"
14+
binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="261" y="175"/>
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+
</interface>
22+
<class id="3" language="java" name="Thief" project="marker" file="/marker/src/main/java/Thief.java" binary="false"
23+
corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="236" y="355"/>
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+
<realization id="4">
32+
<end type="SOURCE" refId="1"/>
33+
<end type="TARGET" refId="2"/>
34+
</realization>
35+
<classifier-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+
</classifier-display>
40+
<association-display labels="true" multiplicity="true"/>
41+
</class-diagram>

marker/pom.xml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
The MIT License
4+
Copyright (c) 2014-2016 Ilkka Seppälä
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+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
-->
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<parent>
25+
<artifactId>java-design-patterns</artifactId>
26+
<groupId>com.iluwatar</groupId>
27+
<version>1.16.0-SNAPSHOT</version>
28+
</parent>
29+
<modelVersion>4.0.0</modelVersion>
30+
31+
<artifactId>marker</artifactId>
32+
<dependencies>
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
41+
</project>

marker/src/main/java/App.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import org.slf4j.Logger;
2+
import org.slf4j.LoggerFactory;
3+
4+
/**
5+
* Created by Alexis on 28-Apr-17.
6+
* With Marker interface idea is to make empty interface and extend it.
7+
* Basically it is just to identify the special objects from normal objects.
8+
* Like in case of serialization , objects that need to be serialized must implement serializable interface
9+
* (it is empty interface) and down the line writeObject() method must be checking
10+
* if it is a instance of serializable or not.
11+
* <p>
12+
* Marker interface vs annotation
13+
* Marker interfaces and marker annotations both have their uses,
14+
* neither of them is obsolete or always better then the other one.
15+
* If you want to define a type that does not have any new methods associated with it,
16+
* a marker interface is the way to go.
17+
* If you want to mark program elements other than classes and interfaces,
18+
* to allow for the possibility of adding more information to the marker in the future,
19+
* or to fit the marker into a framework that already makes heavy use of annotation types,
20+
* then a marker annotation is the correct choice
21+
*/
22+
public class App {
23+
24+
/**
25+
* Program entry point
26+
*
27+
* @param args command line args
28+
*/
29+
public static void main(String[] args) {
30+
31+
final Logger logger = LoggerFactory.getLogger(App.class);
32+
Guard guard = new Guard();
33+
Thief thief = new Thief();
34+
35+
if (guard instanceof Permission) {
36+
guard.enter();
37+
} else {
38+
logger.info("You have no permission to enter, please leave this area");
39+
}
40+
41+
if (thief instanceof Permission) {
42+
thief.steal();
43+
} else {
44+
thief.doNothing();
45+
}
46+
}
47+
}
48+

marker/src/main/java/Guard.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import org.slf4j.Logger;
2+
import org.slf4j.LoggerFactory;
3+
4+
/**
5+
* Class defining Guard
6+
*/
7+
public class Guard implements Permission {
8+
9+
private static final Logger LOGGER = LoggerFactory.getLogger(Guard.class);
10+
11+
protected static void enter() {
12+
13+
LOGGER.info("You can enter");
14+
}
15+
}

marker/src/main/java/Permission.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Interface without any methods
3+
* Marker interface is based on that assumption
4+
*/
5+
public interface Permission {
6+
}

marker/src/main/java/Thief.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import org.slf4j.Logger;
2+
import org.slf4j.LoggerFactory;
3+
4+
/**
5+
* Class defining Thief
6+
*/
7+
public class Thief {
8+
9+
private static final Logger LOGGER = LoggerFactory.getLogger(Thief.class);
10+
11+
protected static void steal() {
12+
LOGGER.info("Steal valuable items");
13+
}
14+
15+
protected static void doNothing() {
16+
LOGGER.info("Pretend nothing happened and just leave");
17+
}
18+
}

marker/src/test/java/AppTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import org.junit.Test;
2+
3+
/**
4+
* Application test
5+
*/
6+
public class AppTest {
7+
8+
@Test
9+
public void test() {
10+
String[] args = {};
11+
App.main(args);
12+
}
13+
}

marker/src/test/java/GuardTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.junit.Test;
2+
3+
import static org.hamcrest.CoreMatchers.instanceOf;
4+
import static org.junit.Assert.assertThat;
5+
6+
/**
7+
* Guard test
8+
*/
9+
public class GuardTest {
10+
11+
@Test
12+
public void testGuard() {
13+
Guard guard = new Guard();
14+
assertThat(guard, instanceOf(Permission.class));
15+
}
16+
}

marker/src/test/java/ThiefTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import org.junit.Test;
2+
3+
import static org.junit.Assert.assertFalse;
4+
5+
/**
6+
* Thief test
7+
*/
8+
public class ThiefTest {
9+
@Test
10+
public void testThief() {
11+
Thief thief = new Thief();
12+
assertFalse(thief instanceof Permission);
13+
}
14+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
<module>converter</module>
141141
<module>guarded-suspension</module>
142142
<module>balking</module>
143+
<module>marker</module>
143144
</modules>
144145

145146
<dependencyManagement>

0 commit comments

Comments
 (0)