Skip to content

Commit 7467016

Browse files
BAEL-9391: Abstract Method With Variable List of Arguments (#18805)
1 parent 8ba4e05 commit 7467016

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)