Access to static properties via this.constructor in typescript

but in typescript this.constructor.prop causes error “TS2339: Property ‘prop’ does not exist on type ‘Function’”. Typescript does not infer the type of constructor to be anything beyond Function (after all … the constructor might be a sub class). So use an assertion: class SomeClass { static prop = 123; method() { (this.constructor as typeof SomeClass).prop; … Read more

Difference between static function and singleton class in swift [closed]

Sure this sounds confusing and can be debated. However, from the best practices i can put some suggestions. Singleton is usually used to create a resource intensive and one timer initialisation for instance: a database connector, login handler and such. Utility class are classes that only have static functions and variables. It should not deal … Read more

Why aren’t static methods considered good OO practice? [closed]

Object-orientation is about three things: messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. Of those three, the most important one is messaging. Static methods violate at least messaging and late-binding. The idea of messaging means that in OO, computation is performed by networks of self-contained objects which send … Read more

How to verify static void method has been called with power mockito

If you are mocking the behavior (with something like doNothing()) there should really be no need to call to verify*(). That said, here’s my stab at re-writing your test method: @PrepareForTest({InternalUtils.class}) @RunWith(PowerMockRunner.class) public class InternalServiceTest { //Note the renaming of the test class. public void testProcessOrder() { //Variables InternalService is = new InternalService(); Order order … Read more

c++ automatic factory registration of derived types

I use a singleton with a member for registration, basically: template< typename KeyType, typename ProductCreatorType > class Factory { typedef boost::unordered_map< KeyType, ProductCreatorType > CreatorMap; … }; Using Loki I then have something along these lines: typedef Loki::SingletonHolder< Factory< StringHash, boost::function< boost::shared_ptr< SomeBase >( const SomeSource& ) > >, Loki::CreateStatic > SomeFactory; Registration is usually … Read more

Is there a way to force a C# class to implement certain static functions?

No, there is no language support for this in C#. There are two workarounds that I can think of immediately: use reflection at runtime; crossed fingers and hope… use a singleton / default-instance / similar to implement an interface that declares the methods (update) Actually, as long as you have unit-testing, the first option isn’t … Read more

Python decorator as a staticmethod

This is not how staticmethod is supposed to be used. staticmethod objects are descriptors that return the wrapped object, so they only work when accessed as classname.staticmethodname. Example class A(object): @staticmethod def f(): pass print A.f print A.__dict__[“f”] prints <function f at 0x8af45dc> <staticmethod object at 0x8aa6a94> Inside the scope of A, you would always … Read more

How can I dynamically create class methods for a class in python [duplicate]

You can dynamically add a classmethod to a class by simple assignment to the class object or by setattr on the class object. Here I’m using the python convention that classes start with capital letters to reduce confusion: # define a class object (your class may be more complicated than this…) class A(object): pass # … Read more