Creating a new instance of an object without using "new"

I came across the code below

XmlReader xmlreader = 
    XmlReader.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");

      

here to create a new object XmlReader

it just used the call method XmlReader

.

I know there Create

is a static method here, but it's a little strange to me. I used to use the word new

for command creating a new instance.

Can anyone please tell me how this line works?

+3


source to share


5 answers


The method you call does this for you:

public class XmlReader {
    public static XmlReader Create(String url) {
        // There probably a lot of fancy code in this method, but it'll use new somewhere
        return new XmlReader(...);
    }
}

      



(Can be avoided altogether by new

using a technique called reflection, but that's not what's happening here.)

+10


source


This is an example of a factory method. (Which can often be a step towards using a separate factory object.)

Somewhere in XmlReader.Create

it uses a keyword new

. To instantiate a new instance of an object you need to use new

to call the constructor. However, to reduce communication between objects, you can abstract what is behind the factory or factory object.

For example, these two implementations do roughly the same thing:

public class Widget
{
    public Widget() { }
}

//... elsewhere ...

var widget = new Widget();

      



and

public class Widget
{
    private Widget() { }

    public static Widget Create()
    {
        return new Widget();
    }
}

//... elsewhere ...

var widget = Widget.Create();

      

In the example, it is simple, there is little difference between them. As the code evolves and gets more complex, there can be a number of good reasons to choose one of these. For example:

  • There are complex constructors and you want to expose a simple interface to instantiate.
  • Constructors can change frequently, and you want your library users to have one consistent interface.
  • There is significant logic when constructing an object and you want to move that logic into your own object.
  • You want to use a separate mock object for automated testing.
  • You have a more complex inheritance structure and want to provide an abstract top-level abstraction of a factory.
  • and etc.
+10


source


It is a static method that creates a new object in it (using new

) and returns it.

You can emulate a template like this:

public class Foo
{
    public static Foo Create()
    {
        return new Foo();
    }
}

      

+1


source


public class Foo
{
 public string Prop { get;set; }
 public Foo(string prop)
 {
  Prop = prop;
 }

 public static Foo Create(string prop)
 {
  return new Foo(prop);
 } 
}

      


This is how it might look.

0


source


There are several reasons for creating factory methods. Do you want to control all instances created for a type? For example, you can do something like this:

public class MyClass {

    private MyClass() // private constructor optional
    {}

    public void Create()
    {
        return new MyClass();
    }

}

      

(private constructors are often used to implement the Singleton Pattern )

Factory Methods can also be in a separate class.

public class MyClass {
    internal MyClass() // means only Classes of same assembly may access this
    {}

}

public class MyClassFactory {

    public void NewMyClass()
    {
        // Do some license checking here or whatever
        return new MyClass();
    }
}

      

Factory Methods define the interface for creating an object, but let subclasses define which class to create. The factory method allows the cluster to defer an instance to subclasses.

Read more about Factory Ways here.

0


source







All Articles