Clone functions

Started by
11 comments, last by jpetrie 8 years, 8 months ago

"Clone" is the common name in factory pattern I always saw, that's why I used it.

But maybe name it "Create" is a better choice to avoid confusion with "Clone" ?

Advertisement


"Clone" is the common name in factory pattern I always saw

I believe you're thinking of the Prototype pattern.

Factory pattern : instanciate based on an ID or a name.

One for loop which find the ID or the name and call Clone function to return an instance.

It's more like: a factory is a thing that creates other things based on some input key.

The pattern itself is not defined by "having a for loop which finds the ID or name and calling a clone function." That's just an implementation detail, and there are several other possible ways to implement a factory, both in terms of how the key associated with the result (for loops are not ideal, faster traversal would be better), and in terms of how to actual materialize the resulting object. And other things.

As far as materializing the object, two of the most common options are simply *creating new instances* or *cloning an existing, prototype instance*. The latter case, which is similar to what you've just described, would involve using a "prototype" pattern within the implementation of a factory pattern as well. In the case of the former example, however, it would be (as others have noticed) extremely poor style to call a method "Clone" if it simply returned a fresh, new object.

Remember that patterns are guidelines for discussion of concepts, they are not concrete implementation examples.

This topic is closed to new replies.

Advertisement