# Instantiator vs New for NamedObjectDomainFactory

**URL:** <https://discuss.gradle.org/t/instantiator-vs-new-for-namedobjectdomainfactory/9468>\
**Category:** Help/Discuss\
**Created:** [May 4, 2015, 10:51pm UTC](https://discuss.gradle.org/t/instantiator-vs-new-for-namedobjectdomainfactory/9468 "2015-05-04T22:51:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Appu\_Goundan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/appu_goundan/32/774_2.png) [@Appu\_Goundan](https://discuss.gradle.org/u/Appu_Goundan)\
**Post date:** [May 4, 2015, 10:51pm UTC](https://discuss.gradle.org/t/instantiator-vs-new-for-namedobjectdomainfactory/9468/1 "2015-05-04T22:51:31Z")

</div>

I want to create an extension that looks like :

```
myExtension {
  configurations {
    named1 {
      val = "hello"
    }
    named2 {
      val = "bye"
    }
  }
}

```

Where `configurations` is of type `NamedObjectDomainContainer<MyType>` and I have a `NamedObjectDomainFactory<MyType>`. and I create the container in my extension using `project.container(Named..Container<MyType>, Named..Factory<MyType>)`

What I don’t really understand is the `Instantiator`, any example I’ve seen of `NamedObjectDomainContainer` has some variation of the instatiator being used to create objects. If I implement a NamedObjectDomainFactory, why don’t I just make new objects using `new MyType(name)` instead of `instantiator.newInstance(MyType.class, name)`

What exactly is this instantiator giving me?

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [May 4, 2015, 10:56pm UTC](https://discuss.gradle.org/t/instantiator-vs-new-for-namedobjectdomainfactory/9468/2 "2015-05-04T22:56:18Z")

</div>

Basically, everything enumerated here:

> <https://github.com/gradle/gradle/blob/8faa5e947fcf8f3fcd21403d1f2ac4ba174bba9c/subprojects/core/src/main/groovy/org/gradle/api/internal/AbstractClassGenerator.java#L43>

By encapsulating instantiation inside an `Instantiator`, it allows us to do stuff like “decorate” object with additional capabilities.

---

<div class="post-metadata">

**Author:** ![Appu\_Goundan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/appu_goundan/32/774_2.png) [@Appu\_Goundan](https://discuss.gradle.org/u/Appu_Goundan)\
**Post date:** [May 4, 2015, 11:00pm UTC](https://discuss.gradle.org/t/instantiator-vs-new-for-namedobjectdomainfactory/9468/3 "2015-05-04T23:00:21Z")

</div>

Okay that makes a lot of sense. Is there any reason why Instatiator is in an “internal” package then? Is there any reason to use NamedObjectDomainContainers without it?

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [May 4, 2015, 11:04pm UTC](https://discuss.gradle.org/t/instantiator-vs-new-for-namedobjectdomainfactory/9468/4 "2015-05-04T23:04:45Z")

</div>

It’s not part of the public API. In general, Gradle handles instantiating objects for you, and how that happens is an implementation detail. The `NamedDomainObjectFactory` interface is simply there if you want to provide the instantiation implementation. Otherwise, Gradle will simply look for a constructor with a single `String` argument which is used for the item’s name.

---

<div class="post-metadata">

**Author:** ![Appu\_Goundan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/appu_goundan/32/774_2.png) [@Appu\_Goundan](https://discuss.gradle.org/u/Appu_Goundan)\
**Post date:** [May 4, 2015, 11:41pm UTC](https://discuss.gradle.org/t/instantiator-vs-new-for-namedobjectdomainfactory/9468/5 "2015-05-04T23:41:49Z")

</div>

Ah okay, so I can avoid this for now. Thanks!
