工厂方法与抽象工厂设计模式有什么区别? [英] What is the difference between factory method and abstract factory design patterns?

查看:135
本文介绍了工厂方法与抽象工厂设计模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工厂方法与抽象工厂设计模式有什么区别?我对这两者感到困惑。抽象工厂是否使用工厂方法来实现?

What is the difference between factory method and abstract factory design patterns ?I am confused about both . Does abstract factory use factory method to implement itself?

请提供一些有形的例子。

Please give some tangible examples .

推荐答案

来自于对象实例化是重要的事情,不应该分散代码,而应该从集中位置进行更多的控制;

Factory Pattern in general comes from the thought that Object instantiation is an important thing and it should not be scattered accross code but should be done from a Centralised Location to have more control; it is neater as well.

工厂方法只是服务于上述目的

     ControlFactory
 {
  public Button GetButton(){return new Button();}
  public Label GetLabel(){return new Label();}
  public TextBox GetTextBox(){return new TextBox();}
 }

然后为了实例化这些控件,你将被写入代码

Then for instantiating these controls you will be writng below code

ControlFactory cf=new ControlFactory();

Button b=cf.GetButton();b.Text="Click Me";
Label l=cf.GetLabel();

抽象工厂另一方面有助于处理对象系列。
例如:如果您为免费和付费用户计划了不同版本的软件,您可以选择使用普通的免费版控件和一些美观的付费版本控件。这可以用抽象工厂模式优雅处理。

Abstract Factory on the other hand is helpful to deal with object families. Eg: If you plan different versions of your software for free and paid users you may choose to use ordinary controls for Free version and some aesthetically better controls for the paid version. This can be handled elegantly with Abstract Factory Pattern.

 abstract class ControlsFactory
 {
    public abstract Button GetButton();
    public abstract Label GetLabel();
 }

---现在FreeControlFactory和ExoticControlFactory继承自上述类

---now FreeControlFactory and ExoticControlFactory inherits from above class

 class FreeControlsFactory:ControlsFactory
 {


  public override Button GetButton()
  {
      return new FreeButton();//Assume that **FreeControl** and **ExoticControl** are Inherited From **Control** 
  }

  public override Label GetLabel()
  {
      return new FreeLabel();
  }

 }

...

 class ExoticControlsFactory : ControlsFactory
 {


  public override Button GetButton()
  {
   return new ExoticButton();
  }

  public override Label GetLabel()
  {
   return new ExoticLabel();
  }

 }

假设 FreeControl ExoticControl 继承自控制(我不是在这里编写代码)

Assume that FreeControl and ExoticControl are Inherited From Control (I am not writing the code here)

现在,您将下面的代码将所有控件切换到另一个版本

Now you will be writing below code to switch all the controls to another version

if (VersionKey=="FreeVersion")
    {ControlFactory cf= new FreeControlFactory();}
ese if (VersionKey=="PaidVersion")
    {cf=new ExoticControlFactory();}






基于上述选择所有的地方控制将被切换


based on above selection all the places Controls will be switched

Button b1=cf.GetButton();//b1 will be based on which family cf is containing
Button b2=cf.GetButton();
Button b3=cf.GetButton();
Label l1= cf.GetLabel();
Label l2= cf.GetLabel();

这是一个可以帮助的多态行为,即cf可以包含freefactory或exoticfacctory。并决定创建哪些控件。

It is the polymorphic behaviour that helps here , ie cf can contain either freefactory or exoticfacctory. and that decides which controls are created.

这篇关于工厂方法与抽象工厂设计模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