【发布时间】:2012-07-11 07:45:58
【问题描述】:
我想知道是否可以使用抽象工厂作为策略,例如嵌套这两种模式并将工厂类称为策略。
我提供了一个例子来说明我的问题。 ShoppingMall 类将是上下文类,而 PizzaStore 将是有问题的抽象工厂,我认为在这种情况下也是一种策略。
// interface of context class, mostly a wrapper
// uses 3 different strategies
interface ShoppingMall
{
PizzaStore GetPizzaStore();
ParkingLot GetParkingLot();
Adverts GetAdverts();
void CustomerArrives();
}
// abstract factory & strategy interface?
interface PizzaStore
{
Pizza CreatePizzaCheese();
Pizza CreatePizzaVeggie();
Pizza CreatePizzaClam();
Pizza CreatePizzaPepperoni();
}
// strategy interface
interface ParkingLot
{
void Pay();
}
// strategy interface
interface Adverts
{
void CreateSpam();
}
class PizzaStoreChicago implements PizzaStore {}
class PizzaStoreNY implements PizzaStore {}
class PizzaStoreObjectVille implements PizzaStore {}
class ParkingLotBig implements ParkingLot {}
class ParkingLotSmall implements ParkingLot {}
class ParkingLotCheap implements ParkingLot {}
class AdvertsAnnoying implements Adverts {}
class AdvertsBoring implements Adverts {}
class AdvertsStupid implements Adverts {}
class ShoppingMallObjectVille implements ShoppingMall {}
class ShoppingMallJavaRanch implements ShoppingMall {}
class ShoppingMallAverage implements ShoppingMall {}
class ShoppingMallOther implements ShoppingMall {}
【问题讨论】:
-
如果它解决了你需要解决的问题,那就去吧。模式只是常见问题的常见解决方案的名称。您可以根据需要使用它们来解决问题。
-
这与它是否解决了问题(确实如此)无关。我想知道在这种情况下,策略和抽象工厂模式的术语是否可以应用于同一个类。
标签: java oop design-patterns factory-pattern strategy-pattern