Java generics software engineering design - java

I have a class RabbitQueue which basically acts like a queue and implements my Pollable interface.
I also have a class SaveToDatabaseStrategy which implements my DataProcessingStrategy interface. This is designed following the strategy-pattern.
Now, my class InputHandler which implements my interface InputListener, contains an instance of the Pollable interface and one of the DataProcessingStrategy interface.
However, I don't want to set the Generic type (String) when I declare these two fields, since the Generic type depends on the implementation of this interface which is given later on.
How would you design this?
public interface Pollable<T> {
T poll();
}
public class RabbitQueue implements Pollable<String> {
// code..
}
public interface DataProcessingStrategy<T> {
void processData(T t);
}
public class SaveToDatabaseStrategy<T> implements DataProcessingStrategy<T> {
private Repository<T, ?> repo;
public SaveToDatabaseStrategy(Repository<T, ?> repo) {
this.repo = repo;
}
#Override
public void processData(T data) {
repo.create(data);
System.out.printf("Received data of type %s: %s\n", data.getClass().getSimpleName(), data);
}
}
public interface InputListener<T> {
void inputReceived();
void inputReceived(T t);
}
public class InputHandler implements InputListener<String> {
private Pollable<String> queue;
private DataProcessingStrategy<String> strategy;
public InputHandler(String host, String queueName) throws IOException, TimeoutException {
queue = new RabbitQueue(host, queueName, this);
}
public void setStrategy(DataProcessingStrategy strategy) {
this.strategy = strategy;
}
#Override
public void inputReceived() {
System.out.println("Input received!");
strategy.processData(queue.poll());
}
#Override
public void inputReceived(String s) {
System.out.println("Input received: " + s + "!");
System.out.println("> " + queue.poll());
}
}

You could add a type parameter to the InputHandler class.
public class InputHandler<T> implements InputListener<T> {
private Pollable<T> queue;
private DataProcessingStrategy<T> strategy;
public InputHandler(String host, String queueName) throws IOException, TimeoutException {
queue = new RabbitQueue(host, queueName, this);
}
public void setStrategy(DataProcessingStrategy strategy) {
this.strategy = strategy;
}
#Override
public void inputReceived() {
System.out.println("Input received!");
strategy.processData(queue.poll());
}
#Override
public void inputReceived(String s) {
System.out.println("Input received: " + s + "!");
System.out.println("> " + queue.poll().toString());
}
}
Then create a new object like
new InputHandler<String>(host, queueName)

Related

Java class as condition in generic method

