设计模式之工厂模式

                                               工厂模式

三种工厂:简单工厂模式、工厂方法模式、抽象工厂模式。

 

简单工厂模式:

简单工厂模式:用户可以通过工厂得到自己需要的产品,比如说通过工厂得到一双鞋,鞋子有耐克,adidas等。

public class Customer {
    public static void main(String[] args) {
        Factory factory = new Factory();
        factory.createShoes("Nike");
        factory.createShoes("Adidas");
    }
}

public class Factory {

    public Shoes createShoes(String name) {
        switch (name) {
            case "Nike":
                return new Nike();
            case "Adidas":
                return new Adidas();
            default:
                break;
        }
        return null;
    }
}

abstract class Shoes {
    public Shoes() {

    }
}

public class Nike extends Shoes {
    public Nike() {
        System.out.println("制造一双Nike");
    }
}

public class Adidas extends Shoes {
    public Adidas() {
        System.out.println("制造一双Adidas");
    }
}

 

可以看出,简单工厂模式的扩展性不佳,如果要增加一种鞋子,需要另加一个类,同时switch中也要加上对应的种类。

 

工厂方法模式:

工厂方法模式在需要扩展时不会在工厂类中新增代码,只会新增一个具体的工厂实现类。

public class Customer {
    public static void main(String[] args) {
        // 特定的工厂制造特定的鞋子。
        FactoryShoesNike factoryShoesNike = new FactoryShoesNike();
        factoryShoesNike.createShoes();
        FactoryShoesAdidas factoryShoesAdidas = new FactoryShoesAdidas();
        factoryShoesAdidas.createShoes();
    }
}


//工厂父类提供造鞋方法
interface Factory {
    Shoes createShoes();
}

//具体的工厂实现制造过程
public class FactoryShoesNike implements Factory {
    @Override
    public Nike createShoes() {
        return new Nike();
    }
}

public class FactoryShoesAdidas implements Factory {
    @Override
    public Adidas createShoes() {
        return new Adidas();
    }
}

abstract class Shoes {
    public Shoes() {
        
    }
}

public class Nike extends Shoes {
    public Nike() {
        System.out.println("制造一双Nike");
    }
}

public class Adidas extends Shoes {
    public Adidas() {
        System.out.println("制造一双Adidas");
    }
}

抽象工厂模式:

随着需求的增加,同一款鞋子会出现不同的特征,比如说鞋的气垫会不一样,鞋的鞋面材质会不一样等等。这时候就需要就需要气垫,材质这样的产品基类。

public class Customer {
    public static void main(String[] args) {
        // 每双鞋的气垫和材质都不一样
        FactoryShoesAdidas factoryShoesAdidas = new FactoryShoesAdidas();
        factoryShoesAdidas.createAirCushion();
        factoryShoesAdidas.createMaterial();

        FactoryShoesNike factoryShoesNike = new FactoryShoesNike();
        factoryShoesNike.createAirCushion();
        factoryShoesNike.createMaterial();
    }
}

// 工厂类
interface Factory {
    public AirCushion createAirCushion();

    public Material createMaterial();
}

public class FactoryShoesAdidas implements Factory {
    @Override
    public AirCushion createAirCushion() {
        return new AirCushionA();
    }

    @Override
    public Material createMaterial() {
        return new MaterialA();
    }
}

public class FactoryShoesNike implements Factory {
    @Override
    public AirCushion createAirCushion() {
        return new AirCushionB();
    }

    @Override
    public Material createMaterial() {
        return new MaterialB();
    }
}


//产品基类
public class AirCushion {

}

public class AirCushionA extends AirCushion{
    public AirCushionA() {
        System.out.println("制造气垫A");
    }
}

public class AirCushionB extends AirCushion{
    public AirCushionB() {
        System.out.println("制造气垫B");
    }

}

public class Material {
}

public class MaterialA extends Material{
    public MaterialA() {
        System.out.println("使用A材料");
    }
}

public class MaterialB extends Material{
    public MaterialB() {
        System.out.println("使用B材料");
    }
}

 

 

 

你可能感兴趣的:(Java)