工厂模式(工厂方法模式和抽象工厂模式)

1.简单工厂:

简单工厂不属于23种设计模式的一种,他是工厂方法和抽象工厂的基础,工厂方法和抽象是简单工厂的演进。
贴出类图如下:


简单工厂类图.jpg
public interface Fruit {
    void juice();
}

public class Apple implements Fruit{

    private int price;
    private String color;
    public Apple() {
    }

    public Apple(int price, String color) {
        this.price = price;
        this.color = color;
    }

    @Override
    public void juice() {

    }
}

public class Orange implements Fruit{

    private int price;

    public Orange(int price) {
        this.price = price;
    }

    @Override
    public void juice() {
        
    }
}
/**
*工厂类
*/
public class FruitFactory {

    public static final int TYPE_APPLE = 1;
    public static final int TYPE_ORANGE = 2;

    public static Fruit getFruit(int type){
        if(type == 1){
            return new Apple(90,"red");
        }else if(type == 2){
            return new Orange(8);
        }

        return null;
    }

    public static Fruit getApple(){
        return FruitFactory.getFruit(TYPE_APPLE);
    }

    public static Fruit getOrange(){
        return FruitFactory.getFruit(TYPE_ORANGE);
    }
}

/**
 * 测试类
 */
public class Test {

    public static void main(String[] args) {
        Fruit fruit = FruitFactory.getFruit(FruitFactory.TYPE_APPLE);
    }
}


2.工厂方法:

工厂方法类图 .png
/**
 * 工厂接口    实体类沿用上个例子的
 */
public interface IFactory {
    Fruit getFruit();
}
/**
 * 苹果工厂
 */
public class AppleFactory implements IFactory {

    @Override
    public Fruit getFruit() {
        System.out.println("------new an apple");
        Apple apple = new Apple();
        apple.juice();
        return apple;
    }
}
/**
 * 橘子工厂
 */
public class OrangeFactory implements IFactory {
    @Override
    public Fruit getFruit() {
        Orange orange = new Orange(22);
        orange.juice();
        return orange;
    }
}
/**
 * 简单工厂的测试类
 */
public class Test {

    public static IFactory iFactory = null;

    public static void main(String[] args) {
        IFactory iFactory = new AppleFactory();
        iFactory.getFruit();

        iFactory = new OrangeFactory();
        iFactory.getFruit();
    }
}

3.抽象工厂:

工厂方法是抽象工厂的一种特殊案例

抽象工厂类图 .jpg
/**
 * 多了一项包装业务
 * 包装接口
 */
public interface IPack {
    void packFruit();
}

/**
 * 苹果包装的类
 */
public class PackApple implements IPack {
    @Override
    public void packFruit() {
        System.out.println("包装苹果------");
    }
}
/**
 * 橘子包装类
 */
public class PackOrange implements IPack {
    @Override
    public void packFruit() {
        System.out.println("包装橘子。。。。。");
    }
}

/**
 * 抽象工厂
 * 两个抽象方法,负责两项业务
 */
public abstract class AbstractFactory  {
    public abstract Fruit getFruit();
    public abstract IPack getPack();
}
/**
 * 苹果工厂
 * 具有两项业务的功能
 */
public class AppleFactory extends AbstractFactory {

    @Override
    public Fruit getFruit() {
        return new Apple();
    }

    @Override
    public IPack getPack() {
        IPack pack = new PackApple();
        pack.packFruit();
        return pack;
    }
}
/**
 * 橘子工厂
 * 具有两项业务的功能
 */
public class OrangeFactory extends AbstractFactory {
    @Override
    public Fruit getFruit() {
        return new Orange(20);
    }

    @Override
    public IPack getPack() {
        IPack pack = new PackOrange();
        pack.packFruit();
        return pack;
    }
}

/**
 * 抽象工厂的测试类
 */
public class TestAbstractFactory {
    public static void main(String[] args) {
        AbstractFactory factory = new AppleFactory();
        factory.getFruit();
        factory.getPack();
        
        factory = new OrangeFactory();
        factory.getPack();
        factory.getFruit();
    }
}

你可能感兴趣的:(工厂模式(工厂方法模式和抽象工厂模式))