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
Shape
class is theProduct
. It is an abstract class that defines theDraw()
method. Concrete subclasses ofShape
must implement theDraw()
method. - The
Circle
andSquare
classes are theConcreteProduct
s. They are concrete subclasses ofShape
that implement theDraw()
method. - The
ShapeFactory
class is theCreator
. It is an abstract class that defines theCreateShape()
method. Concrete subclasses ofShapeFactory
must override theCreateShape()
method to create a specific type ofConcreteProduct
object. - The
CircleFactory
andSquareFactory
classes are theConcreteCreator
s. They are concrete subclasses ofShapeFactory
that override theCreateShape()
method to createCircle
andSquare
objects, respectively. - The
Main()
function is theClient
. It creates an instance of theCircleFactory
class and calls theCreateShape()
method to create aCircle
object. TheCircle
object 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
new
operator considered harmful. There is a difference between requesting an object and creating one. Thenew
operator 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.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-09-07 在线求极限
2022-09-07 Is there a quick way to find all columns in SQL Server 2008 R2 that are encrypted/have encrypted data?
2022-09-07 幸运的人一生都被童年治愈;不幸的人一生都在治愈童年。
2021-09-07 Update to enable TLS 1.1 and TLS 1.2 as default secure protocols in WinHTTP in Windows
2021-09-07 How to check if TLS 1.2 is enabled?
2021-09-07 How to Activate TLS 1.2 on Windows Server 2008 R2 and IIS 7.5
2021-09-07 生成证书