What is this practice called in JavaScript?

When you wrap your JavaScript code in a function like this: (function(){ var field = …; function doSomthing(){… … })(); I noticed that this fixes scoping problems for me on a lot of web pages. What is this practice called? Answer The pattern is called self-invocation, a self-invoking function. It can create a closure, but … Read more

Difference between @Delegate, @Mixin and Traits in Groovy?

Would someone explain when I would want to use Groovy Traits vs. Mixins (@Mixin) vs. Delegates (@Delegate)? Maybe some trade-offs and design concerns would help. They all seem to allow for reusing multiple “classes” of behavior. Thanks. 🙂 This SO thread was helpful too: Difference between @Delegate and @Mixin AST transformations in Groovy Answer I … Read more

Polymorphism vs Strategy pattern

What is the difference between the Strategy pattern and Polymorphism in Java? I’m confused that whatever is achieved via Strategy Pattern is basically possible by polymorphism. Correct me if I’m wrong in this regard. Please, also provide me example to eradicate my confusion. Answer For me, the link from CKing post and the example in … Read more

What are the differences between Presenter, Presentation Model, ViewModel and Controller?

I have a pretty good idea how each of these patterns work and know about some of the minor differences between them, but are they really all that different from each other? It seems to me that the Presenter, Presentation Model, ViewModel and Controller are essentially the same concept. Why couldn’t I classify all of … Read more

what is the difference between a view model and a data transfer object?

I’m basing this question on Fowler PoEAA. Given your familiarity with this text, aren’t the ViewModels used in ASP.NET MVC the same as DTOs? Why or why not? Thank you. Answer They serve a similar purpose (encapsulating data for another layer of the application) but they do it differently and for different reasons. The purpose … Read more

What is the solution for the N+1 issue in JPA and Hibernate?

I understand that the N+1 problem is where one query is executed to fetch N records and N queries to fetch some relational records. But how can it be avoided in Hibernate? Answer The problem The N+1 query issue happens when you forget to fetch an association and then you need to access it. For … Read more

Conditional Builder Method Chaining Fluent Interface

I was wondering what would be the best way to implement a .When condition in a fluent interface using method chaining in a Builder object? For instance how would I implement the .WithSkill() and .When() methods in the following example: var level = 5; var ninja = NinjaBuilder .CreateNinja() .Named(“Ninja Boy”) .AtLevel(level) .WithShurikens(10) .WithSkill(Skill.HideInShadows) .When(level … Read more