tags:

views:

137

answers:

4

To show an example what is this question about:

I have currently a dilemma in PHP project I'm working on. I have in mind a method that will be used by multiple classes (UIs in this case - MVC model), but I'm not sure how to represent such methods in OO design. The first thing that came into my mind was to create a class with static functions that I'd call whenever I need them. However I'm not sure if it's the right thing to do.

To be more precise, I want to work, for example, with time. So I'll need several methods that handle time. I was thinking about creating a Time class where I'd be functions that check whether the time is in correct format etc.

Some might say that I shouldn't use class for this at all, since in PHP I can still use procedural code. But I'm more interested in answer that would enlighten me how to approach such situations in OOP / OOD.

So the actual questions are: How to represent such methods? Is static function approach good enough or should I reconsider anything else?

A: 

Use a class as a namespace. So yes, have a static class.

class Time {
    public static function getCurrentTime() {
        return time() + 42;
    }
}
strager
Static class = tight coupling
Mark Seemann
@Seemann, I prefer simple to "correct" and "proper" solutions (yes, I'm somewhat of a hypocrite at times...). If I need to make a "normal" class later, I can easily refactor the code to do that. Otherwise, it's wasted time, IMO.
strager
A: 

I don't do PHP, but from an OO point of view, placing these sorts of utility methods as static methods is fine. If they are completely reusable in nature, consider placing them in a utils class.

spender
Static class = tight coupling
Mark Seemann
Not that I don't appreciate IoC/dep inj, but in this instance, where a method deals with something as straightforward and non-extendable as time, creating another level of indirection seems to me to be overengineering.
spender
Time is neither straight-forward nor non-extendable. In unit testing it is often very important that you can simulate that the 'current' time has many different values within miliseconds of each other.
Mark Seemann
+5  A: 

I would recommend creating a normal class the contains this behavior, and then let that class implement an interface extracted from the class' members.

Whenever you need to call those methods, you inject the interface (not the concrete class) into the consumer. This lets you vary the two independently of each other.

This may sound like more work, but is simply the Strategy design pattern applied.

This will also make it much easier to unit test the code, because the code is more loosely coupled.


Here's an example in C#.

Interface:

public interface ITimeMachine
{
    IStopwatch CreateStopwatch();

    DateTimeOffset GetNow();
}

Production implementation:

public class RealTimeMachine : ITimeMachine
{
    #region ITimeMachine Members

    public IStopwatch CreateStopwatch()
    {
        return new StopwatchAdapter();
    }

    public DateTimeOffset GetNow()
    {
        return DateTimeOffset.Now;
    }

    #endregion
}

and here's a consumer of the interface:

public abstract class PerformanceRecordingSession : IDisposable
{
    private readonly IStopwatch watch;

    protected PerformanceRecordingSession(ITimeMachine timeMachine)
    {
        if (timeMachine == null)
        {
            throw new ArgumentNullException("timeMachine");
        }        

        this.watch = timeMachine.CreateStopwatch();
        this.watch.Start();
    }

    public abstract void Record(long elapsedTicks);

    public virtual void StopRecording()
    {
        this.watch.Stop();
        this.Record(this.watch.ElapsedTicks);
    }
}
Mark Seemann
This means I should create class for handling custom functions + class for each of these functions and then simply call just execute()? So, for example, function "timeToString" would become a separate class with just execute() function, implementing interface. And I'd call it through new CustomFunction( new TimeToString ); ... $custom_function->execute() ? Is this how Strategy pattern works or do I misunderstand the concept? Isn't it a bit of overkill to create basically class for every single function?
Ondrej Slinták
You don't have to create a class for each function. You can bundle related operations in a single class. That's what encapsulation and cehesion is all about, after all. Those operations can all be defined on the same interface, thus giving you the ability to pass around a single cohesive set of operations in a bundle.
Mark Seemann
Could you, please, edit your post with a short example (in any language)? As I still fail to understand how exactly to do this. Should I simply create a class with multiple functions, implement an interface extracted from members and then inject this interface where ever I need it and call all those functions through it? I think what confuses me is that you mentioned Strategy pattern and I'm trying to implement the solutions exactly by it. I also don't understand why would I have an interface that is implemented by just one class. Just to prevent tight couplings by calling concrete class?
Ondrej Slinták
@Ondrej Slinták: Done
Mark Seemann
+1  A: 

Although you say you want a structure for arbitrary, unrelated functions, you have given an example of a Time class, which has many related functions. So from an OO point of view you would create a Time class and have a static function getCurrentTime(), for example, which returns an instance of this class. Or you could define that the constuctors default behaviour is to return the current time, whichever you like more. Or both.

class DateTime {

    public static function getNow() {
        return new self();
    }

    public function __construct() {
        $this->setDateTime('now');
    }

    public function setDateTime($value) {
        #...
    }

}

But apart from that, there is already a builtin DateTime class in PHP.

soulmerge