设计模式——工厂模式(代码分析)

代码分析可结合另一篇逻辑分析图配合工厂模式逻辑分析图

简单工厂

组成:
1)工厂
2)抽象产品
3)产品

 //1)工厂
 public class Factory {
    //可以在工厂类中添加任何你所需要的逻辑
    public static Product create(String str)
    {
        //生成ProductA
        if(str.equalsIgnoreCase("ProductA"))
        {
            return new ProductA();
        }
        else
            //生成ProductB
            if(str.equalsIgnoreCase("ProductB"))
            {
                return new ProductB();
            }
        return null;
    }
}
//2)抽象产品
public interface  Product { //声明类所需继承的共同接口,也可以是抽象类
    
}
 
//3)产品A
class ProductA implements Product {
    public ProductA() {
        System.out.println("ProductA");
    }
}
 //3)产品B
class ProductB implements Product {
    public ProductB() {
        System.out.println("ProductB");
    }
}
//测试
 public class test {
    public static void main(String[] args) {
        Factory.create("productA");
        Factory.create("ProductB");
    }
}

普通工厂

组成:
1)抽象工厂
2)具体工厂
3)抽象产品
4)具体产品

 //	1)抽象工厂
public abstract class VehicleFactory {
    abstract Moveable create();
}
//2)具体工厂
public class PlaneFactory extends VehicleFactory{
    public Moveable create() {
        return new Plane();
    }
}
//2)具体工厂
public class BroomFactory extends VehicleFactory{
    public Moveable create() {
        return new Broom();
    }
}
//3)抽象产品
public interface Moveable {
    void run();
}
//	4)具体产品
public class Plane implements Moveable {
    @Override
    public void run() {
        System.out.println("plane....");
    }
}
//	4)具体产品
public class Broom implements Moveable {
    @Override
    public void run() {
        System.out.println("broom.....");
    }
}
//测试
 public class Test {
    public static void main(String[] args) {
        VehicleFactory factory = new PlaneFactory ();
        Moveable m = factory.create();
        m.run();
    }
}

抽象工厂

组成:
1)抽象工厂
2)具体工厂
3)抽象产品
4)具体产品
区别:
在普通工厂的基础上,由于产品A和产品B的种类细化需要将产品分类更细分为1系列和2系列,所以产品A分为A-1和A-2,产品B分为B-1和B-2。A系列产品需要一个抽象类,B系列产品需要一个抽象类。两个抽象类在抽象工厂中声明。工厂方法模式只生产一个等级的产品,而抽象工厂模式可生产多个等级的产品。

//1)抽象工厂
interface Factory {
    public Product createProduct();
    public Gift createGift();

}
//2)具体工厂
class FactoryA implements Factory {
    @Override
    public Product createProduct(){
        return new ProductA();
    }
    @Override
    public Gift createGift(){
        return new GiftA();
    }
}
//2)具体工厂
class FactoryB implements Factory {
    @Override
    public Product createProduct(){
        return new ProductB();
    }
    @Override
    public Gift createGift(){
        return new GiftB();
    }
}
//3)抽象产品
interface Product{}
//3)抽象产品
interface Gift {} 

//	4)具体产品
class ProductA implements Product {
    public ProductA() {
        System.out.println("ProductA");
    }
}
//	4)具体产品
class ProductB implements Product {
    public ProductB() {
        System.out.println("ProductB");
    }
}
//	4)具体产品
class GiftA implements Gift {
    public GiftA(){
        System.out.println("GiftA");
    }
}
//	4)具体产品
class GiftB implements Gift {
    public GiftB(){
        System.out.println("GiftB");
    }
}
//测试
 public class test{
    public static void main(String[] args) {
        Factory factory = new FactoryA();
        factory.createGift();
        factory.createProduct();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白菜S

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值