开发者

Where to put methods used by multiple classes?

开发者 https://www.devze.com 2022-12-19 04:52 出处:网络
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)

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 metho开发者_运维百科ds 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?


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);
    }
}


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.


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

class Time {
    public static function getCurrentTime() {
        return time() + 42;
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号