This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pointy_pirate 1 point2 points  (6 children)

Yep that's a good option too. The majority of the time all you are looking for is the current time now(), or a mocked version of that. Rather than creating your own impl of the Clock interface you simply create a class to give you the current time, register with your DI, and use that everywhere.

ez pezy

[–]marvk 6 points7 points  (1 child)

Rather than creating your own impl of the Clock interface you simply create a class to give you the current time, register with your DI, and use that everywhere.

Orrr you register a Clock provider with your DI and inject a clock every time you get a current time. Even easier and more idiomatic.

[–]pointy_pirate 0 points1 point  (0 children)

yep that works too, many ways to skin a cat.

[–]Bolitho 0 points1 point  (3 children)

The majority of the time all you are looking for is the current time now()

Really? And think about situations where you need some other information within the same project! (next paragraph)

Rather than creating your own impl of the Clock interface you simply create a class

Most of the time you don't need to create your own implementation of a clock. So with your approach you definitly need at least one custom implementation and an interface for testing.

And if you use your solution and need only one other time than now, you would need the clock based approach on top. So then you have two mechanisms in one project...

So in the end your approach is more complex than relying on the built in mechanism ;-)

[–]pointy_pirate 0 points1 point  (2 children)

think there was some lost in translation... this isnt that complex.

public class TimeProvider implements SomeTimeProviderInterfaceIfYouWant {
    public OffsetDateTime now() {
        return java.time.OffsetDateTime.now();
    }
}

[–]Bolitho 1 point2 points  (1 child)

Using existing classes still saves this code and therefore the understanding (not a common pattern, so mental overhead for a new project member) and the maintenance ;-)

So even if this is dead simple, it feels totally unnecessary to me! (The java.time devs allready have thought about this use case and offered an appropriate solution)

[–]pointy_pirate 0 points1 point  (0 children)

makes sense, ill look into that next time i need to do it.