This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
On Design Patterns: When to use the Singleton?
Hi
Just wondering what are good candidates for singleton?
Just reading about it and wondering if sometimes I have misused it.
Generally speaking when would you use a singleton?
thanks a lot
Basically, whenever I need a class but am not sure I will need a singleton pattern, I code to the singleton interface, but let the implementing class determine whether or not to return a single instance or a new instance. That way, calling classes don't have to worry about changing if the move to singleton (or the move away from singleton) should occur.
Places I've used this successfully is on my repositories. But I always preface every singleton answer with a 'Make sure you pay attention to thread-safety, whichever way you go'. Sometimes weird things can happen in a multi-threaded environment when dealing with a singleton.
Related
This question already has answers here:
Extension methods versus inheritance
(9 answers)
Closed 6 years ago.
I am facing this question regularly in interview. But I am not getting it's answer anywhere.Please help me.
Inheritance and extension methods are entirely orthogonal. It's just a simple way of writing a function in C# using the familiar . syntax. They only look similar if you use inheritance for code reuse, which tends to be frowned upon (but make sure you understand why - "best" practices are context-sensitive :)).
In any case, I'd expect the question is there to get you talking. Don't focus too hard on what the "right" answer is - just outline what inheritance is used for according to you, and what extension methods are used for, and what benefits and drawbacks each of those has. Get the dialog running.
For me, for example, extension methods are all about making common functions against an interface. That is, the functions add functionality on top of an interface (or class) while only using its public interface. This makes the "size" of the interface available to the function much smaller, which in turn makes it much easier to reason about.
Different people use extension methods differently, just like different people use inheritance differently. However, I find that as you shift from inheritance to composition, extension methods become more and more useful (and really, natural). Inheritance is a very specific technique that started being used for pretty much everything with little reason, but that's a big topic on Programmers.SE already anyway, just like the composition vs. inheritance debate :))
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why seal a class?
I haven't seen a lot of classes with this modifier, however I'm wondering in which kind of scenarios this would be useful.
In case you do not wand anyone to extend a class.
Classic example is a String class, it's self-sufficient and implements flyweight pattern, that might be easily destroyed if anyone could overwrite it's methods/properties.
You want to use the sealed class if you provide your API that doesn't need to be changed, for any of above reasons.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How will I know when to create an interface?
Hi guys,
This will sound a bit thick, I guess, but I am battling to understand the reason to use interfaces. People keep saying that they are 'contracts' for classes. But, why use them? If I was a single developer, on an application, that I knew no one would ever work on (I know - not a common example, but I am just trying to understand), would I use Interfaces? They seem to just duplicate work. It seem I define what a class must implement, and then go an implement it. I'm doing it twice - why?
Please note: I am not in anyway saying they're useless... I'm just tying to find out why, in projects I work on, they define an IClass, and then based on that, define the class which they use...
Sorry if it's very basic... Just hoping someone can help me out.
I use interfaces because it makes my code a lot more modular. Using interfaces in combination with an inversion of control container (http://code.google.com/p/autofac/) will allow you to swap in various implementations of an interface easily.
Also, interfaces are easier to unit test.
Those are just a couple good reasons; really, there are more. But those are strong enough to make me want to use interfaces.
if you want to have several classes that all support the same set of methods. For example you might have a class that stores data and the code that calls them does not care about the details of which class its working with. They must implement methods store and fetch. In this case you can either have an interface with those 2 methods or you can have a common base class.
Why not have a comon base class.
you can only have 1 common base class and you need that for something else
they are really quite different that having a common base class feels forced
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is Inversion of Control?
Okay, I'm new to this site and I've seen that people is really willing to help, so imma take advantage of that and just ask another question if you don't mind.
So, I've readed a lot, I swear, BUT, I just can't seem to figure it out. WHAT in the world is Inversion of Control (IoC or Dependency Injection)? Why are ASP.NET MVC + Repository Pattern projects using it so much? And lastly, what they mean by "containers" and when they say "Inject my Controllers"?
I know it might be an old topic (or even a dumb question) but I just can't seem to get any for-dummies answers.
Think of Dependency Injection/Inversion of Control as little more than a big object factory, a declarative, configuration-driven virtual constructor. Instead of littering your code with calls to "new" that hardwire the concrete type that your client class uses, you're now going to have that virtual constructor instantiate objects for you.
What's the advantage that all that complexity is buying you?
Object creation is now a declarative thing. If you happen to base your design on appropriate interfaces, you can ask the the object factory to create a proxy that implements the same interface when it's convenient. All kinds of good things are now possible: aspect-oriented programming, transparent remoting, declarative transactions, etc.
Simple answer: It lets you hand in the "things" that any given object will use to do its work.
Contrived Example: Say the object wants to get the time for some purpose, you hand it a "ITimeService" and it calls "GetTime" on that.
The purpose of this is to "de-couple" the class from having hard relationships to things you may not wish it, and to aid testing.
In my humble opinion some people go a little overboard, but the testing argument is a valid one, and certainly it's an approach that is useful to adopt at times.
More involved answer: Martin Fowler on Inversion of Control.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Singleton: How should it be used
why we should use singleton class
Well, I would try to avoid using them since effectively you are introducing global elements into your project. However you may decide to use them if you have a resource in your project that you only want to ever have one of. For instance a link to a single database source, a cache or a Factory.
don't............
You are building an application in C#. You need a class that has only one instance, and you need to provide a global point of access to the instance. You want to be sure that your solution is efficient and that it takes advantage of the Microsoft .NET common language runtime features. You may also want to make sure that your solution is thread safe.
Benefits
#
The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs.
#
The Double-Check Locking idiom described earlier in "Multithreaded Singleton" is implemented correctly in the common language runtime.
Liabilities
If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.