dofactory C# Factory Method
The classes and objects participating in this pattern include:
- Product (
Page)- defines the interface of objects the factory method creates
- ConcreteProduct (
SkillsPage, EducationPage, ExperiencePage)- implements the Product interface
- Creator (
Document)- declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
- may call the factory method to create a Product object.
- ConcreteCreator (
Report, Resume)- overrides the factory method to return an instance of a ConcreteProduct.
abstract class Shape {
public abstract void Draw();
}
class Circle : Shape {
public override void Draw() {
Console.WriteLine("Drawing a circle");
}
}
class Square : Shape {
public override void Draw() {
Console.WriteLine("Drawing a square");
}
}
abstract class ShapeFactory {
public abstract Shape CreateShape();
}
class CircleFactory : ShapeFactory {
public override Shape CreateShape() {
return new Circle();
}
}
class SquareFactory : ShapeFactory {
public override Shape CreateShape() {
return new Square();
}
}
class Program {
public static void Main(string[] args) {
ShapeFactory shapeFactory = new CircleFactory();
Shape shape = shapeFactory.CreateShape();
shape.Draw();
}
}
The participants in the code are labeled as follows:
- The
Shapeclass is theProduct. It is an abstract class that defines theDraw()method. Concrete subclasses ofShapemust implement theDraw()method. - The
CircleandSquareclasses are theConcreteProducts. They are concrete subclasses ofShapethat implement theDraw()method. - The
ShapeFactoryclass is theCreator. It is an abstract class that defines theCreateShape()method. Concrete subclasses ofShapeFactorymust override theCreateShape()method to create a specific type ofConcreteProductobject. - The
CircleFactoryandSquareFactoryclasses are theConcreteCreators. They are concrete subclasses ofShapeFactorythat override theCreateShape()method to createCircleandSquareobjects, respectively. - The
Main()function is theClient. It creates an instance of theCircleFactoryclass and calls theCreateShape()method to create aCircleobject. TheCircleobject is then drawn.
Relations with Other Patterns refactoring guru
-
Many designs start by using Factory Method (less complicated and more customizable via subclasses) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, but more complicated).
-
Abstract Factory classes are often based on a set of Factory Methods, but you can also use Prototype to compose the methods on these classes.
-
You can use Factory Method along with Iterator to let collection subclasses return different types of iterators that are compatible with the collections.
-
Prototype isn’t based on inheritance, so it doesn’t have its drawbacks. On the other hand, Prototype requires a complicated initialization of the cloned object. Factory Method is based on inheritance but doesn’t require an initialization step.
-
Factory Method is a specialization of Template Method. At the same time, a Factory Method may serve as a step in a large Template Method.
Rules of thumb source making
- Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.
- Factory Methods are usually called within Template Methods.
- Factory Method: creation through inheritance. Prototype: creation through delegation.
- Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
- Prototype doesn't require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn't require Initialize.
- The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.
- Some Factory Method advocates recommend that as a matter of language design (or failing that, as a matter of style) absolutely all constructors should be private or protected. It's no one else's business whether a class manufactures a new object or recycles an old one.
- The
newoperator considered harmful. There is a difference between requesting an object and creating one. Thenewoperator always creates an object, and fails to encapsulate object creation. A Factory Method enforces that encapsulation, and allows an object to be requested without inextricable coupling to the act of creation.

浙公网安备 33010602011771号