java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java设计模式之工厂模式

java设计模式之工厂模式详解

作者:北极熊不在北极

本文介绍了三种常见的设计模式:简单厂模式、工厂方法模式和抽象工厂模式,简单厂模式通过一个工厂类来生产不同的产品实例,例如同时生产华为和小米手机,工厂方法模式则通过定义一个创建对象的接口,让子类决定实例化哪一个类

一、简单厂模式

一个工厂既代工生产华为手机也生产小米手机。

package com.liuyc.designpattern.factory;

public interface Phone {

    void showInfo();
}
package com.liuyc.designpattern.factory;

public class HuaWeiPhone implements Phone{
    @Override
    public void showInfo() {
        System.out.println("华为手机");
    }
}
package com.liuyc.designpattern.factory;

public class XiaoMiPhone implements Phone{
    @Override
    public void showInfo() {
        System.out.println("小米手机");
    }
}
package com.liuyc.designpattern.factory.simplefactory;

import com.liuyc.designpattern.factory.Phone;

public class SimpleFactory {

    public static Phone getPhone(String className){
        Phone phone = null;
        try {
            Class<Phone> aClass = (Class<Phone>) Class.forName(className);
            phone = aClass.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return phone;
    }
}
package com.liuyc.designpattern.factory.simplefactory;

import com.liuyc.designpattern.factory.Phone;

public class SimpleFactoryMain {

    public static void main(String[] args) {
        Phone phone = SimpleFactory.getPhone("com.liuyc.designpattern.factory.XiaoMiPhone");
        phone.showInfo();
    }
}

二、工厂方法模式

package com.liuyc.designpattern.factory.factorymethod;

import com.liuyc.designpattern.factory.Phone;

public interface PhoneFactory {

    Phone getPhone();
}
package com.liuyc.designpattern.factory.factorymethod;

import com.liuyc.designpattern.factory.HuaWeiPhone;
import com.liuyc.designpattern.factory.Phone;

public class HuaWeiPhoneFactory implements PhoneFactory {
    @Override
    public Phone getPhone() {
        return new HuaWeiPhone();
    }
}
package com.liuyc.designpattern.factory.factorymethod;

import com.liuyc.designpattern.factory.Phone;
import com.liuyc.designpattern.factory.XiaoMiPhone;

public class XiaoMiPhoneFactory implements PhoneFactory {
    @Override
    public Phone getPhone() {
        return new XiaoMiPhone();
    }
}
package com.liuyc.designpattern.factory.factorymethod;

import com.liuyc.designpattern.factory.Phone;

public class FactoryMethodMain {

    public static void main(String[] args) {
        PhoneFactory phoneFactory = new XiaoMiPhoneFactory();
        Phone phone = phoneFactory.getPhone();
        phone.showInfo();
    }
}

三、抽象工厂模式

现在,不管是华为也好,小米也好,他们已不满足于只生产手机了,他们也开始造车了,现在有一个“组”的概念了,或者说族。

package com.liuyc.designpattern.factory;

public interface Car {

    void showInfo();
}
package com.liuyc.designpattern.factory;

public class HuaWeiCar implements Car {
    @Override
    public void showInfo() {
        System.out.println("华为小汽车");
    }
}
package com.liuyc.designpattern.factory;

public class XiaoMiCar implements Car {
    @Override
    public void showInfo() {
        System.out.println("小米汽车");
    }
}
package com.liuyc.designpattern.factory.abstactfactory;

import com.liuyc.designpattern.factory.Car;
import com.liuyc.designpattern.factory.Phone;

public interface AbstractFactory {

    Phone getPhone();

    Car getCar();
}
package com.liuyc.designpattern.factory.abstactfactory;

import com.liuyc.designpattern.factory.Car;
import com.liuyc.designpattern.factory.HuaWeiCar;
import com.liuyc.designpattern.factory.HuaWeiPhone;
import com.liuyc.designpattern.factory.Phone;

public class HuaWeiFactory implements AbstractFactory{
    @Override
    public Phone getPhone() {
        return new HuaWeiPhone();
    }

    @Override
    public Car getCar() {
        return new HuaWeiCar();
    }
}
package com.liuyc.designpattern.factory.abstactfactory;

import com.liuyc.designpattern.factory.*;

public class XiaoMiFactory implements AbstractFactory{
    @Override
    public Phone getPhone() {
        return new XiaoMiPhone();
    }

    @Override
    public Car getCar() {
        return new XiaoMiCar();
    }
}
package com.liuyc.designpattern.factory.abstactfactory;

import com.liuyc.designpattern.factory.Car;
import com.liuyc.designpattern.factory.Phone;

public class AbstractFactoryMain {

    public static void main(String[] args) {
        AbstractFactory abstractFactory = new XiaoMiFactory();
        Car car = abstractFactory.getCar();
        Phone phone = abstractFactory.getPhone();
        car.showInfo();
        phone.showInfo();
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文