I have a Service which is responsible to gather some information through Providers. Depending on the information required, a certain Provider has to be used. For now the Provider is chosen upon a given Class. This example is simplified, but it should give a reasonable understanding of my setup
abstract class AbstractProvider {
private String provider = "MyProvider";
public String doSomethingAwsome() {
return provider;
}
}
class ProviderA extends AbstractProvider {
#Override
public String doSomethingAwsome() {
return super.doSomethingAwsome() + "_A";
}
}
class ProviderB extends AbstractProvider {
#Override
public String doSomethingAwsome() {
return super.doSomethingAwsome() + "_B";
}
}
class MyService {
private ProviderA providerA;
private ProviderB providerB;
MyService() {
providerA = new ProviderA();
providerB = new ProviderB();
}
public <T extends AbstractProvider> String doSomethingWithProvider(Class<T> providerClass) {
if (providerClass.equals(ProviderA.class)) {
return providerA.doSomethingAwsome();
} else if (providerClass.equals(ProviderB.class)) {
return providerB.doSomethingAwsome();
}
throw new IllegalArgumentException("No Provider found for Class: " + providerClass);
}
}
class Main {
public static void main(String[] args) {
MyService myService = new MyService();
String fromProviderA = myService.doSomethingWithProvider(ProviderA.class);
String fromProviderB = myService.doSomethingWithProvider(ProviderB.class);
System.out.printf("Provider A: %s, Provider B: %s", fromProviderA, fromProviderB);
}
}
Now I'm not happy with the solution in MyService.doSomethingWithProvider(). How would you implement this?
Is there a generic way of choosing the Provider? I dont like to use if-else clauses here.
Edit
Based on some suggestions, using a map seems better
class MyService {
private Map<Class<? extends AbstractProvider>, AbstractProvider> providers = new HashMap<>();
MyService() {
providers.put(ProviderA.class, new ProviderA());
providers.put(ProviderB.class, new ProviderB());
}
public <T extends AbstractProvider> String doSomethingWithProvider(Class<T> providerClass) {
if (providers.containsKey(providerClass)) {
return providers.get(providerClass).doSomethingAwsome();
}
new IllegalArgumentException("No Provider found for Class: " + providerClass)
}
}
Turing85 suggested to have a look at the Chain of Responsibility design pattern. Honestly I had to dig a little deeper for this one. The idea is quite easy to understand but getting my head around the implementation took me a little further.
So thanks for your input, I'm happy with this
abstract class AbstractProvider {
private String provider = "MyProvider";
public AbstractProvider nextProcessor;
public AbstractProvider(AbstractProvider nextProcessor) {
this.nextProcessor = nextProcessor;
}
public String doSomethingAwsome() {
return provider;
}
public abstract <T extends AbstractProvider> String doSomethingAwsome(Class<T> provider) throws IllegalArgumentException;
}
class ProviderA extends AbstractProvider {
public ProviderA (AbstractProvider nextProvider) {
super(nextProvider);
}
#Override
public String doSomethingAwsome() {
return super.doSomethingAwsome() + "_A";
}
#Override
public <T extends AbstractProvider> String doSomethingAwsome(Class<T> provider) throws IllegalArgumentException {
if (provider.equals(this.getClass())) {
return this.doSomethingAwsome();
} else if (nextProcessor != null) {
return nextProcessor.doSomethingAwsome(provider);
}
throw new IllegalArgumentException("No provider found for class: " + provider);
}
}
class ProviderB extends AbstractProvider {
public ProviderB (AbstractProvider nextProvider) {
super(nextProvider);
}
#Override
public String doSomethingAwsome() {
return super.doSomethingAwsome() + "_B";
}
#Override
public <T extends AbstractProvider> String doSomethingAwsome(Class<T> provider) throws IllegalArgumentException {
if (provider.equals(this.getClass())) {
return this.doSomethingAwsome();
} else if (nextProcessor != null) {
return nextProcessor.doSomethingAwsome(provider);
}
throw new IllegalArgumentException("No provider found for class: " + provider);
}
}
class MyService {
private AbstractProvider providerChain;
MyService() {
providerChain = new ProviderA(new ProviderB(null));
}
public <T extends AbstractProvider> String doSomethingWithProvider(Class<T> providerClass) {
return providerChain.doSomethingAwsome(providerClass);
}
}
I'm pretty sure you can do several more improvements here. Don't hesitate to give me your input

Custom JavaFX events

I have a custom event listener:
public interface IMyCustomListener {
public void onEvent1(int param0);
public void onEvent2(String param0);
}
now I would fire events with javafx events pattern (Event, EventTarget, EventHandler...), how can I do this?
You cannot. JavaFX uses javafx.event.Events or subtypes, so String or even primitive int can't be passed.
You could however create a custom subtype of Event and add the parameters to this class.
Similarly only classes implementing javafx.event.EventHandler can be registered as event handlers.
You could create a event handler class that delegates to your methods though:
public abstract class CustomEvent extends Event {
public static final EventType<CustomEvent> CUSTOM_EVENT_TYPE = new EventType(ANY);
public CustomEvent(EventType<? extends Event> eventType) {
super(eventType);
}
public abstract void invokeHandler(MyCustomEventHandler handler);
}
public class CustomEvent1 extends CustomEvent {
public static final EventType<CustomEvent> CUSTOM_EVENT_TYPE_1 = new EventType(CUSTOM_EVENT_TYPE, "CustomEvent1");
private final int param;
public CustomEvent1(int param) {
super(CUSTOM_EVENT_TYPE_1);
this.param = param;
}
#Override
public void invokeHandler(MyCustomEventHandler handler) {
handler.onEvent1(param);
}
}
public class CustomEvent2 extends CustomEvent {
public static final EventType<CustomEvent> CUSTOM_EVENT_TYPE_2 = new EventType(CUSTOM_EVENT_TYPE, "CustomEvent2");
private final String param;
public CustomEvent2(String param) {
super(CUSTOM_EVENT_TYPE_2);
this.param = param;
}
#Override
public void invokeHandler(MyCustomEventHandler handler) {
handler.onEvent2(param);
}
}
public abstract class MyCustomEventHandler implements EventHandler<CustomEvent> {
public abstract void onEvent1(int param0);
public abstract void onEvent2(String param0);
#Override
public void handle(CustomEvent event) {
event.invokeHandler(this);
}
}
Usage Example
Button btn = new Button("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
btn.fireEvent(new CustomEvent1(42));
btn.fireEvent(new CustomEvent2("Hello World"));
});
btn.addEventHandler(CustomEvent.CUSTOM_EVENT_TYPE, new MyCustomEventHandler() {
#Override
public void onEvent1(int param0) {
System.out.println("integer parameter: " + param0);
}
#Override
public void onEvent2(String param0) {
System.out.println("string parameter: "+param0);
}
});

