B
BigStef
Hi everybody,
I was reading this article on MSDN: http://msdn.microsoft.com/en-us/library/ms954600.aspx
and I was wondering about the implementation explained in this article
about passing a factory instance from the main class to the class that
calls the factory methods. I'm puzzled about the approach explained
there.
Here's a bit of what is explained in this article and what approach I
was used to take:
MSDN's:
class ComputerAssembler {
public void AssembleComputer(ComputerFactory factory) { //
<---------------------- Receives the factory instance
Computer computer = factory.GetComputer();
Console.WriteLine("assembled a {0} running at {1} MHz",
computer.GetType().FullName, computer.Mhz);
}//AssembleComputer
}//ComputerAssembler
class MainClass {
static void Main(string[] args) {
ComputerFactory factory = new ConcreteComputerFactory(); //
<----------- Creates an instance of the factory to use...
new ComputerAssembler().AssembleComputer(factory); //Passing the
instance to a method
}//Main
}//MainClass
My approach:
class ComputerAssembler {
private static ComputerFactory factory = new
ConcreteComputerFactory(); //<----------- Creates an instance of the
factory to use
public void AssembleComputer() {
Computer computer = factory.GetComputer();
Console.WriteLine("assembled a {0} running at {1} MHz",
computer.GetType().FullName, computer.Mhz);
}//AssembleComputer
}//ComputerAssembler
class MainClass {
static void Main(string[] args) {
new ComputerAssembler().AssembleComputer(); //Not passing any
factory instance since it's encapsulated within the ComputerAssembler
class
}//Main
}//MainClass
As you can see, the client doesn't have to know that a factory is
involved in the creation of a specific object. It just calls whatever
the method needed to.
One discussion I had with another developper is the fact that the
MSDN's example is also extending this sample a bit further by reading
command line args that might have been passed to instanciate the
appropriate factory then pass it from the main class. This can be
easilly done by using generics. I was just wondering if there were any
benefits from doing this that way.
Thanks,
Steph
I was reading this article on MSDN: http://msdn.microsoft.com/en-us/library/ms954600.aspx
and I was wondering about the implementation explained in this article
about passing a factory instance from the main class to the class that
calls the factory methods. I'm puzzled about the approach explained
there.
Here's a bit of what is explained in this article and what approach I
was used to take:
MSDN's:
class ComputerAssembler {
public void AssembleComputer(ComputerFactory factory) { //
<---------------------- Receives the factory instance
Computer computer = factory.GetComputer();
Console.WriteLine("assembled a {0} running at {1} MHz",
computer.GetType().FullName, computer.Mhz);
}//AssembleComputer
}//ComputerAssembler
class MainClass {
static void Main(string[] args) {
ComputerFactory factory = new ConcreteComputerFactory(); //
<----------- Creates an instance of the factory to use...
new ComputerAssembler().AssembleComputer(factory); //Passing the
instance to a method
}//Main
}//MainClass
My approach:
class ComputerAssembler {
private static ComputerFactory factory = new
ConcreteComputerFactory(); //<----------- Creates an instance of the
factory to use
public void AssembleComputer() {
Computer computer = factory.GetComputer();
Console.WriteLine("assembled a {0} running at {1} MHz",
computer.GetType().FullName, computer.Mhz);
}//AssembleComputer
}//ComputerAssembler
class MainClass {
static void Main(string[] args) {
new ComputerAssembler().AssembleComputer(); //Not passing any
factory instance since it's encapsulated within the ComputerAssembler
class
}//Main
}//MainClass
As you can see, the client doesn't have to know that a factory is
involved in the creation of a specific object. It just calls whatever
the method needed to.
One discussion I had with another developper is the fact that the
MSDN's example is also extending this sample a bit further by reading
command line args that might have been passed to instanciate the
appropriate factory then pass it from the main class. This can be
easilly done by using generics. I was just wondering if there were any
benefits from doing this that way.
Thanks,
Steph