Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put methods used by multiple classes?

Tags:

oop

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?

like image 306
Ondrej Slinták Avatar asked Feb 03 '10 10:02

Ondrej Slinták


2 Answers

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);
    }
}
like image 195
Mark Seemann Avatar answered Sep 28 '22 04:09

Mark Seemann


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.

like image 24
soulmerge Avatar answered Sep 28 '22 02:09

soulmerge