0

Is there any Guideline or pattern which suggest regarding the creation of an object within the same Class, is it ok or not recommended?

Example: Car is a class, lets say there is a method called StartEngine(); does it makes sense to create an object (myCar) in the StartEngine method?

Pls note: I did googling already but did not find any relevant info and also may be this question looks very basic....but still asking; my apologize. Thanks.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
Mohammed Hameed
  • 73
  • 1
  • 10
  • 1
    **Why** do you want to do this? Give an example. Technically it's possible and there are valid cases. One example are [immutable objects that return a modified copy](https://blogs.msdn.microsoft.com/andrewarnottms/2013/01/08/simple-immutable-objects/). – Dennis Kuypers Oct 15 '18 at 02:49
  • Yes, I know it is possible and agreed there are valid cases as well. Just was curious to know about any recommended guidelines or approach which suggest to avoid it and instead recommend any other way. – Mohammed Hameed Oct 15 '18 at 02:54
  • 1
    I belieave there is no special guidline about this. If you need another object to be instantiated within a method - why not? As for cars, when I start engine of mine, no other car spawned around, so I think this example doesn't make sense. – vasily.sib Oct 15 '18 at 02:55
  • Adding to what Dennis said. This is kind of used/done when you have to build a Reactive/Data Stream where you say. Object.DoThis().ThenDoThis().AndFinallyThat(); – Prateek Shrivastava Oct 15 '18 at 02:56
  • 2
    It sounds like you may want to have `StartEngine()` be a `static` method of the `Car` class which returns back a new `Car` instance. E.g. `var myCar = Car.StartEngine();` The method would look like `public static Car StartEngine() { return new Car(); }` – C. Mitchell Oct 15 '18 at 02:57
  • Ohh, plz note that car and startengine() I just gave them as example; it is not my requirement. Just wanted to know if any design guideline is out there regarding this usecase. – Mohammed Hameed Oct 15 '18 at 03:03
  • 3
    The singleton design pattern creates an instance of it's own class, but it's not for the same purpose as your example. – Rufus L Oct 15 '18 at 03:10
  • 1
    It's possible and whether you should do it depends on what you're trying to achieve. For instance, the `Stopwatch` class has a `Start` method that you can call on an existing instance and it also has a `static`/`Shared` `StartNew` method that creates an instance, starts it and then returns it. It's really up to you whether it's appropriate to do that sort of thing for any particular class. I would think that you'd only do it for classes that didn't require significant (perhaps any) configuration when created, but that's subjective. – jmcilhinney Oct 15 '18 at 03:35
  • I think you are referring to singleton pattern that instantiate single shared instance which is not thread safe in nature. – Michel Hanna Oct 15 '18 at 03:38
  • @jmcilhinney Nice explanation...it's now more clear to me. thnx. – Mohammed Hameed Oct 15 '18 at 04:00

0 Answers0