I'm creating something like this and got struck about the usage of generics in java.
Idea: Producer produces something of type T and consumers contains command objects, command objects holds different mediators. Mediators holds objects of type Subject and updates values of type T
Note: I'm doing this to understand how generics works in terms of inheritance of the generic types and composition of the type parameter definitions in generic type intefaces and concrete classes, so please don't bother about rationale of the design.
Interfaces definitions:
Composition:
IObserver
ISubject which holds object of type T and IObserver.
IMediator holds objects of type ISubject and type T
ICommand holds objects of type IMediator and type T
IProducerConsumer holds objects of type T and ICommand.
There are some concrete Objects for the corresponding interfaces. I defined interfaces like this:
public interface IObserver<T>
public interface ISubject<T,O extends IObserver<T>>
Cool until now. But Now the problems started.
public interface IMediator<T,S extends ISubject<T,O>, O extends IObserver<T>>
compiler is forcing me to do this. I mean O extends IObserver<T>
as above. So, I infered that I can't define like below
public interface IMediator<T,S extends ISubject<T,O extends IObserver<T>> >
I concluded: that inner type parameter definition must not be expanded as above.
So, finally happy with
public interface IMediator<T,S extends ISubject<T,O>, O extends IObserver<T>>
Now mess started in ICommand
public interface ICommand <T,M extends IMediator<T, ?, ?>>
,
I'm struck now compiler is not accepting many of my possibilities even what inference i made as above. I mean
public interface ICommand <T,M extends IMediator<T, S, o>, S extends ISubject<T, IObserver<T>>,O extends IObserver<T>>
is not working. I don't want to user wild card I want to tell to the compiler something more concrete.
My questions are:
Is my inference correct as in ICommand definition.
How to interpret the above case studies.
What are the best defintions assuming that I want to insert T and must able to get and put.
What is the rules and relations of the type parameter definitions in interface and implemented classes.
Please explain ?
Mediator
. (I guess it's just a typing mistake.)IObserver<T>
in stead of O
to ISubject
which would definitely cause a parameter bound mismatch.Correct Version:
interface ICommand<T, M extends IMediator<T, S, O>, S extends ISubject<T, O>, O extends IObserver<T>>
Interface declarations:
interface IObserver<T>
interface ISubject<T, O extends IObserver<T>>
interface IMediator<T, O extends IObserver<T>, S extends ISubject<T,O>>
interface ICommand<T, O extends IObserver<T>, S extends ISubject<T, O>,
M extends IMediator<T, O, S>>
interface IProducerConsumer<T, O extends IObserver<T>, S extends ISubject<T, O>,
M extends IMediator<T, O, S>, C extends ICommand<T, O, S, M>>
Possibly sufficient way:
interface IObserver<T>
interface ISubject<T>
interface IMediator<T>
interface ICommand<T>
interface IProducerConsumer<T>
Two simple cases:
// General way
private class ProductObserver implements IObserver<Product> { }
private ProductObserver productObserver;
// Aspect oriented way
private class LoggerObserver<T> implements IObserver<T> { }
private LoggerObserver<Product> loggerObserver;
Hope this helps.
Good luck.