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 the Product. It is an abstract class that defines the Draw() method. Concrete subclasses of Shape must implement the Draw() method.
  • The Circle and Square classes are the ConcreteProducts. They are concrete subclasses of Shape that implement the Draw() method.
  • The ShapeFactory class is the Creator. It is an abstract class that defines the CreateShape() method. Concrete subclasses of ShapeFactory must override the CreateShape() method to create a specific type of ConcreteProduct object.
  • The CircleFactory and SquareFactory classes are the ConcreteCreators. They are concrete subclasses of ShapeFactory that override the CreateShape() method to create Circle and Square objects, respectively.
  • The Main() function is the Client. It creates an instance of the CircleFactory class and calls the CreateShape() method to create a Circle object. The Circle 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. The new 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    
posted @   ChuckLu  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用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 生成证书
点击右上角即可分享
微信分享提示