当前位置:
首页
文章
移动端
详情

设计模式-策略模式

下面有两个源代码,但是输出是一样的。第一个源代码使用 if 语句来选择逻辑。第二个源代码使用策略设计模式(重构结果)。

我们先讨论第一个源代码。在比萨课上,烹饪比萨有四个步骤。

  1. 收集材料

2.准备面团

  1. 设置 Variety,选择披萨口味(在本例中,我们有三个选项:capricciosa、Hawaiian 和 margheerita)。

4.加热和享受

在第一个源代码中,Set Variety 将使用 IF 逻辑来选择要使用的品种。如果要添加其他品种,那么我们必须更改 set_variety 方法中的代码并添加另一个 if (这不是推荐的设计)。

第二个源代码使用 Pizza 类的厨师方法作为算法家族(第 1 点:定义算法家族)。我们在每个类中封装了每个品种的披萨(第2点:封装了每个算法),当我们在cook方法中调用步骤3(设置品种)时,我们让每个附加到它的对象执行set_variety。如果要添加其他品种,那么我们只需要使用 set_variety 方法创建一个新类。好处是我们不必更改现有 Pizza 类中的代码。

未使用策略模式

class Pizza
  attr_reader :variety

  def initialize(variety: )
    @variety = variety
  end

  def cook
    gather_ingredients
    prepare_the_dough
    set_variety(variety: @variety)
    heat_and_enjoy
  end

  def gather_ingredients
    puts 'Step 1: Heat the souse while Chopping up ingredients'
  end

  def prepare_the_dough
    puts 'Step 2: Gently shape the dough'
  end

  def heat_and_enjoy
    puts 'Step 4: Heat to 425 for about 20-25 minutes'
    puts 'Enjoy the pizza'
    puts '----------------------------------------------------'    
  end

  def set_variety(variety: )
    puts 'Step 3: Set ingredients based variety'
    if variety == 'capricciosa'
      puts '  ham, mushrooms, artichokes, egg'
    elsif variety == 'hawaiian'
      puts '  ham, pineapple, bacon'
    elsif variety == 'margheerita'
      puts '  tomato, basil, mozzarella '
    end
  end
end

@pizza = Pizza.new(variety: 'capricciosa')
@pizza.cook
@pizza = Pizza.new(variety: 'hawaiian')
@pizza.cook

# program output
# Step 1: Heat the souse while Chopping up ingredients
# Step 2: Gently shape the dough
# Step 3: Set ingredients based variety
#   ham, mushrooms, artichokes, egg
# Step 4: Heat to 425 for about 20-25 minutes
# Enjoy the pizza
# ----------------------------------------------------
# Step 1: Heat the souse while Chopping up ingredients
# Step 2: Gently shape the dough
# Step 3: Set ingredients based variety
#   ham, pineapple, bacon
# Step 4: Heat to 425 for about 20-25 minutes
# Enjoy the pizza
# ----------------------------------------------------

使用策略模式

class Pizza
  attr_reader :variety

  def initialize(variety: )
    @variety = variety
  end

  def cook
    gather_ingredients
    prepare_the_dough
    puts 'Step 3: Set ingredients based variety'
    @variety.set_variety
    heat_and_enjoy
  end

  def gather_ingredients
    puts 'Step 1: Heat the souse while Chopping up ingredients'
  end

  def prepare_the_dough
    puts 'Step 2: Gently shape the dough'
  end

  def heat_and_enjoy
    puts 'Step 4: Heat to 425 for about 20-25 minutes'
    puts 'Enjoy the pizza'
    puts '----------------------------------------------------'
  end
end

class CapricciosaPizza
  def set_variety
    puts '  ham, mushrooms, artichokes, egg'
  end
end

class HawaiianPizza
  def set_variety
    puts '  ham, pineapple, bacon'
  end
end

class MargheeritaPizza
  def set_variety
    puts '  tomato, basil, mozzarella '
  end
end

@pizza = Pizza.new(variety: CapricciosaPizza.new)
@pizza.cook
@pizza = Pizza.new(variety: HawaiianPizza.new)
@pizza.cook

# program output
# Step 1: Heat the souse while Chopping up ingredients
# Step 2: Gently shape the dough
# Step 3: Set ingredients based variety
#   ham, mushrooms, artichokes, egg
# Step 4: Heat to 425 for about 20-25 minutes
# Enjoy the pizza
# ----------------------------------------------------
# Step 1: Heat the souse while Chopping up ingredients
# Step 2: Gently shape the dough
# Step 3: Set ingredients based variety
#   ham, pineapple, bacon
# Step 4: Heat to 425 for about 20-25 minutes
# Enjoy the pizza
# ----------------------------------------------------

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。