简单工厂模式和抽象工厂模式

简单工厂模式

定义一些类及抽象类

package com.learn.simpleFactory.model;
public class Apple implements Fruit{
    @Override
    public void eatFruit() {
        System.out.println("吃了一个苹果");
    }

    @Override
    public void buyFruit() {
        System.out.println("买了一个苹果");
    }


package com.learn.simpleFactory.model;
public class Banana implements Fruit{
    @Override
    public void eatFruit() {
        System.out.println("吃了一个香蕉");
    }

    @Override
    public void buyFruit() {
        System.out.println("买了一个香蕉");
    }
}

}


package com.learn.simpleFactory.model;
//创建一个水果公共接口类
public interface Fruit {
    void eatFruit();
    void buyFruit();
}


创建一个工厂类用来生产这两个对象的实例

package com.learn.simpleFactory.factory;

import com.learn.simpleFactory.model.Apple;
import com.learn.simpleFactory.model.Banana;
import com.learn.simpleFactory.model.Fruit;

//创建一个水果工厂对象用于创建不同水果的实例
public class FruitFactory {
    //通过参数去获取不同的水果对象的实例
    public static Fruit getInstance(String param){
        if ("apple".equals(param)){
            return new Apple();
        }else {
            return new Banana();
        }
    }
}

demo

package com.learn.simpleFactory.run;

import com.learn.simpleFactory.factory.FruitFactory;
import com.learn.simpleFactory.model.Fruit;

public class App {
    public static void main(String[] args) {
        Fruit apple = FruitFactory.getInstance("apple");
        apple.eatFruit();
        apple.buyFruit();
        Fruit banana = FruitFactory.getInstance("banana");
        banana.eatFruit();
        banana.buyFruit();
    }
}

简单工厂关系图

在这里插入图片描述

抽象工厂模式

简单工厂适用于解决单种类型的多个对象,对于多类型的对象应该使用抽象工厂模式,例:苹果和香蕉都有品牌,并且每个品牌都可以实现多种水果

定义水果类及抽象类


//创建一个水果公共接口类
public interface Fruit {
    void eatFruit();
    void buyFruit();
}

package com.learn.absFactory.model;
public class FSKApple implements Fruit{

    @Override
    public void eatFruit() {
        System.out.println("吃富士康苹果");
    }

    @Override
    public void buyFruit() {
        System.out.println("买富士康苹果");
    }
}

package com.learn.absFactory.model;
public class FSKBanana implements Fruit{
    @Override
    public void eatFruit() {
        System.out.println("吃富士康香蕉");
    }

    @Override
    public void buyFruit() {
        System.out.println("买富士康香蕉");
    }
}

package com.learn.absFactory.model;
public class HPGApple implements Fruit{
    @Override
    public void eatFruit() {
        System.out.println("吃红苹果苹果");
    }

    @Override
    public void buyFruit() {
        System.out.println("买红苹果苹果");
    }
}

package com.learn.absFactory.model;
public class HPGBanana implements Fruit{
    @Override
    public void eatFruit() {
        System.out.println("吃红苹果香蕉");
    }

    @Override
    public void buyFruit() {
        System.out.println("买红苹果香蕉");
    }
}

创建一个抽象工厂类,作为超级工厂管理多个类型的工厂

package com.learn.absFactory.factory;

import com.learn.absFactory.model.Fruit;
//创建一个抽象的水果工厂
public interface FruitFactory {
    Fruit getAppleInstance();
    Fruit getBananaInstance();
}




package com.learn.absFactory.factory;
import com.learn.absFactory.model.FSKApple;
import com.learn.absFactory.model.FSKBanana;
import com.learn.absFactory.model.Fruit;
//实现具体的创建实例流程
public class FSKFactory implements FruitFactory{
    @Override
    public Fruit getAppleInstance() {
        return new FSKApple();
    }

    @Override
    public Fruit getBananaInstance() {
        return new FSKBanana();
    }
}


package com.learn.absFactory.factory;
import com.learn.absFactory.model.Fruit;
import com.learn.absFactory.model.HPGApple;
import com.learn.absFactory.model.HPGBanana;

public class HPGFactory implements FruitFactory{

    @Override
    public Fruit getAppleInstance() {
        return new HPGApple();
    }

    @Override
    public Fruit getBananaInstance() {
        return new HPGBanana();
    }
}

demo

package com.learn.absFactory.run;

import com.learn.absFactory.factory.FSKFactory;
import com.learn.absFactory.factory.FruitFactory;
import com.learn.absFactory.factory.HPGFactory;
import com.learn.absFactory.model.Fruit;

public class App {
    public static void main(String[] args) {
        FruitFactory fskFactory = new FSKFactory();
        //获取富士康的苹果和香蕉
        fskFactory.getAppleInstance().buyFruit();
        fskFactory.getAppleInstance().eatFruit();

        fskFactory.getBananaInstance().buyFruit();
        fskFactory.getBananaInstance().eatFruit();

        FruitFactory hpgFactory = new HPGFactory();
        Fruit hpgApple = hpgFactory.getAppleInstance();
        hpgApple.buyFruit();
        hpgApple.eatFruit();
        Fruit hpgBanana = hpgFactory.getBananaInstance();
        hpgBanana.buyFruit();
        hpgBanana.eatFruit();

    }
}

抽象工厂关系图

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值