1

Are there any creational design patterns that allow for completely new objects (as in newly written) to be instantiated without having to add a new statement somewhere in existing code?

zzelman
  • 425
  • 5
  • 14
  • Do you mean that you shouldn't have to change your program to refer to the new class at all (not just that you shouldn't have to say `new`)? Or is your question really about how to instantiate a class that you have a reference to but don't know anything about? – Dave Schweisguth May 06 '16 at 05:38

2 Answers2

1

One things which I think here is that someone needs to do a new to your newly written Class. If you are not doing that then may be some framework needs to do that.

I could remember something similar which I did in one of my side projects using Java Spring . I had an interface and multiple implementations to it. My project required to iterate over all the implementations do some processing. Now for this even I was looking for some solution with which I would not have to manually do the instantiation of that particular class or some explicit wiring of that particular class. So Spring facilitated me to do that through @Autowired annotation. It injected all the implementation of that interface on the fly. Eg :-

@Autowired
private List<IMyClass> myClassImplementations;

Now in the above example I can simply iterate over the list of implementations injected and I would not have to do instantiation of my new implementation every time I write a new one.

But I think in most of the cases it would be difficult to use this approach (even though it fits in my case). I would rather go with a Factory pattern in general case and try to use that for creating new instances. I know that would require new but in my perception engineering it in a way that its object is automatically created and injected is a bit an extra overhead.

Siddharth
  • 2,046
  • 5
  • 26
  • 41
1

The main problem to solve is how to identify the class or classes to instantiate. I know of and have used three general patterns for discovering classes, which I'll call registration, self-registration and discovery by type. I'm not aware of them having been written up in a formal pattern description.

Registration: Each class that wants to be discovered is registered somewhere where a framework can find it:

  • the class name is put in an environment variable or Java system property, in a file, etc.
  • some code adds the class or its name to a singleton list early in program execution

Self-registration: Each class that wants to be discovered registers itself, probably in a singleton list. The trick is how the class knows when to do that.

  • The class might have to be explicitly referred to by some code early in the program (e.g. the old way of choosing a JDBC driver).
  • In Ruby, the code that defines a class can register the class somewhere (or do anything else) when the class is defined. It suffices to require the file that contains the class.

Discovery by type: Each class that wants to be discovered extends or implements a type defined by the framework, or is named in a particular way. Spring autowiring class annotations are another version of this pattern.

There are several ways to find classes that descend from a given type in Java (here's one SO question, here's another) and Ruby. As with self-registration, in languages like those where classes are dynamically loaded, something has to be done to be sure the class is loaded before asking the runtime what classes are available.

Community
  • 1
  • 1
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
  • Fantastic! I never knew those language constructs existed, and they were what I was looking for. (btw sorry for the late accept, life got in the way) – zzelman May 16 '16 at 11:51