工厂模式和策略模式的区别

打印 上一主题 下一主题

主题 562|帖子 562|积分 1686


一、前言

策略模式和工厂模式是两种不同的设计模式,它们有不同的目标和使用场景。
二、工厂模式和策略模式的区别

2.1 工厂模式

工厂模式提供了一种创建对象的机制,通过工厂方法来根据给定的参数返回一个对象。这种模式可以制止直接使用new操作符来创建对象,提高了代码的灵活性和可扩展性。
工厂模式适合:凡是出现了大量的产物须要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建。
实现示例
以平凡工厂模式为例
创建一个工厂类,对实现了同一接口的一些类进行实例的创建。

举例如下:(我们举一个发送邮件和短信的例子)
首先,创建二者的共同接口:
  1. public interface Sender {
  2. public void Send();
  3. }
复制代码
其次,创建实现类:
  1. public class MailSender implements Sender {
  2. @Override
  3. public void Send() {
  4.   System.out.println("this is mailsender!");
  5. }
  6. }
复制代码
  1. public class SmsSender implements Sender {
  2. @Override
  3. public void Send() {
  4.   System.out.println("this is sms sender!");
  5. }
  6. }
复制代码
2.2 策略模式

策略模式界说了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户。须要设计一个接口,为一系列实现类提供同一的方法,多个实现类实现该接口,设计一个抽象类(可有可无,属于辅助类),提供辅助函数,关系图如下:

策略模式的决定权在用户,体系本身提供不同算法的实现,新增大概删除算法,对各种算法做封装。因此,策略模式多用在算法决策体系中,外部用户只须要决定用哪个算法即可。
实现示例
首先同一接口:
  1. public interface ICalculator {
  2. public int calculate(String exp);
  3. }
复制代码
辅助类:
  1. public abstract class AbstractCalculator {
  2.   
  3. public int[] split(String exp,String opt){
  4.   String array[] = exp.split(opt);
  5.   int arrayInt[] = new int[2];
  6.   arrayInt[0] = Integer.parseInt(array[0]);
  7.   arrayInt[1] = Integer.parseInt(array[1]);
  8.   return arrayInt;
  9. }
  10. }
复制代码
三个实现类:
  1. public class Plus extends AbstractCalculator implements ICalculator {
  2. @Override
  3. public int calculate(String exp) {
  4.   int arrayInt[] = split(exp,"\\+");
  5.   return arrayInt[0]+arrayInt[1];
  6. }
  7. }
复制代码
  1. public class Minus extends AbstractCalculator implements ICalculator {
  2. @Override
  3. public int calculate(String exp) {
  4.   int arrayInt[] = split(exp,"-");
  5.   return arrayInt[0]-arrayInt[1];
  6. }
  7. }
复制代码
  1. public class Multiply extends AbstractCalculator implements ICalculator {
  2. @Override
  3. public int calculate(String exp) {
  4.   int arrayInt[] = split(exp,"\\*");
  5.   return arrayInt[0]*arrayInt[1];
  6. }
  7. }
复制代码
简单的测试类:
  1. public class StrategyTest {
  2. public static void main(String[] args) {
  3.   String exp = "2+8";
  4.   ICalculator cal = new Plus();
  5.   int result = cal.calculate(exp);
  6.   System.out.println(result);
  7. }
  8. }
复制代码
输出:10
2.3 区别

关注点不同
策略模式关注的是算法的替换和变化,它夸大的是举动的选择和执行。
工厂模式关注的是对象的创建,它夸大的是实例化过程的控制和管理。
目标不同
策略模式通过界说一组算法来实现举动的可替换性和灵活性。
工厂模式通过提供创建对象的接口来实现对象创建过程的解耦和灵活性。
实现方式不同
策略模式包罗策略接口和详细策略类,通过上下文类来使用不同的策略。
工厂模式包罗工厂接口或抽象类和详细工厂类,通过工厂方法来创建对象。
应用场景不同
策略模式适用于须要在运行时动态选择算法或举动的场景。
工厂模式适用于须要控制实例化过程,大概须要创建一系列相干对象的场景。
2.4 结合

结合使用策略模式和工厂模式,可以使体系更灵活。工厂模式用于创建详细的策略对象,而策略模式用于在运行时选择和执行这些策略。
示例
下面的示例展示了如何结合使用策略模式和工厂模式。工厂模式负责创建策略对象,策略模式负责在运行时选择和执行策略。
策略接口
  1. // IStrategy.cs
  2. public interface IStrategy
  3. {
  4.     void Execute();
  5. }
复制代码
详细策略
  1. // ConcreteStrategyA.cs
  2. public class ConcreteStrategyA : IStrategy
  3. {
  4.     public void Execute()
  5.     {
  6.         Console.WriteLine("执行策略 A");
  7.     }
  8. }
  9. // ConcreteStrategyB.cs
  10. public class ConcreteStrategyB : IStrategy
  11. {
  12.     public void Execute()
  13.     {
  14.         Console.WriteLine("执行策略 B");
  15.     }
  16. }
复制代码
策略工厂
  1. // StrategyFactory.cs
  2. public class StrategyFactory
  3. {
  4.     public IStrategy CreateStrategy(string strategyType)
  5.     {
  6.         return strategyType switch
  7.         {
  8.             "A" => new ConcreteStrategyA(),
  9.             "B" => new ConcreteStrategyB(),
  10.             _ => throw new ArgumentException("Invalid strategy type")
  11.         };
  12.     }
  13. }
复制代码
上下文类
  1. // Context.cs
  2. public class Context
  3. {
  4.     private IStrategy _strategy;
  5.     public void SetStrategy(IStrategy strategy)
  6.     {
  7.         _strategy = strategy;
  8.     }
  9.     public void ExecuteStrategy()
  10.     {
  11.         _strategy.Execute();
  12.     }
  13. }
复制代码
客户端代码
  1. // Program.cs
  2. using System;
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         StrategyFactory factory = new StrategyFactory();
  8.         Context context = new Context();
  9.         // 选择策略类型,可以从配置文件、用户输入等获取
  10.         string strategyType = "A";
  11.         
  12.         // 使用工厂创建策略
  13.         IStrategy strategy = factory.CreateStrategy(strategyType);
  14.         // 设置上下文策略并执行
  15.         context.SetStrategy(strategy);
  16.         context.ExecuteStrategy();
  17.         // 切换到另一种策略
  18.         strategyType = "B";
  19.         strategy = factory.CreateStrategy(strategyType);
  20.         context.SetStrategy(strategy);
  21.         context.ExecuteStrategy();
  22.     }
  23. }
复制代码
结合使用的好处
1、职责分离
工厂模式负责创建详细的策略对象。
策略模式负责在运行时选择和执行策略。
2、扩展性强
可以很轻易地添加新的策略,而不须要修改上下文类和客户端代码,只需在工厂中增长对应的策略创建逻辑即可。
3、代码简便
客户端代码不须要相识策略的详细实现,只需通过工厂获取策略对象,并在上下文中使用即可。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

去皮卡多

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表