特点

  1. 结果论,不关心过程。

代码示例

package com.factory;

public interface Car {
    String getName();
}
package com.factory;

public class Audi implements Car {
    @Override
    public String getName() {
        return "Audi";
    }
}
package com.factory;

public class Benz implements Car {
    @Override
    public String getName() {
        return "Benz";
    }
}
package com.factory;

public class Bmw implements Car {
    @Override
    public String getName() {
        return "Bmw";
    }
}

简单工厂

package com.factory.simple;

import com.factory.Audi;
import com.factory.Benz;
import com.factory.Bmw;
import com.factory.Car;

public class SimpleFactory {
    public Car getCar(String name) {
        if ("Audi".equalsIgnoreCase(name)) {
            return new Audi();
        } else if ("Bmw".equalsIgnoreCase(name)) {
            return new Bmw();
        } else if ("Benz".equalsIgnoreCase(name)) {
            return new Benz();
        }
        return null;
    }
}
package com.factory.simple;

import com.factory.Car;

public class UnitTest {
    public static void main(String[] args) {
        Car car = new SimpleFactory().getCar("Benz");
        System.out.println(car.getName());
    }
}

工厂方法

package com.factory.method;

import com.factory.Car;

public interface FacotryMethod {
    Car getCar();
}
package com.factory.method;

import com.factory.Audi;
import com.factory.Car;

public class AudiFacotry implements FacotryMethod {
    @Override
    public Car getCar() {
        return new Audi();
    }
}
package com.factory.method;

import com.factory.Benz;
import com.factory.Car;

public class BenzFacotry implements FacotryMethod {
    @Override
    public Car getCar() {
        return new Benz();
    }
}
package com.factory.method;

import com.factory.Bmw;
import com.factory.Car;

public class BmwFacotry implements FacotryMethod {
    @Override
    public Car getCar() {
        return new Bmw();
    }
}
package com.factory.method;

import com.factory.Bmw;
import com.factory.Car;

public class UnitTest {
    public static void main(String[] args) {
        Car car = new AudiFacotry().getCar();
        System.out.println(car.getName());

        car = new BenzFacotry().getCar();
        System.out.println(car.getName());

        car = new BmwFacotry().getCar();
        System.out.println(car.getName());
    }
}

抽象工厂

package com.factory.abstr;

import com.factory.Car;

public abstract class AbastractFactory {
    protected abstract Car getCar();

    public Car getCar(String name) {
        if ("Audi".equalsIgnoreCase(name)) {
            return new AudiFacotry().getCar();
        } else if ("Bmw".equalsIgnoreCase(name)) {
            return new BmwFacotry().getCar();
        } else if ("Benz".equalsIgnoreCase(name)) {
            return new BenzFacotry().getCar();
        }
        return null;
    }
}
package com.factory.abstr;

import com.factory.Car;

public class DefaultFacotry extends AbastractFactory {
    private AudiFacotry facotry = new AudiFacotry();

    @Override
    protected Car getCar() {
        return facotry.getCar();
    }
}
package com.factory.abstr;

import com.factory.Audi;
import com.factory.Car;
import com.factory.method.FacotryMethod;

public class AudiFacotry extends AbastractFactory {
    @Override
    public Car getCar() {
        return new Audi();
    }
}
package com.factory.abstr;

import com.factory.Benz;
import com.factory.Car;
import com.factory.method.FacotryMethod;

public class BenzFacotry extends AbastractFactory {
    @Override
    public Car getCar() {
        return new Benz();
    }
}
package com.factory.abstr;

import com.factory.Bmw;
import com.factory.Car;
import com.factory.method.FacotryMethod;

public class BmwFacotry extends AbastractFactory {
    @Override
    public Car getCar() {
        return new Bmw();
    }
}
package com.factory.abstr;

public class UnitTest {
    public static void main(String[] args) {
        DefaultFacotry defaultFacotry = new DefaultFacotry();
        System.out.println(defaultFacotry.getCar("Bmw"));
    }
}