static-methods

Static Methods in an Interface/Abstract Class

First off, I understand the reasons why an interface or abstract class (in the .NET/C# terminology) cannot have abstract static methods. My question is then more focused on the best design solution. What I want is a set of "helper" classes that all have their own static methods such that if I get objects A, B, and C from a third party ...

How to mock with static methods?

I'm new to mock objects, but I understand that I need to have my classes implement interfaces in order to mock them. The problem I'm having is that in my data access layer, I want to have static methods, but I can't put a static method in an interface. What's the best way around this? Should I just use instance methods (which seems ...

C# method can be made static, but should it?

Resharper likes to point out multiple functions per asp.net page that could be made static. Does it help me if I do make them static? Should I make them static and move them to a utility class? ...

"Inline" Class Instantiation in PHP? (For Ease of Method Chaining)

An idiom commonly used in OO languages like Python and Ruby is instantiating an object and chaining methods that return a reference to the object itself, such as: s = User.new.login.get_db_data.get_session_data In PHP, it is possible to replicate this behavior like so: $u = new User(); $s = $u->login()->get_db_data()->get_session_dat...

Class with single method -- best approach?

Say I have a class that's meant to perform a single function. After performing the function, it can be destroyed. Is there any reason to prefer one of these approaches? // Initialize arguments in constructor MyClass myObject = new MyClass(arg1, arg2, arg3); myObject.myMethod(); // Pass arguments to method MyClass myObject = new MyClass...

Getting the name of a child class in the parent class (static context)

Hi everybody, I'm building an ORM library with reuse and simplicity in mind; everything goes fine except that I got stuck by a stupid inheritance limitation. Please consider the code below: class BaseModel { /* * Return an instance of a Model from the database. */ static public function get (/* varargs */) { //...

C#: Determine derived object type from a base class static method

In a C# program, I have an abstract base class with a static "Create" method. The Create method is used to create an instance of the class and store it locally for later use. Since the base class is abstract, implementation objects will always derive from it. I want to be able to derive an object from the base class, call the static C...

The best way to invoke methods in Python class declarations?

Say I am declaring a class C and a few of the declarations are very similar. I'd like to use a function f to reduce code repetition for these declarations. It's possible to just declare and use f as usual: >>> class C(object): ... def f(num): ... return '<' + str(num) + '>' ... v = f(9) ... w = f(42) ... >>> C.v...

Why can't static methods be abstract in Java

The question is in Java why can't I define an abstract static method? for example abstract class foo { abstract void bar( ); // <-- this is ok abstract static void bar2(); //<-- this isn't why? } ...

Refactoring To Remove Static Methods Code Smell

I have the current basic structure for each domain object that I need to create: class Model_Company extends LP_Model { protected static $_gatewayName = 'Model_Table_Company'; protected static $_gateway; protected static $_class; public static function init() { if(self::$_gateway == null) { self::...

How do I know if a C# method is thread safe?

I'm working on creating a call back function for an ASP.NET cache item removal event. The documentation says I should call a method on an object or calls I know will exist (will be in scope), such as a static method, but it said I need to ensure the static is thread safe. Part 1: What are some examples of things I could do to make it u...

How to setup an Object Creation Interface "rule" in C#?

The general rule is that I want to say, "T has a method with a String parameter which will return List." Put verbosely, we might call the interface ICanCreateListOfObjectsFromString. A possible application might be search. It feels like it'd be nice to have a static method in my interface, but I know that's not allowed in C#. What is...

Is there any advantage in using a Python class?

I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions? ...

.Net Static Methods and it's effects on Concurrency ?

I am currently building an API which will be used by a webservice. I was wondering what performance issues I could meet if I built my API using a large amount of static methods. The original idea was to build expert objects which act as services. In a single user environment this approach was great! But I will soon need to port this...

Why can't I define a static method in a Java interface?

Here's the example: public interface IXMLizable<T> { public static T newInstanceFromXML(Element e); public Element toXMLElement(); } Of course this won't work. But why not? One of the possible issues would be, what happens when you call: IXMLizable.newInstanceFromXML(e); In this case, I think it should just call an empty meth...

Java - static methods best practices

Let's say I have a class designed to be instantiated. I have several private "helper" methods inside the class that do not require access to any of the class members, and operate solely on their arguments, returning a result. public class Example { private Something member; public double compute() { double total = 0; ...

How to write class methods that return collections of instances

I'm struggling to define a class method that populates and returns a collection of instances. The issue I don't know how to get around is that I have private attributes to populate. Let's use the example of a Book class. I don't want the code to directly set (say) the availability of a book. I want the code to have to use a CheckOut ...

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

I am developing a set of classes that implement a common interface. A consumer of my library shall expect each of these classes to implement a certain set of static functions. Is there anyway that I can decorate these class so that the compiler will catch the case where one of the functions is not implemented. I know it will eventually ...

Returning values...(Edited with new code and more questions) - illegal start of expression

These are my questions: I'm getting a couple of errors on the line "public static boolean validNumCheck(String num){" - "illegal start of expression", "';' expected", and "')' expected". How can I give the user 3 tries in total for each number? I believe right now the programme asks the user for 3 numbers and gives them 3 tries in tota...

Static methods or Singletons performance-wise (Android)?

In an app with a small number of POJOs and lots of helper methods that operate on them, what's better performance-wise: to make the helper classes singletons or to make the methods static? ...