工厂模式

工厂模式

水果采集,通常我们都是直接定义一个抽象水果接口,然后各种水果进行实现水果接口,然后new去创建,先上代码块

/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:47
 **/
public interface Fruit {

    /**
     * 采集水果
     */
    void get();
}
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public class Apple implements Fruit{
    @Override
    public void get() {
        System.out.println("采集苹果");
    }
}
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public class Banana implements Fruit{
    @Override
    public void get() {
        System.out.println("采集香蕉");
    }
}
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:48
 **/
public class Main {

    public static void main(String[] args) {
        //这种一旦对象创建特别复杂,多个地方调用,就会有大量的重复代码,一旦修改,不容易维护。
        Fruit apple = new Apple();
        Fruit banana = new Banana();
        apple.get();
        banana.get();
    }
}

简单工厂

/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:50
 * 水果工厂  简单工厂实现
 **/
public class FruitFactory {



    public static Fruit getFruit(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        return (Fruit)Class.forName(className).newInstance();
    }
}

   public static Fruit getFruit(FruitType fruitType) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
       return fruitType.getClazz().newInstance();
   }
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:48
 **/
public class Main {
	
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        //这样只需要传入获取对象的对应参数,获取对象,无需再关心如何创建,只需要获取使用
        Fruit apple = FruitFactory.getFruit("Apple");
        Fruit banana = FruitFactory.getFruit("Banana");
//        Fruit apple = FruitFactory.getFruit(FruitType.APPLE);
//        Fruit banana = FruitFactory.getFruit(FruitType.BANANA);
        apple.get();
        banana.get();
    }
}

在这里插入图片描述

在这里插入图片描述

工厂方法

简单工厂有个缺点:

  1. 工厂类的职责相对过重,增加新的产品时需要修改工厂类的判断
    逻辑,违背开闭原则。

  2. 不易于扩展过于复杂的产品结构。

    /**
     * @author DK
     * @version 1.0
     * @date 2020-03-16 12:50
     **/
    public interface FruitFactory {
    
        Fruit getFruit() throws IllegalAccessException, InstantiationException;
    
    }
    
    /**
     * @author DK
     * @version 1.0
     * @date 2020-03-16 12:50
     **/
    public class BananaFactory implements  FruitFactory{
    
        @Override
        public Fruit getFruit() throws IllegalAccessException, InstantiationException {
            return Banana.class.newInstance();
        }
    }
    
    /**
     * @author DK
     * @version 1.0
     * @date 2020-03-16 12:50
     **/
    public class AppleFactory implements FruitFactory{
    
    
        @Override
        public Fruit getFruit() throws IllegalAccessException, InstantiationException {
            return Apple.class.newInstance();
        }
    }
    
    public class Main {
    	//用户只需关心所需产品对应的工厂,无须关心创建细节。
    	//加入新产品符合开闭原则,提高了系统的可扩展性。
        public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
            FruitFactory appleFactory = new AppleFactory();
            FruitFactory bananaFactory = new BananaFactory();
            Fruit apple = appleFactory.getFruit();
            Fruit banana = bananaFactory.getFruit();
            apple.get();
            banana.get();
        }
    }
    

在这里插入图片描述

在这里插入图片描述

抽象工厂

一旦出现多个品牌相同产品的时候,工厂方法不再适用,如果南方水果,和北方水果,此时采用抽象工厂

/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public abstract class Apple implements Fruit {
    @Override
    public abstract  void get();
}
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public abstract class Banana implements Fruit {
    @Override
    public abstract  void get();
}

/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:47
 **/
public interface Fruit {

    /**
     * 采集水果
     */
    void get();
}
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:50
 * 水果工厂  抽象工厂 对应产品结构
 **/
public interface FruitFactory {

    Fruit getApple() throws IllegalAccessException, InstantiationException;

    Fruit getBanana() throws IllegalAccessException, InstantiationException;
}
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public  class NorthApple extends Apple {

    @Override
    public void get() {
        System.out.println("北方苹果");
    }
}

/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public  class NorthBanana extends Banana {
    @Override
    public void get() {
        System.out.println("北方香蕉");
    }
}

/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public class SouthApple extends Apple {


    @Override
    public void get() {
        System.out.println("南方苹果");
    }
}

/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:46
 **/
public  class SouthBanana extends Banana {
    @Override
    public void get() {
        System.out.println("南方香蕉");
    }
}

/**
 * @author DK
 * @version 1.0
 * @date 2020/3/16 下午10:19
 */
public class SouthFactory implements FruitFactory{
    @Override
    public Fruit getApple() throws IllegalAccessException, InstantiationException {
        return SouthApple.class.newInstance();
    }

    @Override
    public Fruit getBanana() throws IllegalAccessException, InstantiationException {
        return SouthBanana.class.newInstance();
    }
}
/**
 * @author DK
 * @version 1.0
 * @date 2020/3/16 下午10:24
 */
public class NorthFactory implements FruitFactory{
    @Override
    public Fruit getApple() throws IllegalAccessException, InstantiationException {
        return NorthApple.class.newInstance();

    }

    @Override
    public Fruit getBanana() throws IllegalAccessException, InstantiationException {
        return NorthBanana.class.newInstance();
    }
}
/**
 * @author DK
 * @version 1.0
 * @date 2020-03-16 12:48
 **/
public class Main {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        FruitFactory southFactory = new SouthFactory();
        Fruit southFactoryApple = southFactory.getApple();
        Fruit southFactoryBanana = southFactory.getBanana();
        southFactoryApple.get();
        southFactoryBanana.get();
        FruitFactory northFactory = new NorthFactory();
        Fruit northFactoryApple = northFactory.getApple();
        Fruit northFactoryBanana = northFactory.getBanana();
        northFactoryApple.get();
        northFactoryBanana.get();

    }
}

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值