2

I am designing a new architecture for my company's software product. I am fairly new to unit testing. I read some horror stories about the use of singleton and static methods, but I am not clearly understanding the problem with using them and would appreciate some enlightenment.

Here is what I am doing:

I have a multi-tier architecture. At the server side level, I am using a series of reusable objects to represent database tables, called "Handlers". These handlers use other objects, like XMLObjects, XMLTables, different Datastructures, etc. Most of these are homemade, not your prepacked objects. Anyway, on top of this layer is a pseudo-singleton layer. The primary purpose of this is to simplify higher levels of server side code and create seamless class management. I can say:

$tablehandler = databasename::Handler('tablename') 

...to get a table. I don't see the inherent problem with this. I am using a stack of handlers (an associative array) to contain instances of the different objects. I am not using globals, and all static datamembers are protected or private. My question is how this can cause problems with unit testing. I am not looking for bandwagon rhetoric, I am looking for a causal relationship. I would also appreciate any other insight to this. I feel like its an extremely flexible and efficient architecture at this point. Any help here would be great.

7
  • 4
    You really don't like white spaces .... Commented Oct 4, 2012 at 2:07
  • that is a nice design pattern, actually. Can't help u with singleton debate, but sure quite innovative thinking Commented Oct 4, 2012 at 2:10
  • I'd say the biggest difference is leveraging interfaces Commented Oct 4, 2012 at 2:12
  • 2
    Static/singleton classes are essentially globals. Read my long answer against globals here: stackoverflow.com/a/12446305/476 Commented Oct 4, 2012 at 2:15
  • @deceze What you wrote on that other post doesn't really apply here. There is no required data outside of the static classes. The classes contain static datamembers and static functions used to access instantiated class objects. There is nothing hardcoded that it depends on to function. The purpose of it is to have access to the lower levels of the architecture without having to instantiate more objects to handle other instantiated objects, which is a massive waste of memory. Commented Oct 4, 2012 at 2:33

1 Answer 1

4

Using static methods to provide access to managed instances--whether they be singletons, pooled objects, or on-the-fly instances--isn't a problem for testing as long as you provide a way for the tests to inject their own mocks and stubs as needed and remove them once complete. From your description I don't see anything that would block that ability. It's merely up to you to build it when necessary.

You'll experience difficulty if you have a class that makes static calls to methods that do the work without an extension point or going through an instance. For example, a call like this

UserTable::findByEmail($email);

makes testing difficult because you cannot plug in a mock, memory-only table, etc. However, changing it to

UserTable::get()->findByEmail($email);

solves the problem because the test can call UserTable::set($mock) in the setup code. For a more detailed example, see this answer.

Sign up to request clarification or add additional context in comments.

2 Comments

Of course, it should be noted that the second example which "solves the problem" is a blatant violation of the Law of Demeter.
@rdlowrey - It seems fairly innocuous given that the OP has decided to use static methods. LoD is more about not exposing internal state, e.g., $order->addItem($item) instead of $order->getItems()->add($item), than it is about the service locator or singleton patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.