File tree Expand file tree Collapse file tree 9 files changed +33
-31
lines changed
Expand file tree Collapse file tree 9 files changed +33
-31
lines changed Original file line number Diff line number Diff line change @@ -10,20 +10,20 @@ tags:
1010---
1111
1212## Intent
13- Using empy interfaces as markers to distinguish special treated objects.
13+ Using empty interfaces as markers to distinguish special treated objects.
1414
1515![ alt text] ( ./etc/MarkerDiagram.png " Marker Interface ")
1616
1717## Applicability
1818Use the Marker Interface pattern when
1919
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
2222
2323## Real world examples
2424
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 )
2727
2828## Credits
2929
Original file line number Diff line number Diff line change 3030
3131 <artifactId >marker</artifactId >
3232 <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 >
4233 <dependency >
4334 <groupId >junit</groupId >
4435 <artifactId >junit</artifactId >
36+ <scope >test</scope >
4537 </dependency >
4638 </dependencies >
4739
Original file line number Diff line number Diff line change 1+ import org .slf4j .Logger ;
2+ import org .slf4j .LoggerFactory ;
3+
14/**
25 * Created by Alexis on 28-Apr-17.
36 * With Marker interface idea is to make empty interface and extend it.
@@ -25,13 +28,14 @@ public class App {
2528 */
2629 public static void main (String [] args ) {
2730
31+ final Logger logger = LoggerFactory .getLogger (App .class );
2832 Guard guard = new Guard ();
2933 Thief thief = new Thief ();
3034
3135 if (guard instanceof Permission ) {
3236 guard .enter ();
3337 } 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" );
3539 }
3640
3741 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+
14/**
2- * Created by Alexis on 29-Apr-17.
5+ * Class defining Guard
36 */
47public class Guard implements Permission {
58
9+ private static final Logger LOGGER = LoggerFactory .getLogger (Guard .class );
10+
611 protected static void enter () {
7- System .out .println ("You can enter" );
8- }
912
13+ LOGGER .info ("You can enter" );
14+ }
1015}
Original file line number Diff line number Diff line change 11/**
2- * Created by Alexis on 29-Apr-17.
32 * Interface without any methods
43 * Marker interface is based on that assumption
54 */
Original file line number Diff line number Diff line change 1+ import org .slf4j .Logger ;
2+ import org .slf4j .LoggerFactory ;
3+
14/**
2- * Created by Alexis on 02-May-17.
5+ * Class defining Thief
36 */
47public class Thief {
8+
9+ private static final Logger LOGGER = LoggerFactory .getLogger (Thief .class );
10+
511 protected static void steal () {
6- System . out . println ("Steal valuable items" );
12+ LOGGER . info ("Steal valuable items" );
713 }
814
915 protected static void doNothing () {
10- System . out . println ("Pretend nothing happened and just leave" );
16+ LOGGER . info ("Pretend nothing happened and just leave" );
1117 }
1218}
Original file line number Diff line number Diff line change 1- /**
2- * Created by Alexis on 01-May-17.
3- */
4-
51import org .junit .Test ;
62
73/**
Original file line number Diff line number Diff line change 44import static org .junit .Assert .assertThat ;
55
66/**
7- * Created by Alexis on 02-May-17.
7+ * Guard test
88 */
99public class GuardTest {
1010
@@ -13,4 +13,4 @@ public void testGuard() {
1313 Guard guard = new Guard ();
1414 assertThat (guard , instanceOf (Permission .class ));
1515 }
16- }
16+ }
Original file line number Diff line number Diff line change 33import static org .junit .Assert .assertFalse ;
44
55/**
6- * Created by Alexis on 02-May-17.
6+ * Thief test
77 */
88public class ThiefTest {
99 @ Test
10- public void testGuard () {
10+ public void testThief () {
1111 Thief thief = new Thief ();
1212 assertFalse (thief instanceof Permission );
1313 }
14- }
14+ }
You can’t perform that action at this time.
0 commit comments