File tree 9 files changed +33
-31
lines changed
9 files changed +33
-31
lines changed Original file line number Diff line number Diff line change @@ -10,20 +10,20 @@ tags:
10
10
---
11
11
12
12
## Intent
13
- Using empy interfaces as markers to distinguish special treated objects.
13
+ Using empty interfaces as markers to distinguish special treated objects.
14
14
15
15
![ alt text] ( ./etc/MarkerDiagram.png " Marker Interface ")
16
16
17
17
## Applicability
18
18
Use the Marker Interface pattern when
19
19
20
- * you want to identify the special objects from normal objects
21
- * define a type that is implemented by instances of the marked class, marker annotations can not do that
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
22
23
23
## Real world examples
24
24
25
- * [ javase.7 .docs.api.java.io.Serializable] ( https://docs.oracle.com/javase/7 /docs/api/java/io/Serializable.html )
26
- * [ javase.7 .docs.api.java.lang.Cloneable] ( https://docs.oracle.com/javase/7 /docs/api/java/lang/Cloneable.html )
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
27
28
28
## Credits
29
29
Original file line number Diff line number Diff line change 30
30
31
31
<artifactId >marker</artifactId >
32
32
<dependencies >
33
- <dependency >
34
- <groupId >org.junit.jupiter</groupId >
35
- <artifactId >junit-jupiter-api</artifactId >
36
- <version >RELEASE</version >
37
- </dependency >
38
- <dependency >
39
- <groupId >junit</groupId >
40
- <artifactId >junit</artifactId >
41
- </dependency >
42
33
<dependency >
43
34
<groupId >junit</groupId >
44
35
<artifactId >junit</artifactId >
36
+ <scope >test</scope >
45
37
</dependency >
46
38
</dependencies >
47
39
Original file line number Diff line number Diff line change
1
+ import org .slf4j .Logger ;
2
+ import org .slf4j .LoggerFactory ;
3
+
1
4
/**
2
5
* Created by Alexis on 28-Apr-17.
3
6
* With Marker interface idea is to make empty interface and extend it.
@@ -25,13 +28,14 @@ public class App {
25
28
*/
26
29
public static void main (String [] args ) {
27
30
31
+ final Logger logger = LoggerFactory .getLogger (App .class );
28
32
Guard guard = new Guard ();
29
33
Thief thief = new Thief ();
30
34
31
35
if (guard instanceof Permission ) {
32
36
guard .enter ();
33
37
} else {
34
- System . out . println ("You have no permission to enter, please leave this area" );
38
+ logger . info ("You have no permission to enter, please leave this area" );
35
39
}
36
40
37
41
if (thief instanceof Permission ) {
Original file line number Diff line number Diff line change
1
+ import org .slf4j .Logger ;
2
+ import org .slf4j .LoggerFactory ;
3
+
1
4
/**
2
- * Created by Alexis on 29-Apr-17.
5
+ * Class defining Guard
3
6
*/
4
7
public class Guard implements Permission {
5
8
9
+ private static final Logger LOGGER = LoggerFactory .getLogger (Guard .class );
10
+
6
11
protected static void enter () {
7
- System .out .println ("You can enter" );
8
- }
9
12
13
+ LOGGER .info ("You can enter" );
14
+ }
10
15
}
Original file line number Diff line number Diff line change 1
1
/**
2
- * Created by Alexis on 29-Apr-17.
3
2
* Interface without any methods
4
3
* Marker interface is based on that assumption
5
4
*/
Original file line number Diff line number Diff line change
1
+ import org .slf4j .Logger ;
2
+ import org .slf4j .LoggerFactory ;
3
+
1
4
/**
2
- * Created by Alexis on 02-May-17.
5
+ * Class defining Thief
3
6
*/
4
7
public class Thief {
8
+
9
+ private static final Logger LOGGER = LoggerFactory .getLogger (Thief .class );
10
+
5
11
protected static void steal () {
6
- System . out . println ("Steal valuable items" );
12
+ LOGGER . info ("Steal valuable items" );
7
13
}
8
14
9
15
protected static void doNothing () {
10
- System . out . println ("Pretend nothing happened and just leave" );
16
+ LOGGER . info ("Pretend nothing happened and just leave" );
11
17
}
12
18
}
Original file line number Diff line number Diff line change 1
- /**
2
- * Created by Alexis on 01-May-17.
3
- */
4
-
5
1
import org .junit .Test ;
6
2
7
3
/**
Original file line number Diff line number Diff line change 4
4
import static org .junit .Assert .assertThat ;
5
5
6
6
/**
7
- * Created by Alexis on 02-May-17.
7
+ * Guard test
8
8
*/
9
9
public class GuardTest {
10
10
@@ -13,4 +13,4 @@ public void testGuard() {
13
13
Guard guard = new Guard ();
14
14
assertThat (guard , instanceOf (Permission .class ));
15
15
}
16
- }
16
+ }
Original file line number Diff line number Diff line change 3
3
import static org .junit .Assert .assertFalse ;
4
4
5
5
/**
6
- * Created by Alexis on 02-May-17.
6
+ * Thief test
7
7
*/
8
8
public class ThiefTest {
9
9
@ Test
10
- public void testGuard () {
10
+ public void testThief () {
11
11
Thief thief = new Thief ();
12
12
assertFalse (thief instanceof Permission );
13
13
}
14
- }
14
+ }
You can’t perform that action at this time.
0 commit comments