Spring: how to get bean from application context which implements generic interface?

I have interface:
public interface CommandHandler<T extends Command> {
void handle(T command);
}
There are commands which implement Command marker interface
public class CreateCategoryCommand implements Command {
}
public class CreateCategoryCommand implements Command {
}
For each command I have apropriate CommandHandler implementations:
#Component
public class CreateProductCommandHandler implements CommandHandler<CreateProductCommand> {
#Override
public void handle(CreateProductCommand command) {
System.out.println("Command handled");
}
}
#Component
public class CreateCategoryCommandHandler implements CommandHandler<CreateCategoryCommand> {
#Override
public void handle(CreateCategoryCommand command) {
}
}
Question:
I have command bus
#Component
public class SimpleCommandBus implements CommandBus {
#Autowired
private ApplicationContext context;
#Override
public void send(Command command) {
// OF COURSE, THIS NOT COMPILED, BUT I NEED SOMETHING LIKE THIS
CommandHandler commandHandler = context.getBean(CommandHandler<command.getClass()>)
}
}
How to get bean from application context which implements generic interface with particular type?
Way I solved it:
#Component
public class SimpleCommandBus {
private final Logger logger;
private final Set<CommandHandler<?>> handlers;
private final Map<Class<?>, CommandHandler<?>> commandHandlersCache = new WeakHashMap<>();
public SimpleCommandBus(Logger logger, Set<CommandHandler<?>> handlers) {
this.logger = logger;
this.handlers = handlers;
}
public void send(Command command) {
CommandHandler<Command> commandHandler = getCommandHandler(command);
if (commandHandler != null)
commandHandler.handle(command);
else
logger.error("Can't handle command " + command);
}
#SuppressWarnings("unchecked")
private <C extends Command> CommandHandler<C> getCommandHandler(C command) {
Class<?> commandType = command.getClass();
if (commandHandlersCache.containsKey(commandType))
return (CommandHandler<C>) commandHandlersCache.get(commandType);
for (CommandHandler<?> haandler : handlers) {
Class<?> supportedCommandType = resolveTypeArgument(haandler.getClass(), CommandHandler.class);
if (commandType.isAssignableFrom(supportedCommandType)) {
commandHandlersCache.put(commandType, haandler);
return (CommandHandler<C>) haandler;
}
}
commandHandlersCache.put(commandType, null);
return null;
}
}

How is it possible to communicate between two classes in Java using an interface?

