static-classes

AS3: Static class versus Singleton

非 Y 不嫁゛ 提交于 2019-12-06 01:05:25
问题 I know SO pals are not fans of "versus" questions but... even rephrasing the tittle a bit, the versus part is still there, so, why hide it. Basically, I'd like to know when and why should I use a singleton or a static class, what can offer a singleton that a static class cant, and vice versa. For a long time I used both, and I cant see a reason why I shouldn't use one over the other. Thanks. 回答1: Both are basically global variables with caveats. Static classes prevent inheritance, and

What's the difference between enums & using static classes with constants?

家住魔仙堡 提交于 2019-12-05 22:53:32
What are the performance implications between these two items? I've seen the static class in the wild recently and I'm not sure what to make of it. public enum SomeEnum { One = 1, Two, Three } public static class SomeClass { public static readonly int One = 1; public static readonly int Two = 2; public static readonly int Three = 3; } The difference is type safety. Suppose you have two of these enums. How are you going to tell the difference: void SomeMethod(int x, int y) // Compiles, but won't do what you want. SomeMethod(SomeOtherClass.Xyz, SomeClass.One); vs void SomeMethod(SomeEnum x,

Extension methods on a static class? [duplicate]

半城伤御伤魂 提交于 2019-12-05 16:49:32
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Can I add extension methods to an existing static class? I know i can do the below to extend a class. I have a static class i would like to extend. How might i do it? I would like to write ClassName.MyFunc() static public class SomeName { static public int HelperFunction(this SomeClass v) 回答1: You can't have extension methods on static classes because extension methods are only applicable to instantiable types

Having separate copy of base class static member in each derived class

浪子不回头ぞ 提交于 2019-12-05 12:59:57
I have following class structure: public abstract class PresenterBase { public static Dictionary<string, MethodInfo> methodsList; public void Bind() public void Initialize(); } public class DevicePresenter: PresenterBase { public void ShowScreen(); public void HandleEvents(); } public class HomePresenter: PresenterBase { public void ShowScreen(); public void HandleEvents(); } I want to have HomePresenter and DevicePresenter to have separate copy of methodsList static member defined in PresenterBase. Unfortunately they share the same copy with above implementation. Is they alternative approach,

Should I never use static methods and classes and singletons when following the Test Driven Development paradigm

你。 提交于 2019-12-05 10:52:35
问题 I've been reading that static methods, static classes, and singletons are evil when you try to implement unit testing in your project. When following the TDD paradigm, should I just forget that they ever existed and never use them again or is it ok to use them sometimes? 回答1: Never say never--static classes and methods have their place in your toolbox. That said, if the class you are trying to isolate and test (subject under test or SUT) depends on a static class or method, you will be unable

PHP avoid static classes to avoid dependencies, but I need to use global everywhere

烈酒焚心 提交于 2019-12-05 04:19:22
问题 Many times I heard to avoid static classes because they will insert dependencies that will render your code unusable in other projects, and will not allow to unit test it. Let's say we have a typical class DB to access the Data Base, if such class is static we could call it wherever in our code: DB::execQuery(...); but this creates dependencies, so let's make the DB class NOT static, in such case we would have somewhere in our code: $db = new DB(); and then we could call in our code $db-

How to inject into static classes using Dagger?

假如想象 提交于 2019-12-05 01:47:24
I want to introduce dependency injection through Dagger to a project. The following code acts as an example to describe the problem of injection into static classes . The static method setupTextView() is called from multiple classes: public abstract class TextViewHelper { public static void setupTextView(TextView textView, Spanned text, TrackingPoint trackingPoint) { textView.setText(text, TextView.BufferType.SPANNABLE); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyApp.getTracker

C++ High performance unit testing with Google Mock?

烈酒焚心 提交于 2019-12-04 08:47:06
I'm using Google Mock, and I'm struggling to mock out C++ system calls (specifically the C++11 chrono functions). I'm know I'm supposed to make an interface, create a class to implement the interface for my actual implementation, and then mock out the interface on my tests. I'm trying to write an embedded application, so this level of indirection sounds too expensive for me. What is the most efficient/performant way to incorporate system calls into Google Mock? No, you don't need to resort to mocked static classes - that's one of many options. If you're in an embedded environment where a

AS3: Static class versus Singleton

徘徊边缘 提交于 2019-12-04 05:46:48
I know SO pals are not fans of "versus" questions but... even rephrasing the tittle a bit, the versus part is still there, so, why hide it. Basically, I'd like to know when and why should I use a singleton or a static class, what can offer a singleton that a static class cant, and vice versa. For a long time I used both, and I cant see a reason why I shouldn't use one over the other. Thanks. Both are basically global variables with caveats. Static classes prevent inheritance, and Singletons are ugly and add overhead with a property lookup. Both make automated testing difficult. AS3 supports

Extension methods on a static class? [duplicate]

妖精的绣舞 提交于 2019-12-04 02:28:20
This question already has answers here : Closed 6 years ago . Possible Duplicate: Can I add extension methods to an existing static class? I know i can do the below to extend a class. I have a static class i would like to extend. How might i do it? I would like to write ClassName.MyFunc() static public class SomeName { static public int HelperFunction(this SomeClass v) You can't have extension methods on static classes because extension methods are only applicable to instantiable types and static classes cannot be instantiated. Check this code.. public static bool IsEmail(this string email) {