mercredi 1 avril 2015

Using private static nested class to implement Singleton

I am currently learning about the Singleton pattern. I learnt that the classic way to implement it is to create a static field of the Singleton class type, hide the constructor using private access modifier, and provide a public getInstance() method.


However, I thought of another way of implementing it without using private constructors:



public class SWrapper {
private static Singleton holder = new Singleton();
private static class Singleton{ /* implementation without private constructor*/}
public static Singleton getInstance() {
return holder;
}


QUESTION: Does this implementation work? (I think it does but I can't be sure.) If it does, are there any advantages or disadvantages to using this implementation?


How to do logic based on object ID

Suppose I have a game, where there are buildings sorted by type. Each type is represented as a separate class, but sometimes I have to do some uncommon logic for the buildings inside the same type. How could one implement this kind of behaviour? For example, I can identify buildings and have giant switch for concrete building ID. But I think that something is not right with this approach.


What is the best approach to use multiple services inside a resource controller?

I have an controller that call three services :



public class ProductController(){

private AccountService accountService;

private ProcessService processService;

private releaseService releaseService;

@RequestMapping("/process")
public Product process(@RequestParam(value="name", defaultValue="docs") ProductProcessed process) {

accountService.notify();
releaseService.sendRelease(process);


return processService.process(process);
}


}


What is the best approach to encapsulate this service calls??


JAVA: Choosing algorithm based on multiple dimensions

I have an instance of class Address, which I have to change according to environment:

1) Region: base class with sub-classes RegionA and RegionB

2) Site: base class with sub-classes SiteA, SiteB and SiteC

3) Language: base class with sub-classes LanguageA and LanguageB

Each subclass defines constraints about Address modification.

The problem is that each tuple (Region, Site, Language) has to define its own modifier.


So, I have a method adjust(Address a, Region r, Site s, Language l).

What is the best design patter to use in this case?


Polimorphic Jackson and Spring Container

I've got some method, which has @RequestBody parameter as abstract class(jackson create concrete class dependes on one of json parameters).


This class looks some like that:



@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = JsonParams.PolimorphicParam)
@JsonSubTypes({ @JsonSubTypes.Type(A.class), @JsonSubTypes.Type(B.class) })
public abstract class X{
protected String someProperty;
}


In Jackson it's really cool future, because i've some kind of class hierarchy with inherited properties, but i want more. I want every child class to overrite method, which produce some object. To produce this object i need inject Service bean into this class, but jackson create it with new operator out of spring container.


Is it any way to @Autowired this service? I know there is ApplicationContextProvider which can help me to find bean in context:



EngineService engine= (EngineService) ApplicationContextProvider.getApplicationContext().getBean(
"engine");


But it looks awful and I want to autowired it.


And another question, is it good to give some behavior jackson deserialized objects?


Design pattern for interface that determines order of method invocation

I want to create a Java interface with a number methods. But I want the user of the interface to only be able to invoke methods in the sequence or order that I define. For instance buyTicket() should not be called before reserveTicket(). Q: Is there a design pattern or any tips on how to go about this?


I considered:




  • A)



    • Interface is wrapped, only showing the next possible method. Each invocation of a method returns a new operation that can be called after it, and so on.

    • So ReserveTicketOperation has public BuyTicketOperation execute();

    • Then BuyTicketOperation has public RenderTicketOperation execute();




  • B)



    • Use come kind of context state machine that records the position of execution using enums and has a factory for obtaining the next operation.




Any thoughts or suggestions are greatly appreciated. Thanks


Is MVP a design pattern or an architectural pattern ? What about MvC and MvvM?

After doing some research on android patterns I kept finding different answers. Some people will start talking about design pattern, architectural or even architectural presentation patterns.


I'm reaching the conclusion that they are all architectural presentation pattern but MvC is an architectural pattern while MvP & MvvM are design pattern on the same rank as something like the design pattern observer. Is it correct?