![](/img/trans.png)
[英]I am studying the abstract factory pattern, is it good practice to have an abstract factory for each type of class?
[英]Is this a good example for representing the abstract factory pattern
想檢查這是否是表示抽象工廠模式的好例子。 這是主題戴爾(工廠)制造xps(產品)戴爾(工廠)制造靈感(產品)惠普(工廠)使特使(產品)惠普(工廠)制造presario(產品)
BestBuy銷售電腦。
//Abstract factory
abstract class ComputerFactory
{
public abstract Computer BuildComputer(Computer.ComputerType compType);
}
//Concrete factory
class Dell : ComputerFactory
{
public override Computer BuildComputer(Computer.ComputerType compType)
{
if (compType == Computer.ComputerType.xps)
return (new xps());
else if (compType == Computer.ComputerType.inspiron)
return new inspiron();
else
return null;
}
}
//Concrete factory
class Hp : ComputerFactory
{
public override Computer BuildComputer(Computer.ComputerType compType)
{
if (compType == Computer.ComputerType.envoy)
return (new envoy());
else if (compType == Computer.ComputerType.presario)
return new presario();
else
return null;
}
}
//Abstract product
public abstract class Computer
{
public abstract string Mhz { get; set; }
public enum ComputerType
{
xps,
inspiron,
envoy,
presario
}
}
//Concrete product for DELL
public class xps : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
//Concrete product for DELL
public class inspiron : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
//Concrete product for HP
public class envoy : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
//Concrete product for HP
public class presario : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
public class BestBuy
{
ComputerFactory compFactory;
Computer comp;
public BestBuy(Computer.ComputerType compType)
{
if (compType == Computer.ComputerType.xps || compType == Computer.ComputerType.inspiron)
compFactory = new Dell();
else
compFactory = new Hp();
comp = compFactory.BuildComputer(compType);
}
public Computer Sell()
{
return comp;
}
}
提前致謝。
這是模式部分的一個很好的例子。 對象的基本構造是一個不錯的例子,但邏輯依賴於單個Computer.ComputerType
枚舉。 這個枚舉需要事先知道每個工廠暴露的每種類型的計算機。
通常,使用抽象工廠的動機是從圖片中抽象出這種類型的硬編碼要求。 最好不要使用單個枚舉,而是添加ComputerType
類,並允許工廠返回可用類型的集合。 然后,您可以使用返回的ComputerType
來構建新系統。
這允許您在不更改API的情況下添加其他工廠,這是抽象工廠模式的主要優點之一。 閱讀抽象工廠模式 - 其中一個要點是:
客戶端不知道(或關心)從每個內部工廠獲得哪些具體對象,因為它僅使用其產品的通用接口。
在這種情況下,您將已知類型“硬編碼”到枚舉中,這違反了模式的這一部分。
我不是工廠模式專家,但這里有幾件我會做的事情不同:
我認為,在您提供的場景和代碼中,只有一種類型的產品,即“計算機”。 沒有涉及的產品系列。 因此,抽象工廠模式不適用於此處。 相反,工廠模式可以在這里使用。 我修改了下面的代碼以便理解。
//Abstract factory
abstract class ComputerFactory
{
public abstract Computer BuildComputer(Computer.ComputerType compType);
}
public class ConcreteFactory : ComputerFactory
{
public override Computer BuildComputer(Computer.ComputerType compType)
{
if (compType == Computer.ComputerType.xps)
return (new xps());
else if (compType == Computer.ComputerType.inspiron)
return new inspiron();
else if (compType == Computer.ComputerType.envoy)
return (new envoy());
else if (compType == Computer.ComputerType.presario)
return new presario();
else
return null;
}
}
//Abstract product
public abstract class Computer
{
public abstract string Mhz { get; set; }
public enum ComputerType
{
xps,
inspiron,
envoy,
presario
}
}
//Concrete product for DELL
public class xps : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
//Concrete product for DELL
public class inspiron : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
//Concrete product for HP
public class envoy : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
//Concrete product for HP
public class presario : Computer
{
string _mhz = string.Empty;
public override string Mhz
{
get
{
return _mhz;
}
set
{
_mhz = value;
}
}
}
public class BestBuy
{
ConcreteFactory compFactory;
Computer comp;
public BestBuy(Computer.ComputerType compType)
{
comp = compFactory.BuildComputer(compType);
}
public Computer Sell()
{
return comp;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.