Hi ive been reading on some similar topics here but none of them answer my question. Some say you cant even do this which is not a good thing since I cant finnish my course in that case.
Heres som simple code. Think of each block as a separate class.
public interface Interface {
void printMessage(String meddelande);
}
public class Model implements Interface {
String message = "hej!";
public static void main(String[] args) {
Model model1 = new Model();
View view1 = new View();
model1.printMessage(model1.message); //Ska jag anropa funktionen såhär ens?
}
public void printMessage(String str) {
}
}
public class View implements Interface {
printMessage(String str) {
}
}
So, how is it now possible to tel the view to print this string from the model class without the classes knowing about each other? Its not allowed to send a reference of the model-objekt to the view-object. ; (
Define an Interface:
public interface MyInterface {
void printMessage(String str);
}
Define a class that can trigger the notification:
public class ClassNotifier {
MyInterface mInterface;
public ClassNotifier(MyInterface mInterface) {
this.mInterface = mInterface;
}
public void triggerTheMsg(String msg) {
if (mInterface != null) {
mInterface.printMessage(msg);
}
}
}
Define a class that will be informed:
public class InformedClass implements MyInterface {
public static void main(String[] args) throws Exception {
InformedClass c = new InformedClass();
ClassNotifier cn = new ClassNotifier(c);
}
#Override
public void printMessage(String newMsg) {
System.out.println("A new msg is here: " + newMsg);
}
}
How does it works?:
this is named a callback parttern, the class ClassNotifier has a reference to the interface MyInterface, which is impl. by Informed class to, so every time the ClassNotifier calls the method printMessage, the method printMessage in the class Informed will be triggered too.
I advice you to use dependency injection, for example:
public class Model {
String message = "hej!";
Interface printer;
public void Model(Interface printer) {
printer = printer;
}
public static void main(String[] args) {
Model model1 = new Model(new View());
model1.printMessage(model1.message);
}
public void printMessage(String str) {
printer.printMessage(str);
}
}

How to design a type safe message API in Java?

I have a Java client which wants to communicate with a device through messages over serial communication. The client should be able to use a clean API, abstracting the ugly details of the serial communication. The client can send many types of messages through that API and gets responses. I'm searching for advice which way is best to implement this API.
For simplicity, say we have only two message types: HelloMessage which triggers a HelloResponse and InitMessage which triggers an InitResponse (in reality, there are many more)
Designing the API (that is, the Java abstraction of the device) I could have:
One method per message type:
public class DeviceAPI {
public HelloResponse sendHello(HelloMessage){...}
public InitResponse sendInit(InitMessage){...}
... and many more message types ....
This is nicely type safe. (It could also be many times the same send() method, overloaded, but that's about the same). But it is very explicit, and not very flexible - we cannot add messages without modification of the API.
I could also have a single send method, which takes all message types:
class HelloMessage implements Message
class HelloResponse implements Response
...
public class DeviceAPI {
public Response send(Message msg){
if(msg instanceof HelloMessage){
// do the sending, get the response
return theHelloResponse
} else if(msg instanceof ...
This simplifies the API (only one method) and allows for additional Message types to be added later without changing the API. At the same time, it requires the Client to check the Response type and cast it to the right type.
Client code:
DeviceAPI api = new DeviceAPI();
HelloMessage msg = new HelloMessage();
Response rsp = api.send(msg);
if(rsp instanceOf HelloResponse){
HelloResponse hrsp = (HelloResponse)rsp;
... do stuff ...
This is ugly in my opinion.
What do you recommend? Are there other approaches which give cleaner results?
References welcome! How did others solve this?
Here is a way to do it in type-safe (and extensible) way using generics:
public interface MessageType {
public static final class HELLO implements MessageType {};
}
public interface Message<T extends MessageType> {
Class<T> getTypeClass();
}
public interface Response<T extends MessageType> {
}
public class HelloMessage implements Message<MessageType.HELLO> {
private final String name;
public HelloMessage(final String name) {
this.name = name;
}
#Override
public Class<MessageType.HELLO> getTypeClass() {
return MessageType.HELLO.class;
}
public String getName() {
return name;
}
}
public class HelloResponse implements Response<MessageType.HELLO> {
private final String name;
public HelloResponse(final String name) {
this.name = name;
}
public String getGreeting() {
return "hello " + name;
}
}
public interface MessageHandler<T extends MessageType, M extends Message<T>, R extends Response<T>> {
R handle(M message);
}
public class HelloMessageHandler
implements MessageHandler<MessageType.HELLO, HelloMessage, HelloResponse> {
#Override
public HelloResponse handle(final HelloMessage message) {
return new HelloResponse(message.getName());
}
}
import java.util.HashMap;
import java.util.Map;
public class Device {
#SuppressWarnings("rawtypes")
private final Map<Class<? extends MessageType>, MessageHandler> handlers =
new HashMap<Class<? extends MessageType>, MessageHandler>();
public <T extends MessageType, M extends Message<T>, R extends Response<T>>
void registerHandler(
final Class<T> messageTypeCls, final MessageHandler<T, M, R> handler) {
handlers.put(messageTypeCls, handler);
}
#SuppressWarnings("unchecked")
private <T extends MessageType, M extends Message<T>, R extends Response<T>>
MessageHandler<T, M, R> getHandler(final Class<T> messageTypeCls) {
return handlers.get(messageTypeCls);
}
public <T extends MessageType, M extends Message<T>, R extends Response<T>>
R send(final M message) {
MessageHandler<T, M, R> handler = getHandler(message.getTypeClass());
R resposnse = handler.handle(message);
return resposnse;
}
}
public class Main {
public static void main(final String[] args) {
Device device = new Device();
HelloMessageHandler helloMessageHandler = new HelloMessageHandler();
device.registerHandler(MessageType.HELLO.class, helloMessageHandler);
HelloMessage helloMessage = new HelloMessage("abhinav");
HelloResponse helloResponse = device.send(helloMessage);
System.out.println(helloResponse.getGreeting());
}
}
To add support for a new message type, implement MessageType interface to create a new message type, implement Message, Response and MessageHandler interfaces for the new MessageType class and register the handler for the new message type by calling Device.registerHandler.
I've got a fully working example now of what you want:
To define the types of messages:
public interface MessageType {
public static class INIT implements MessageType { }
public static class HELLO implements MessageType { }
}
Base Message and Response classes:
public class Message<T extends MessageType> {
}
public class Response<T extends MessageType> {
}
Create custom init messages and responses:
public class InitMessage extends Message<MessageType.INIT> {
public InitMessage() {
super();
}
public String getInit() {
return "init";
}
}
public class InitResponse extends Response<MessageType.INIT> {
public InitResponse() {
super();
}
public String getInit() {
return "init";
}
}
Create custom hello messages and responses:
public class HelloMessage extends Message<MessageType.HELLO> {
public HelloMessage() {
super();
}
public String getHello() {
return "hello";
}
}
public class HelloResponse extends Response<MessageType.HELLO> {
public HelloResponse() {
super();
}
public String getHello() {
return "hello";
}
}
The DeviceAPI:
public class DeviceAPI {
public <T extends MessageType, R extends Response<T>, M extends Message<T>> R send(M message) {
if (message instanceof InitMessage) {
InitMessage initMessage = (InitMessage)message;
System.out.println("api: " + initMessage.getInit());
return (R)(new InitResponse());
}
else if (message instanceof HelloMessage) {
HelloMessage helloMessage = (HelloMessage)message;
System.out.println("api: " + helloMessage.getHello());
return (R)(new HelloResponse());
}
else {
throw new IllegalArgumentException();
}
}
}
Note that it does require an instanceof-tree, but you need that to handle what kind of message it is.
And a working example:
public static void main(String[] args) {
DeviceAPI api = new DeviceAPI();
InitMessage initMsg = new InitMessage();
InitResponse initResponse = api.send(initMsg);
System.out.println("client: " + initResponse.getInit());
HelloMessage helloMsg = new HelloMessage();
HelloResponse helloResponse = api.send(helloMsg);
System.out.println("client: " + helloResponse.getHello());
}
Output:
api: init
client: init
api: hello
client: hello
UPDATE: Added example on how to get input from the messages the client wants to send.
You could have a system of message handlers, and your DeviceAPI could choose which handler is suitable for the incoming message; and delegate it to the appropriate message handler:
class DeviceAPI {
private List<Handler> msgHandlers = new ArrayList<Handler>();
public DeviceAPI(){
msgHandlers.add(new HelloHandler());
//Your other message handlers can be added
}
public Response send(Message msg) throws Exception{
for (Handler handler : msgHandlers) {
if (handler.canHandle(msg)){
return handler.handle(msg);
}
}
throw new Exception("No message handler defined for " + msg);
}
}
The HelloHandler would look like:
interface Handler<T extends Message, U extends Response> {
boolean canHandle(Message message);
U handle(T message);
}
class HelloHandler implements Handler<HelloMessage, HelloResponse> {
#Override
public boolean canHandle(Message message) {
return message instanceof HelloMessage;
}
#Override
public HelloResponse handle(HelloMessage message) {
//Process your message
return null;
}
}
Ditto for your other messages. I'm sure you could make it more elegant, but the idea still remains the same - donot have one monster method with ifs; instead use polymorphism.
I don't think this is ugly at all:
if(rsp instanceOf HelloResponse){
HelloResponse hrsp = (HelloResponse)rsp;
...
else if ...
as long as you don't have like 100 different responses. You can encapsulate many kind of responses in one, depending on the data they have. For example:
class GenericResponse{
private String message;
private int responseType;
...
}
I have developed some multiplayer games and this is a good way to do it.
In case you have too many different types of messages, you just can use generic java types, like the example of skiwi above.
hope it helps

Categories