我想知道是否可以将抽象工厂用作策略,例如嵌套两种模式并将工厂类也称为策略。
我提供了一个例子来说明我的问题。该类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 {}