4

I'm still trying to grasp both concepts, so if I'm misunderstanding something kindly explain.

I've read a few sources on idempotent operations (namely the SO entry) and yesterday a senior dev at my workplace sent this article around about singleton's. While reading the singleton article I was wondering, is a constructor for an object that properly implements the singleton pattern idempotent?

My understanding is that it would be because calling Singleton.Instance() (v. 6 from the Singleton article) more than 1 time would not change anything because a singleton cannot be instantiated more than once, but I'm not entirely sure if I'm combining the two topics correctly.

Community
  • 1
  • 1
Alex Vallejo
  • 1,331
  • 1
  • 12
  • 15
  • Yes, it is. Calling the constructor a second (or third, etc.) time does not change any state. So it's idempotent. – Jim Mischel Jul 25 '13 at 15:12
  • I'd like my avatar picture back, please. Was here first :) – Dialecticus Jul 25 '13 at 15:13
  • Jim, thanks! Dialecticus, It's a great picture, what can I say? haha – Alex Vallejo Jul 25 '13 at 15:15
  • Note that programmers use "idempotent" differently from how mathematicians use it. For mathematicians, a function `foo` is said to be an *idempotent function* if for all `x` the value `foo(x)` is a *fixed point* of `foo`. That is, `foo(foo(x))` is always equal to `foo(x)`. Programmers use idempotent to mean "there is no difference between calling a function once and calling the function two, three, four.. times", which is different. For example, in the mathematician sense, `abs` is idempotent because `abs(abs(x))` equals `abs(x)` for all `x`. – Eric Lippert Jul 25 '13 at 17:19
  • And yes, a singleton factory is a good example of an idempotent operation, in the programmer sense. It is not an example of an idempotent operation in the mathematician sense. – Eric Lippert Jul 25 '13 at 17:21

1 Answers1

1

When an operation is described as idempotent then what that really means is any calls made to it with the same parameters should result in no additional state change. For example, in a REST API a DELETE request is generally idempotent which means if I make subsequent requests to delete a resource and it's already gone I would still get a success back. So from the users point of view it appears like something has happened, but really the system isn't changing anything it's simply acknowledging the request i.e.

User: "Please delete resource A"

System: "Check status of resource A"

System: "Resource A found, deleting Resource A" || "Resource A not found"

System: "Return OK"

So taking that into the concept of a singleton it works pretty much the same. You make subsequent requests to create an object, however, the operation is only performed once - the rest are simply returned the created instance.

James
  • 80,725
  • 18
  • 167
  • 237
  • gotcha. I forgot idempotent functions must have the same parameters, so I guess a singleton constructor could be idempotent but doesn't have to be (depending on if it accepts parameters or not) – Alex Vallejo Jul 25 '13 at 15:46