哪种设计模式与工厂模式相反?

发布于 2024-08-04 14:03:38 字数 182 浏览 5 评论 0原文

我想知道是否有与工厂模式相反的模式。例如,当需要删除某个对象时,需要完成一些额外的工作,以撤消在工厂对象中执行的配置。

例如,使用删除方法扩展工厂对象似乎是错误的,因为工厂模式是严格的创建模式。

更新:我使用工厂的原因是因为需要完成的配置会引入一些不适合的对象的依赖关系。将这种取消配置放入构造函数中会带来同样的问题。

I was wondering if there is an opposite pattern of the factory pattern. For example, when a certain object needs to be deleted some extra work needs to be done, to undo the configuration which was performed in the factory object.

Extending the factory object with a Delete method for instance seems wrong, since the factory pattern is a strict creational pattern.

Update: The reason why I'm using a factory is because the configuration which needs to be done would introduce some dependencies to the object which wouldn't fit. Putting this de-configuration in the constructor would pose the same problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开柳相依 2024-08-11 14:03:39

我使用“回收设施”模式与工厂协同工作:

  • 为每个可以回收的类提供一个“干净”的方法,
  • 实例提供一个“唯一的 ID”。

为每个对象 将其发送到“回收设施”(RF):

  • RF 根据某种策略存储对象(例如,仅保留类 Y 的 X 个实例)。
  • 当需要类 Y 的实例时,工厂“询问”RF 是否需要该对象。有一个
    • 如果 RF 方便的话,RF 会调用实例上的“clean()”方法并将其返回到工厂

......等等。

希望这有帮助。

I use a "Recycling Facility" pattern working in tandem with the Factory:

  • have a "clean" method for each class that can be recycled
  • have a "unique id" for each object instance

Each time an object reaches its end-of-life, send it to the "Recycling Facility" (RF):

  • The RF stores the object according to some policy ( e.g. keep only X instances of class Y )
  • When a an instance of class Y is required, the Factory "asks" the RF if its got one
    • if the RF has one handy, the RF calls the "clean()" method on the instance and returns it to the Factory

... and so on so forth.

Hope this helps.

绿光 2024-08-11 14:03:39

我有同样的问题,我对现有的答案不满意,而且我不喜欢“回收”(我不重复使用它)或“DeathKeeper”(听起来轻松)这些术语。存储库很好,但有时有点过分了。它甚至可能不是您应用程序的重要组成部分,而更像是 GDPR 功能。

工厂模式没有完全相反的模式,我同意这个名称意味着创建性,并且单一责任规则反对向其添加删除功能。

我决定创建一个像 AccountDisposer 这样的 Disposer:

public interface IAccount
{
    int Id { get; }
    void Dispose(AccountDisposer accountDisposer);
    // Other properties and methods
}

public interface IAccountDisposer {
    void DisposeAccount(AccountSet account);
}

public class AccountSet : IAccount
{
    public int Id { get; set; }

    //TODO: move into AccountFactory
    public static void Create(int id) {
        var accountSet = new AccountSet { Id = id };
    }

    public void Dispose(AccountDisposer accountDisposer)
    {
        accountDisposer.DisposeAccount(this);
    }
}

public class AccountDisposer : IAccountDisposer
{
    private readonly MyDbContext _dbContext;

    public AccountDisposer(MyDbContext dbContext) {
        _dbContext = dbContext;
    }

    public void DisposeAccount(AccountSet account)
    {
        _dbContext.AccountSet.Remove(account); // <- this matters
    }
}

请注意,我没有使用 IDisposable,因为我需要一个参数。如果您知道更聪明的方法或有其他反馈,请在评论中告诉我。 AccountSet 中的 DisposeAccount() 方法通常是可选的。

I had the same question and I am not happy with the existing answers and I don't like the terms Recycling (I do not reuse it) or DeathKeeper (sounds lighthearted). Repository is nice but sometimes overkill. It might not even be an essential part of your application and more of a GDPR feature.

There is no exact opposite pattern of the factory pattern and I would agree that the name implies creational and the single responsibility rule would speak against adding delete functionality to it.

I decided on creating a Disposer like an AccountDisposer:

public interface IAccount
{
    int Id { get; }
    void Dispose(AccountDisposer accountDisposer);
    // Other properties and methods
}

public interface IAccountDisposer {
    void DisposeAccount(AccountSet account);
}

public class AccountSet : IAccount
{
    public int Id { get; set; }

    //TODO: move into AccountFactory
    public static void Create(int id) {
        var accountSet = new AccountSet { Id = id };
    }

    public void Dispose(AccountDisposer accountDisposer)
    {
        accountDisposer.DisposeAccount(this);
    }
}

public class AccountDisposer : IAccountDisposer
{
    private readonly MyDbContext _dbContext;

    public AccountDisposer(MyDbContext dbContext) {
        _dbContext = dbContext;
    }

    public void DisposeAccount(AccountSet account)
    {
        _dbContext.AccountSet.Remove(account); // <- this matters
    }
}

Notice that I did not use IDisposable because I need a parameter. If you know a smarter way or have other feedback tell me in the comments. Having DisposeAccount() method in AccountSet is generally optional.

淑女气质 2024-08-11 14:03:38

存储库可用于删除持久化对象,或者您可以使用 dispose 方法对仅内存中的对象进行一些清理。

A repository could be used to delete a persisted object, or you could use the dispose method to do some cleanup on an in memory only object.

脸赞 2024-08-11 14:03:38

这就是使用工厂的正确方法。工厂不仅是创建对象的方式,也是表达:我需要为这些对象进行特殊初始化的方式。对于您的问题,我认为最好的解决方案是通知工厂一些事件,例如处置。因此,您的对象创建将以这样的方式完成:创建、订阅工厂到新创建对象的事件。每次删除对象时,您都会通知工厂并执行您需要的操作。

如果您不喜欢将其放入工厂,您可以将其委托给某种其他对象,例如 DeathKeeper ;-)。所以你的代码看起来像这样:

//Inside factory create method
MyObject obj = GetNewInitializedObject();
_deathKeeper.RegisterObject(obj);

return obj;

现在每次你需要删除对象时,你的对象都会通知死亡守护者,它会执行所有处理逻辑。顺便说一句,我不知道它是如何工作的,但是您可以使用 IDisposable 接口来执行自定义逻辑来处理对象持有的资源。该决定取决于您的项目中的内容并且由您决定。

That's right way to use factory. Factory is not only way to create objects, but also the way to say: I need a special initialization for these kind of objects. With your problem I think the best solution would be to notify factory with some event, like disposed. So your object creation will be done in such a way: create, subscribe factory to event of newly created object. Every time object is deleted you`ll notify factory on that and perform action you need.

If you don't like to put this into factory, you can delegate it to some kind of other object, like DeathKeeper ;-). So your code will look sometihng like this:

//Inside factory create method
MyObject obj = GetNewInitializedObject();
_deathKeeper.RegisterObject(obj);

return obj;

Now every time you will need to delete object, your object will notify death keeper and it would make all dispose logic. By the way, I do not know how it all works, but you can use IDisposable interface to do the custom logic for disposing resources held by object. The decision depends on what`s there in your project and is up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文