gpt4 book ai didi

java - 哪种设计模式使用策略或工厂设计?

转载 作者:行者123 更新时间:2023-11-29 04:10:16 24 4
gpt4 key购买 nike

我收到了家庭作业。我无法决定使用哪种设计模式。

我目前已经实现了策略模式。但是我的一些 friend 正在实现工厂设计模式。我只需要对其进行验证。

问题是:

Suppose you are working at a new startup where you have created a collection of specialized algorithms. Interface of each algorithm is well defined in terms of input and output of the algorithm. For example:

// Class: AlgorithmFn1
public java.util.List compute(java.util.Map<String, Float> data)

// Class: AlgorithmFn2
public java.util.Map<String, Integer> compute(java.util.Collection data)

and so on…

The startup plans to sell these specialized algorithms as libraries (.jar file) to its client organizations for use in their software applications. Internal implementation of these algorithms keep evolving without affecting the interfaces of algorithms.

You need to implement suitable set of classes (in Java) which can be used by the client organizations to:

Obtain new instances of concrete classes that implement the respective algorithms.

Shield the client’s code from the future changes in your implementations of the algorithms. That is, you need to design suitable classes which will exploit the object oriented programming principles for creating families of related objects without specifying the names of concrete classes of such objects.

最佳答案

策略是在运行时使用相同的 API 在多个不同的算法之间进行选择,但这个分配听起来不像那样。有几件事为 Factory 辩护:

  • 算法的API不同

  • 这个

    Obtain new instances of concrete classes that implement the respective algorithms.

  • 还有这个:

    That is, you need to design suitable classes which will exploit the object oriented programming principles for creating families of related objects without specifying the names of concrete classes of such objects.

是的,每个算法都有一个公共(public)接口(interface)的工厂,每个算法都有一个非公共(public)具体类,每个算法都有一个公共(public)工厂。 (在实现方面有多种方法可以解决这个问题,但从概念上讲,这就是您要寻找的方法。)

关于java - 哪种设计模式使用策略或工厂设计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55459921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com