dongqiuqiu4736 2012-04-13 10:21
浏览 22
已采纳

为什么不进行工厂模式排序? [关闭]

At run time, based on user behavior and history, I need to perform a sorting operation. In my case, SortByDate/SortByDemand/SortByConsumption will just return the string, or we can say order by clause(which can be complex).

In most of the forums, I have found Strategy pattern should be used for sorting.

enter image description here

I have attached the image for Strategy pattern here. Util class will call the object of one the three classes i.e SortByDate/SortByDemand/SortByConsumption

So every time a new method of sorting is defined, I need to change the util class and define a new Strategy.

enter image description here

If however I implemented it using factory, the util class just need to call the factory and it will take care of which class to call. So I think I should use factory.

However I have read that strategy is best pattern for such needs. Why is strategy pattern is better here?

  • 写回答

4条回答 默认 最新

  • douzhi4991 2012-04-13 18:37
    关注

    Strategy is a pattern aimed at allowing you to add new (in your case sort) algorithms to your software without breaking the clients of the algorithms. It's an investment in design complexity that will pay off if you need to add new algorithms without breaking your clients. Factory is a pattern that complements Strategy because the clients of the algorithm implementations should not know specifically which implementation they're using (in terms of software classes). The factory instantiates the concrete implementations of the algorithm so the client can use them without knowing the details.

    Here's the static structure: enter image description here

    Here's the dynamic:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作