设计模式之工厂模式

                                                                    工厂模式

(1)工厂方法模式

提供专用接口,用来创建对象,允许子类决定实例化哪个子类

超类规定标准和通用的行为,业务细节由子类实现,超类可以是接口,抽象类、普通类

应用场景:当超类由多个子类实现的时候,根据输入生成相应的子类实例。

以下是代码示例

public interface Animal {
    void eat();
}
public class Bird implements Animal {
    @Override
    public void eat() {
        System.out.println("鸟吃虫子");
    }
}
public class Monkey implements Animal {
    @Override
    public void eat() {
        System.out.println("猴子吃桃子");
    }
}
public class Tiger implements Animal{
    @Override
    public void eat() {
        System.out.println("老虎喜欢吃肉");
    }
}
public class Sheep implements Animal {
    @Override
    public void eat() {
        System.out.println("羊吃草");
    }
}
 
public class FactoryMethodTest {
    public static void main(String[] args) {
        AnimalFactory af=new AnimalFactory();
        Animal animal=  af.getAnimal("sheep");
        animal.eat();
        Animal anima2=  af.getAnimal("bird");
        anima2.eat();
        Animal anima3=  af.getAnimal("tiger");
        anima3.eat();
        Animal animal4=  af.getAnimal("monkey");
        animal4.eat();
    }

}


 

(2)抽象工厂模式

通过一个接口创建一系列相关的对象,不指定具体的类,具体的类(由工厂完成)

 

抽象工厂可以看作是工厂中的工厂

public interface Animal {
    void eat();
}

public class Bird implements Animal {
    @Override
    public void eat() {
        System.out.println("鸟吃虫子");
    }
}
public class Monkey implements Animal {
    @Override
    public void eat() {
        System.out.println("猴子吃桃子");
    }
}
public class Sheep implements Animal {
    @Override
    public void eat() {
        System.out.println("羊吃草");
    }
}
public class Tiger implements Animal {
    @Override
    public void eat() {
        System.out.println("老虎喜欢吃肉");
    }
}
public interface Food {
    void food();
}
public class Grass implements Food{
    @Override
    public void food() {
        System.out.println("草");
    }
}
public class Meat implements Food {
    @Override
    public void food() {
        System.out.println("肉");
    }
}
public class Peach implements Food {
    @Override
    public void food() {
        System.out.println("桃子");
    }
}
public class Worm implements Food {
    @Override
    public void food() {
        System.out.println("虫子");
    }
}
public abstract class AbstractFactory {
  abstract Animal getAnimal(String animal);
    abstract Food getFood(String food);
}
package com.ddh.abstractfactory;

/**
 * Created by ddh on 2018/5/2.
 */
@SuppressWarnings("all")
public class AnimalFactory extends AbstractFactory {
    @Override
       public Animal getAnimal(String animalType) {
        if (animalType==null){
            return null;
        }
        if (animalType.equalsIgnoreCase("BIRD")){
            return new Bird();
        }else if (animalType.equalsIgnoreCase("MONKEY")){
            return new Monkey();
        }else if (animalType.equalsIgnoreCase("SHEEP")){
            return new Sheep();
        }else if (animalType.equalsIgnoreCase("TIGER")){
            return new Tiger();
        }
        return null;
    }

    @Override
    Food getFood(String food) {
        return null;
    }
}
package com.ddh.abstractfactory;

/**
 * Created by ddh on 2018/5/2.
 */
public class FoodFactory extends AbstractFactory {
    @Override
    Animal getAnimal(String animal) {
        return null;
    }

    @Override
   public Food getFood(String foodType) {
        if (foodType==null){
            return null;
        }
        if (foodType.equalsIgnoreCase("GRASS")){
            return new Grass();
        }else if (foodType.equalsIgnoreCase("MEAT")){
            return new Meat();
        }else if (foodType.equalsIgnoreCase("PEACH")){
            return new Peach();
        }else if (foodType.equalsIgnoreCase("WORM")){
            return new Worm();
        }
        return null;
    }
}
package com.ddh.abstractfactory;

/**
 * Created by ddh on 2018/5/2.
 */
public class FactoryProducer {
    public static AbstractFactory getFactory(String choice){
        if (choice.equalsIgnoreCase("FOOD")){
            return new FoodFactory();
        }else if(choice.equalsIgnoreCase("ANIMAL")){
            return new AnimalFactory();
        }
        return null;
    }
}
package com.ddh.abstractfactory;

/**
 * Created by ddh on 2018/5/2.
 */
public class AbstractFactoryPatternTest {
    public static void main(String[] args) {
        AbstractFactory animalFatory=FactoryProducer.getFactory("Animal");
        Animal animal1=animalFatory.getAnimal("tiger");
        animal1.eat();
        Animal animal2=animalFatory.getAnimal("bird");
        animal2.eat();
        Animal animal3=animalFatory.getAnimal("monkey");
        animal3.eat();
        Animal animal4=animalFatory.getAnimal("sheep");
        animal4.eat();
        AbstractFactory foodfactory=FactoryProducer.getFactory("Food");
        Food food1=foodfactory.getFood("grass");
        food1.food();
        Food food2=foodfactory.getFood("meat");
        food2.food();
        Food food3=foodfactory.getFood("worm");
        food3.food();
        Food food4=foodfactory.getFood("peach");
        food4.food();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值