posted 3/7/2011 by Bevans - Views: [449]
If you haven’t checked out my very basic intro to .net you may want to take a look here.
The Question
One of the questions that was asked was about, assembles and factories. While I did do spend a great deal of time explaining what assembles are I did mention anything about factories. First of the two items really have nothing in common. An assemble is a compiled library of code, and a factory is a creational pattern which make build something complex in code easier and located in one easy to find, and change place. Let’s dig in it a bit.
What’s a Factory?
As I mentioned earlier a factory is a pattern, a repeatable idea. The factory pattern comes in two major flavors the Abstract Factory, and the Factory Method. Today we are going to discuss the factory method. The factory method is a method.
Method refresher…
Public NewMagicBus BuildMagicBus(string OwnerName, Int Year) { //Magic Code Return MagicBus; }
So public means? It means anyone can call it. We call it our scope attribute. Second we our telling the compiler that we want this to give us a “MagicBus” in honor of VW releasing them again next year. 3rd our fancy name we want to call it and its prams.
So this bit of code will be the foundation of our simple bus. Let’s see the whole code now and talk about it.
Public NewMagicBus BuildMagicBus(string OwnerName, Int Year) { MagicBus newBus = new MagicBus(); newBus.wheels = new firestonewheels(); newBus.SteeringWheel = new FuzzyWheel(); Return MagicBus; }
So a factory is a creational pattern that creates thing like.. well a factory. It primary strength is the ability to quickly adjust its subclasses. Such as creating firestone wheels for our bus or creating a snazzy fuzzy wheel. Our bus will always have a steering wheel and wheels. But what kind may change based on how we feel that morning, our caffeine level or other inherent factors like business rules.
Great Pattern Refrence Site
Starting Point