3

I faced with the code below

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

here to make a new object of XmlReader, it just used calling method of XmlReader.

I know here Create is a static method, but it is a little odd for me. I used to exploit new word to command making new instance.

Can anyone please tell me how does this line works?

1

5 Answers 5

10

The method you're calling does it for you:

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

(It's possible to avoid new altogether by using a technique called reflection, but that's not what's going on here.)

Sign up to request clarification or add additional context in comments.

Comments

10

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

Somewhere in XmlReader.Create it is internally making use of the new keyword. In order to instantiate a new instance of an object, you need to use new to call a constructor. To reduce coupling between objects, however, you may abstract that behind a factory method 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 an example this simple, there's little difference between the two. As code evolves and becomes more complex, there can be a number of compelling reasons to choose one over the other. For example:

  1. There are complex constructors and you want to expose a simple interface for building an instance.
  2. The constructors are likely to change often and you want consumers of your library to have a single unchanging interface.
  3. There is significant logic invoked when building an object and you want to move that logic to its own object.
  4. You want to use a separate object for mockability in automated testing.
  5. You have a more complex inheritance structure and want to expose a top-level abstract factory.
  6. etc.

Comments

1

It's a static method that, in it's body, creates a new object (using new) and returns it.

You can emulate the pattern like so:

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

Comments

0
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 can look like under.

Comments

0

There are several reasons to create factory Methods. Do you want control about all the Instances created of a Type? For example you could do something like that:

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 could also be in a seperate 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 an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Read more about Factory Methods here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.