Originally I picked the correct User implementation, based on domain and realm data from the Java EE server. However that was company code, so I had to rewrite the example with numbers. I hope the underlying pattern is still understandable though. For those unfamiliar with CDI, @Inject Instance allows you to iterate through all implementations of an interface. Answer I
Tag: design-patterns
How to inject the right bean implementation according to a RequestParam in the current call
I have this Spring bean (this RestController for the sake of the example) that, depending on the country (let’s say a param that comes in), I want to inject the right implementation of the TaxpayerNameService. So, I have that TaxpayerNameService interface and two (more in the future) implementations of such interface which needs to be injected in the current call
Composition or Inheritance for classes with almost similar implementations but different input and outputs for methods?
I have the following classes, which have quite similar method implementations. Only the classes’ method inputs and outputs seem to be of different types. When I put it like this, it sounds like a case …
How to structure code to prevent incompatible capture of ? extends _?
I was wondering what is the best way to structure code to ensure compiler knows in very simple cases that passed variable is of correct type. I have created very simple code where class O has its execute method execute that accepts some kind of payload (that extends class P) and performs some kind of operation. The thing is that
Observer – specifying modifications of interest explicitly – JAVA implementation
I asked here how I should handle the situation when I need to inform observers only when specific attribute of object changes. I received very nice reply with reference to GoF where is written: …
Java – violation of Liskov substitution principle?
I have the following code: interface Ops{ void remove(); } interface BeforeRemove{ void doBeforeRemove(); } class A implements Ops{ @Override public void remove() { System….
Best way to serialize composite – (design pattern)
I have following java code that is implementation of Composite Design pattern: //composite designed for type safety (all Leaf-only operations only in leaf) interface Component extends Visitable { …
Refactoring code that contains multiple if conditions
Here is some code I’ve written to save a UrlEntity : public UrlEntity saveUrlEntity(String longUrl, LocalDate dateAdded) { int urlLength = longUrl.length(); if (urlLength >= Constants….
Java built-in Observable push notifications
UPDATE Added the full code to make it easier to understand. I am trying to understand how to implement push vs pull notifications using Java built-in Observer. the Observable class has 2 methods …
Which design pattern for merging 2 methods they differ in one place
I wonder which design pattern should I use in my case: I have 2 endpoints that use 2 service methods, each very similar, they differ only in invoking some different methods from the same service: My …