File tree Expand file tree Collapse file tree 7 files changed +118
-0
lines changed
core-java-modules/core-java-lang-8/src
main/java/com/baeldung/abstractmethodvarargs
test/java/com/baeldung/abstractmethodvarargs Expand file tree Collapse file tree 7 files changed +118
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .example .items ;
2+
3+ /** A Book item that doesn't need any external data when used. */
4+ public class Book extends Item <EmptyContext > {
5+ private final String title ;
6+
7+ public Book (String title ) {
8+ this .title = title ;
9+ }
10+
11+ @ Override
12+ public void use (EmptyContext ctx ) {
13+ // ctx is always EmptyContext.INSTANCE; no data to read from it.
14+ System .out .println ("You read the book: " + title );
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .example .items ;
2+
3+ /**
4+ * A tiny singleton context object used when no arguments are needed.
5+ */
6+ public final class EmptyContext {
7+ public static final EmptyContext INSTANCE = new EmptyContext ();
8+ private EmptyContext () {}
9+ }
Original file line number Diff line number Diff line change 1+ package com .example .items ;
2+
3+ /**
4+ * Generic abstract base class for items.
5+ *
6+ * @param <C> the type of the context object that contains parameters
7+ * needed to "use" the item.
8+ */
9+ public abstract class Item <C > {
10+ /**
11+ * Use the item with a typed context object.
12+ * Subclasses will specify C and implement this method.
13+ */
14+ public abstract void use (C context );
15+ }
Original file line number Diff line number Diff line change 1+ package com .example .items ;
2+
3+ /** A Key item that needs a KeyContext to operate. */
4+ public class Key extends Item <KeyContext > {
5+ private final String name ;
6+
7+ public Key (String name ) {
8+ this .name = name ;
9+ }
10+
11+ @ Override
12+ public void use (KeyContext ctx ) {
13+ System .out .println ("Using key '" + name + "' on door: " + ctx .getDoorId ());
14+ System .out .println ("Doors remaining in queue: " + ctx .getDoorsQueue ().size ());
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .example .items ;
2+
3+ import java .util .Queue ;
4+
5+ /**
6+ * Context data for Key items.
7+ * Immutable fields with getters give clear intent and type safety.
8+ */
9+ public final class KeyContext {
10+ private final String doorId ;
11+ private final Queue <String > doorsQueue ;
12+
13+ public KeyContext (String doorId , Queue <String > doorsQueue ) {
14+ this .doorId = doorId ;
15+ this .doorsQueue = doorsQueue ;
16+ }
17+
18+ public String getDoorId () {
19+ return doorId ;
20+ }
21+
22+ public Queue <String > getDoorsQueue () {
23+ return doorsQueue ;
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .example .items ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
6+
7+ class BookUnitTest {
8+
9+ @ Test
10+ void givenBook_whenUseWithEmptyContext_thenNoException () {
11+ Book book = new Book ("The Hobbit" );
12+
13+ assertDoesNotThrow (() -> book .use (EmptyContext .INSTANCE ));
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .example .items ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
5+
6+ import java .util .Queue ;
7+ import java .util .LinkedList ;
8+
9+ public class KeyUnitTest {
10+
11+ @ Test
12+ void givenKey_whenUseWithKeyContext_thenNoException () {
13+ Queue <String > doors = new LinkedList <>();
14+ doors .add ("front-door" );
15+ doors .add ("back-door" );
16+
17+ Key key = new Key ("MasterKey" );
18+ KeyContext ctx = new KeyContext ("front-door" , doors );
19+
20+ assertDoesNotThrow (() -> key .use (ctx ));
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments