1

I was reading this blog post about Hexagonal architecture and at the bottom it says:

The Loopback pattern is an explicit pattern for creating an internal replacement for an external device.

When I google for "Loopback pattern" I don't find any details about it. Does anyone know what the author is referring to? For curiosity's sake, I'd like to know how to implement this.

3
  • 2
    Please see Discuss this ${blog} Apr 29, 2014 at 20:36
  • 2
    Also, if you search for "loopback" on that page, you will find a link to c2.com/cgi/wiki?LoopBack which appears to be the same author recording a short conversation he had with somebody else. There doesn't seem to be anything really interesting here. Apr 29, 2014 at 20:47
  • Is this even a pattern? You can talk to local objects using OO style and remote objects using the Proxy pattern. This is an absence of a proxy and just normal OO. It looks like the original blog post was talking about Mocking. I'm assuming this is about creating a local mock of an external service. Apr 29, 2014 at 22:30

2 Answers 2

3

As an example, I recently had a dependency on a remote Authorization service to validate user access tokens. But then I wrote a service that didn't need Authorization -- it just made calculations. So I implemented the AuthorizationService interface (name changed to protect innocent services) with a NoAuthorizationService that just returned true for validate(token).

I could use that same stub interface to stub out calls to the actual Authorization service. But I considered it a production artifact rather than a test artifact, because I could use that for a live service to plug into if I didn't want to otherwise disable calls to that service.

Think of "127.0.0.1", the loopback IP -- it just goes to localhost. It's a similar idea in a hexagonal architecture, where calls to a remote service are just looping back to the local service instead.

...

In the more common Gang Of Four lingo, this would be the Null Object pattern. Or else a "sentinel" object that takes the place of the real thing but doesn't do anything.

2
  • Not sure I'd agree that this is a case of a Null object pattern. Null doesnt do anything, the quote from the OP clearly stated that this is actually the local system doing something. Apr 29, 2014 at 22:31
  • If you want the loopback to actually perform the function, but locally, then that's more a matter of "embedding" the service and invoking the interface directly. It depends on if you consider the loopback to be a no-op or not. Either way, you're providing a local implementation of a remote interface.
    – sea-rob
    Apr 29, 2014 at 23:32
1

I cannot speak for the author, but it sounds like this is used just as the quote says: replace an external device with software that allows testing other parts of the system that may use it.

For example, one could write a "loopback" printer driver that allows testing program functionality related to printing without actually printing anything. This could be a device that never runs out of paper and never actually consumes any real-world resources (i.e. paper) because it is a virtual device.

I have used these stubs before to test program functionality that interacts with hardware devices that are unreliable (e.g. might run out of paper) or that require user intervention (just fire an event with fake user input) but that do not need to track anything at the driver level (e.g. did a print job get submitted? How many?)

When tests are running in a headless environment in the middle of the night, such a virtual device is crucial.

3
  • How is this different from a mock? Apr 29, 2014 at 20:58
  • Mock is a proxy that is able to fail a test, as opposed to a stub that cannot fail a test. Mock also tracks its own interactions. For example, when I call a mocked object's method, the call gets tracked so that later I can verify that I called the method so many times.
    – CodeART
    Apr 29, 2014 at 21:12
  • 1
    @CodeART thanks, stub was the word I was thinking but did not actually type in my answer anywhere. I blame the lack of caffeine. I updated my answer to be a little more clear in this regard.
    – user22815
    Apr 29, 2014 at 21:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.