三种工厂模式(java)

1.静态工厂模式

package creational_pattern.static_factory_pattern;

public class StaticFactoryPattern {

    public static void main(String[] args) {
        Product concreteProduct = Factory.getProduct("ConcreteProduct");
        concreteProduct.commonMethod();
        concreteProduct.diffMethod();
    }
}

abstract class Product{

    public void commonMethod(){

    }

    public abstract void diffMethod();

}

class ConcreteProduct extends Product{


    @Override
    public void diffMethod() {

    }
}
class OtherProduct extends Product{
    @Override
    public void diffMethod() {

    }
}

class Factory{
    public static Product getProduct(String type){
        if(type.equals("ConcreteProduct"))
            return new ConcreteProduct();
        else{
            return new OtherProduct();
        }

    }
}

2.工厂方法模式

package creational_pattern.factory_method_pattern;

public class FactoryMethodPattern {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //可通过DI
        //LoggerFactory loggerFactory = new FileLoggerFactory();
        Class<?> cl = Class.forName("creational_pattern.factory_method_pattern.FileLoggerFactory");
        LoggerFactory loggerFactory = (LoggerFactory) cl.newInstance();
        Logger logger = loggerFactory.createLogger();
        logger.writeLog();
    }
}
//产品接口
interface Logger{
    void writeLog();
}
class FileLogger implements Logger{
    @Override
    public void writeLog() {
        System.out.println("file logging");
    }
}
class DatabaseLogger implements Logger{

    @Override
    public void writeLog() {
        System.out.println("database logging");
    }
}

interface LoggerFactory{
    public abstract Logger createLogger();
}

class FileLoggerFactory implements LoggerFactory{

    @Override
    public Logger createLogger() {
        //对象初始化前的准备工作
        return new FileLogger();
    }
}
class DatabaseLoggerFactory implements LoggerFactory{
    @Override
    public Logger createLogger() {
        //对象初始化前的准备工作
        return new DatabaseLogger();
    }
}

3.抽象工厂模式

package creational_pattern.abstract_factory_pattern;

public class AbstractFactoryPattern {
    public static void main(String[] args) {
        Factory factory1 = new AlibabaFctory();
        Factory factory2 = new BaiduFctory();
        ProductA productA = factory1.createProductA();
        productA.display();
        System.out.println("----------");
        ProductA preparing_workA = factory1.createProductA("preparing_workA");
        preparing_workA.display();
        System.out.println("----------");
        ProductB preparing_workB = factory1.createProductB("preparing_workB");
        preparing_workB.display();
        System.out.println("----------");
        ProductB productB = factory1.createProductB("productB");
        productB.display();
        System.out.println("----------");
        ProductA productA1 = factory2.createProductA();
        productA1.display();
        System.out.println("----------");
        ProductB productB1 = factory2.createProductB();
        productB1.display();

    }
}

abstract class ProductA{
    abstract void display();
}
class AlibabaProductA1 extends  ProductA{

    @Override
    void display() {
        System.out.println("AlibabaProductA1");
    }
}
class BaiduProductA2 extends ProductA{

    @Override
    void display() {
        System.out.println("BaiduProductA2");
    }
}
abstract class ProductB{
    abstract void display();
}
class AlibabaProductB1 extends  ProductB{
    @Override
    void display() {
        System.out.println("AlibabaProductB1");
    }
}
class BaiduProductB2 extends ProductB{
    @Override
    void display() {
        System.out.println("BaiduProductB2");
    }
}
//声明了一组用于创建一族的对象的方法
interface Factory{
    ProductA createProductA();
    ProductA createProductA(String args);
    ProductB createProductB();
    ProductB createProductB(String args);
}
//创建alibaba产品族的对象
class AlibabaFctory implements Factory{
    @Override
    public ProductA createProductA() {
        return new AlibabaProductA1();
    }

    @Override
    public ProductA createProductA(String args) {
        System.out.println(args);
        return new AlibabaProductA1();
    }

    @Override
    public ProductB createProductB() {
        return new AlibabaProductB1();
    }

    @Override
    public ProductB createProductB(String args) {
        System.out.println(args);
        return new AlibabaProductB1();
    }
}
//创建baidu产品族的对象
class BaiduFctory implements Factory{
    @Override
    public ProductA createProductA() {
        return new BaiduProductA2();
    }

    @Override
    public ProductA createProductA(String args) {
        System.out.println(args);
        return new BaiduProductA2();
    }

    @Override
    public ProductB createProductB() {
        return new BaiduProductB2();
    }

    @Override
    public ProductB createProductB(String args) {
        System.out.println(args);
        return new BaiduProductB2();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值