开发者

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

开发者 https://www.devze.com 2024-11-08 10:39 出处:网络 作者: 北极熊不在北极
目录一、简单厂模式二、工厂方法模式三、抽象工厂模式总结一、简单厂模式
目录
  • 一、简单厂模式
  • 二、工厂方法模式
  • 三、抽象工厂模式
  • 总结

一、简单厂模式

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

  • 手机接口类
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.facjavascripttorymethod;

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() {JpzhPEyr
        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 Fact编程oryMethodMain {

    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 AbstracpythontFactory{
    @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) {
        AbsJpzhPEyrtractFactory abstractFactory = new XiaoMiFactory();
        Car car = abstractFactory.getCar();
        Phone phone = abstractFactory.getPhone();
        car.showInfo();
        phone.showInfo();
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号