Where to use the methods used by several classes?

To show an example what this question is about:

I currently have a dilemma in the PHP project I'm working on. I mean the method that will be used by several classes (the UI in this case is the MVC model), but I'm not sure how to present such methods in the OO design. The first thing that occurred to me was to create a class with static functions, which I would call whenever I needed it. However, I'm not sure if this is the right thing to do.

To be more precise, I want to work, for example, over time. Therefore, I will need several methods that handle time. I was thinking of creating a Time class, where I will function, checking if the time is in the correct format, etc.

Some may say that I should not use a class for this at all, since in PHP I can still use procedural code. But I'm more interested in the answer that will enlighten me how to approach such situations in OOP / OOD.

So, relevant issues are: How to present such methods? Is the static function approach good enough or should I review something else?

+6
oop
source share
4 answers

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

Whenever you need to call these methods, you add an interface to the user (rather than a specific class). This allows you to change two independently of each other.

It may sound like more work, but it's just a Strategy design template.

This will also simplify unit test code because the code is looser bound.


Here is 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 is the user 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); } } 
+5
source share

Although you say you want a structure for arbitrary, unrelated functions, you gave an example of the Time class, which has many related functions. Thus, from the point of view of OO, you must create a time class and, for example, the static function getCurrentTime() , which returns an instance of this class. Or you could determine that the default behavior in constuctors is to return the current time, depending on what you like best. Or both.

 class DateTime { public static function getNow() { return new self(); } public function __construct() { $this->setDateTime('now'); } public function setDateTime($value) { #... } } 

But apart from this, there is already a built-in DateTime class in PHP .

+1
source share

Use the class as a namespace. So yes, there is a static class.

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

I do not do PHP, but from an OO point of view, the placement of utility methods like static methods is in order. If they can be fully reused, consider placing them in the utils class.

0
source share

All Articles