对设计模式—Abstract Factory模式的认识
现在公司搬家了,吃饭都成了问题,每次得跑到外面去吃,走到一个小吃店都不知道吃什么,只好点了个盖交饭.其实厨房就相当与Factory ,你想吃什么就给你炒什么.
public interface Vegetables{ public void cooking();}public class MeatVegetables implements Vegetables{ public void cooking() { System.out.println("Cooking Meat and Vegetables"); }}public class PastaVegetables implements Vegetables{ public void cooking() { System.out.println("Cooking Pasta and Vegetables"); }}public interface Noodle{ public void cooking();}public class MeatNoodle implements Noodle{ public void cooking() { System.out.println("Meat and Noodle"); }}public class VegetablesNoodle implements Noodle{ public void cooking() { System.out.println("Vegetables and Noodle"); } } public interface Buyer{ // public Vegetables buyVegetables(Vegetables whichVegetables); public Noodle buyNoodle(Noodle whichNoodle);}public class WageWorker implements Buyer{ public Vegetables buyVegetables(Vegetables whichVegetables) { return whichVegetables; } public Noodle buyNoodle(Noodle whichNoodle) { return whichNoodle; }}public class TestVegetables{ public static void main(String[]args) { //MeatVegetables Instance Vegetables meatVegetables = new MeatVegetables(); //PastaVegetables Instance Vegetables pastaVegetables = new PastaVegetables(); //MeatNoodle Instance Noodle meatNoodle = new MeatNoodle(); //VegetablesNoodle Instance Noodle vegetablesNoodle = new VegetablesNoodle(); //Custom Instance Buyer custom = new WageWorker(); //Choice meatVegetables custom.buyVegetables(meatVegetables ).cooking(); //Choice pastaVegetables custom.buyVegetables(pastaVegetables).cooking(); //Choice meatNoodle custom.buyNoodle(meatNoodle).cooking(); //Choice vegetablesNoodle custom.buyNoodle(vegetablesNoodle).cooking(); }}
抽象工厂:
1.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,创建多个产品族中的产品对象。这就是抽象工厂模式的用意。
2.抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态
3.我们还可以动态扩张其他类