设计模式-4种工厂模式

设计模式-4种工厂模式,第1张

目录

1、简单工厂Simple Factory Pattern

2、静态工厂Static Factory

3、工厂方法

4、抽象工厂

总结:


1、简单工厂Simple Factory Pattern

示例代码

//接口
public interface Sample{
    
}
//具体类
public class SampleA implements Sample{
    
}
//具体类
public class SampleB implements Sample{
    
}
//简单工厂
public class Factory{
    public Sample creator(int which){ 
    if (which==1)
        return new SampleA();
    else if (which==2)
        return new SampleB();
    }
}
//测试类
public class Test{

  public static void main(String []args){

    Sample sampleA=Factory.creator(1);

   }
    
}
2、静态工厂Static Factory

单例属于静态工厂,它是由静态方法产生的

//接口
public interface Sample{
    
}
//具体类
public class SampleA implements Sample{
    
}
//具体类
public class SampleB implements Sample{
    
}
//简单工厂
public class Factory{
    public static Sample creator(int which){ 
    if (which==1)
        return new SampleA();
    else if (which==2)
        return new SampleB();
    }
}
//测试类
public class Test{

  public static void main(String []args){

    Sample sampleA=Factory.creator(1);

   }
    
}
3、工厂方法

优点:产品模式扩展方便

 

 

 a、定义产品

//交通工具
public interface Moveable{
    void go();
}
//汽车
public class Car implements Moveable {

    public void go(){
        System.out.println("car go ......");
    }
}
//飞机
public class plane implements Moveable {

    public void go(){
        System.out.println("plane go ......");
    }
}

b、定义对对应产品工厂


public class CarFactory{
    public Moveable create(){
        retrun new  Car();
    }
    
}

public class PlaneFactory{
    public Moveable create(){
        retrun new  Plane();
    }
}

c、测试

public class Test{

    public static void main(String[]args){
        Moveable car = CarFactory.create();

        Moveable plane=PlaneFactory.create();

    }
}

4、抽象工厂

优点:产品族扩展方便

 

a、定义产品

//键盘
public abstract class keyboard {
    abstract void key();
}

//鼠标
public abstract class Mouse{
    abstract void dian();
}

b、定义抽象工厂

将产品系列放入工厂内

public abstract class  AbstractFactory{

   abstract keyboard createKeybord();
    
   abstract  Mouse createMouse();    

}

c、实现具体产品工厂

//产品族1 dell

public class DellMouse  extents Mouse{

    public void dian(){
        System.out.println(" create a dell mouse");
    }
}

public class DellKeyborad  extents Mouse{

    public void key(){
        System.out.println(" create a dell keyborad");
    }
}

//产品族2 HP

public class HPMouse  extents Mouse{

    public void dian(){
        System.out.println(" create a hp mouse");
    }
}

public class HPKeyborad  extents Mouse{

    public void key(){
        System.out.println(" create a hp keyborad");
    }
}

d、创建对应产品族工厂

//Dell产品族工厂
public class DellAbstractFactory extends AbstractFactory{

    public Keyborad createKeyborad(){
        retrun new DellKeyBorad();
    }
    public Mouse createMouse(){
        return new DellMouse();
    }
}
//HP产品族工厂
public class HPAbstractFactory extends AbstractFactory{

    public Keyborad createKeyborad(){
        retrun new HPKeyBorad();
    }
    public Mouse createMouse(){
        return new HPMouse();
    }
}

c、测试类

public class Test{

    public static void main(String[]args){
        //Dell产品族
        AbstractFactory dellAf = new  DellAbstractFactory();
        Mouse mouse = dellAf.createMouse();
        Keyborad keyborad = dellAf.createMouse();

        //Hp产品族
        AbstractFactory hpAf = new  HPAbstractFactory();
        Mouse hpmouse = hpAf .createMouse();
        Keyborad hpkeyborad = hpAf .createMouse();
    
    }
}

总结:

工厂方法是抽象工厂单产品族的体现。

工厂模式可以讲项目中大量的if else 逻辑进行抽离。

常见使用场景:框架中数据源的配置,通过工厂模式实现

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/727857.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存