这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.items;

/** A Book item that doesn't need any external data when used. */
public class Book extends Item<EmptyContext> {
private final String title;

public Book(String title) {
this.title = title;
}

@Override
public void use(EmptyContext ctx) {
// ctx is always EmptyContext.INSTANCE; no data to read from it.
System.out.println("You read the book: " + title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.items;

/**
* A tiny singleton context object used when no arguments are needed.
*/
public final class EmptyContext {
public static final EmptyContext INSTANCE = new EmptyContext();
private EmptyContext() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.items;

/**
* Generic abstract base class for items.
*
* @param <C> the type of the context object that contains parameters
* needed to "use" the item.
*/
public abstract class Item<C> {
/**
* Use the item with a typed context object.
* Subclasses will specify C and implement this method.
*/
public abstract void use(C context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.items;

/** A Key item that needs a KeyContext to operate. */
public class Key extends Item<KeyContext> {
private final String name;

public Key(String name) {
this.name = name;
}

@Override
public void use(KeyContext ctx) {
System.out.println("Using key '" + name + "' on door: " + ctx.getDoorId());
System.out.println("Doors remaining in queue: " + ctx.getDoorsQueue().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.items;

import java.util.Queue;

/**
* Context data for Key items.
* Immutable fields with getters give clear intent and type safety.
*/
public final class KeyContext {
private final String doorId;
private final Queue<String> doorsQueue;

public KeyContext(String doorId, Queue<String> doorsQueue) {
this.doorId = doorId;
this.doorsQueue = doorsQueue;
}

public String getDoorId() {
return doorId;
}

public Queue<String> getDoorsQueue() {
return doorsQueue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.items;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

class BookUnitTest {

@Test
void givenBook_whenUseWithEmptyContext_thenNoException() {
Book book = new Book("The Hobbit");

assertDoesNotThrow(() -> book.use(EmptyContext.INSTANCE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.items;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import java.util.Queue;
import java.util.LinkedList;

public class KeyUnitTest {

@Test
void givenKey_whenUseWithKeyContext_thenNoException() {
Queue<String> doors = new LinkedList<>();
doors.add("front-door");
doors.add("back-door");

Key key = new Key("MasterKey");
KeyContext ctx = new KeyContext("front-door", doors);

assertDoesNotThrow(() -> key.use(ctx));
}
}