3,223 questions
2188
votes
36
answers
674k
views
What are drawbacks or disadvantages of singleton pattern? [closed]
The singleton pattern is a fully paid up member of the GoF's patterns book, but it lately seems rather orphaned by the developer world. I still use quite a lot of singletons, especially for factory ...
3687
votes
40
answers
1.3m
views
What is dependency injection?
There have been several questions already posted with specific questions about dependency injection, such as when to use it and what frameworks are there for it. However,
What is dependency injection ...
332
votes
26
answers
185k
views
What should my Objective-C singleton look like? [closed]
My singleton accessor method is usually some variant of:
static MyClass *gInstance = NULL;
+ (MyClass *)instance
{
@synchronized(self)
{
if (gInstance == NULL)
gInstance =...
672
votes
7
answers
570k
views
Examples of GoF Design Patterns in Java's core libraries
I am learning GoF Java Design Patterns and I want to see some real life examples of them. What are some good examples of these Design Patterns in Java's core libraries?
143
votes
11
answers
44k
views
Is there a use-case for singletons with database access in PHP?
I access my MySQL database via PDO. I'm setting up access to the database, and my first attempt was to use the following:
The first thing I thought of is global:
$db = new PDO('mysql:host=127.0.0.1;...
618
votes
13
answers
308k
views
When would you use the Builder Pattern? [closed]
What are some common, real world examples of using the Builder Pattern? What does it buy you? Why not just use a Factory Pattern?
645
votes
22
answers
349k
views
What are the differences between Abstract Factory and Factory design patterns?
I know there are many posts out there about the differences between these two patterns, but there are a few things that I cannot find.
From what I have been reading, I see that the factory method ...
199
votes
6
answers
77k
views
Is Meyers' implementation of the Singleton pattern thread safe?
Is the following implementation, using lazy initialization, of Singleton (Meyers' Singleton) thread safe?
static Singleton& instance()
{
static Singleton s;
return s;
}
If not, why and ...
511
votes
11
answers
237k
views
ViewPager and fragments — what's the right way to store fragment's state?
Fragments seem to be very nice for separation of UI logic into some modules. But along with ViewPager its lifecycle is still misty to me. So Guru thoughts are badly needed!
Edit
See dumb solution ...
379
votes
10
answers
144k
views
Singletons vs. Application Context in Android?
Recalling this post enumerating several problems of using singletons
and having seen several examples of Android applications using singleton pattern, I wonder if it's a good idea to use Singletons ...
119
votes
4
answers
132k
views
Why is Singleton considered an anti-pattern? [duplicate]
I've heard recently that Singleton is an anti-pattern. I know it has to do with the fact making a class singleton is like making that unique instance a global variable, but it's also doing a lot more ...
103
votes
8
answers
35k
views
Why is volatile used in double checked locking
From Head First design patterns book, the singleton pattern with double checked locking has been implemented as below:
public class Singleton {
private volatile static Singleton instance;
...
199
votes
8
answers
96k
views
What does "program to interfaces, not implementations" mean?
One stumbles upon this phrase when reading about design patterns.
But I don't understand it, could someone explain this for me?
215
votes
22
answers
204k
views
Creating the Singleton design pattern in PHP5
How would one create a Singleton class using PHP5 classes?
102
votes
4
answers
44k
views
What components are MVC in JSF MVC framework?
In JSF MVC framework who is Model, View, and Controller?
517
votes
10
answers
68k
views
Why create "Implicitly Unwrapped Optionals", since that implies you know there's a value?
Why would you create a "Implicitly Unwrapped Optional" vs creating just a regular variable or constant?
If you know that it can be successfully unwrapped then why create an optional in the first place?...
234
votes
6
answers
165k
views
Implementing Singleton with an Enum (in Java)
I have read that it is possible to implement Singleton in Java using an Enum such as:
public enum MySingleton {
INSTANCE;
}
But, how does the above work? Specifically, an Object has to be ...
179
votes
10
answers
58k
views
Is ServiceLocator an anti-pattern? [closed]
Recently I've read Mark Seemann's article about Service Locator anti-pattern.
Author points out two main reasons why ServiceLocator is an anti-pattern:
API usage issue (which I'm perfectly fine with)
...
403
votes
11
answers
250k
views
How to implement the factory method pattern in C++ correctly
There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple:
How do I implement Factory ...
112
votes
19
answers
111k
views
Real World Example of the Strategy Pattern [closed]
I've been reading about the OCP principle and how to use the strategy pattern to accomplish this.
I was going to try and explain this to a couple of people, but the only example I can think of is ...
113
votes
15
answers
118k
views
Abstract class vs Interface in Java
I was asked a question, I wanted to get my answer reviewed here.
Q: In which scenario it is more appropriate to extend an abstract class rather than implementing the interface(s)?
A: If we are using ...
253
votes
25
answers
134k
views
Handling Dialogs in WPF with MVVM
In the MVVM pattern for WPF, handling dialogs is one of the more complex operations. As your view model does not know anything about the view, dialog communication can be interesting. I can expose ...
90
votes
8
answers
115k
views
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?
517
votes
21
answers
258k
views
MVC pattern on Android
Is it possible to implement the model–view–controller pattern in Java for Android?
Or is it already implemented through Activities? Or is there a better way to implement the MVC pattern for Android?
575
votes
31
answers
234k
views
Dependency Injection vs Factory Pattern [closed]
Most of the examples quoted for usage of Dependency Injection, we can solve using the factory pattern as well. Looks like when it comes to usage/design the difference between dependency injection and ...