• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
aibianchengya
博客园    首页    新随笔    联系   管理    订阅  订阅
JAVA设计模式-工厂模式

JAVA设计模式-工厂模式

简单工厂模式

介绍

简单工厂模式就是定义一个工厂类,工厂类提供获取实例的方法,方法会根据传入的参数不同来返回不同的实例。不同的实例基本都有共同的父类。对于下面的例子里面增加新的动物需要修改代码,否则无法扩展。

image-20220917131508935

image-20220917130638382

代码示例

/**
 * 接口定义
 */
interface Animal {
    void eat();
}
/**
 * 实现类
 */
class Dog implements Animal{
    public void eat() {

    }
}
/**
 * 实现类
 */
class Cat implements Animal{
    public void eat() {

    }
}
class AnimalFactory{
    public static Animal createAnimal(String type){
        if("Cat".equals(type)){
            return new Cat();
        } else if("Dog".equals(type)){
            return new Dog();
        }
        return null;
    }
}

工厂方法模式

介绍

工厂方法模式和简单工厂模式区别,简单工厂模式工厂类只有一个,工厂方法模式可能有一个或者多个,它们都是实现了相同接口的一组工厂类。

image-20220917131803336

image-20220917132050471

代码示例

/**
 * 接口定义
 */
interface Animal {
    void eat();
}
/**
 * 实现类
 */
class Dog implements Animal{
    public void eat() {

    }
}
/**
 * 实现类
 */
class Cat implements Animal{
    public void eat() {

    }
}
/**
 * 接口定义
 */
interface AnimalFactory {
    Animal createAnimal();
}
/**
 * 实现类
 */
class DogFactory implements AnimalFactory{
    public Animal createAnimal() {
        return new Dog();
    }
}
/**
 * 实现类
 */
class CatFactory implements AnimalFactory{
    public Animal createAnimal() {
        return new Cat();
    }
}

抽象工厂模式

介绍

抽象工厂模式可以理解成创建工厂的工厂,每一个生成的工厂又可以按照工厂模式创建对象。这里面其实有一个产品族的概念,产品族是不同的一组产品等级结构的一组产品。还有产品等级结构等概念,这里就不过多展开了。抽象工厂模式可以理解为解决两个维度组合产品的构造问题,取其中一个维度作为产品族,另一个维度作为产品族中的具体的多个产品。

image-20220917210824122

image-20220917210130567

代码示例
interface Dog {
    void eat();
}

class BlackDog implements Dog{
    public void eat() {

    }
}

class WhiteDog implements Dog{
    public void eat() {

    }
}

interface Cat {
    void eat();
}

class BlackCat implements Cat{
    public void eat() {

    }
}

class WhiteCat implements Cat{
    public void eat() {

    }
}

interface AnimalFactory {
    Dog createGog();
    Cat createCat();
}

class WhiteAnimalFactory implements AnimalFactory{
    public Dog createGog() {
        return new WhiteDog();
    }

    public Cat createCat() {
        return new WhiteCat();
    }
}

class BlackAnimalFactory implements AnimalFactory{
    public Dog createGog() {
        return new BlackDog();
    }

    public Cat createCat() {
        return new BlackCat();
    }
}

小结

工厂方法模式只有一个抽象产品类,可以有多个具体的产品类去实现,工厂类类似,只有一个抽象的工厂类,可以有多个具体的工厂类去实现,每一个具体的工厂类只能创建一个具体的产品类的实例。

抽象工厂模式有多个抽象的产品类,每个抽象的产品类可以有多个具体的产品类去实现,但是只有一个抽象的工厂类,这个工厂类可以有多个具体的工厂类去实现,每个具体的工厂类可以创建多个具体的产品类的实例。

关注微信公众号「平哥技术站」, 每日更新,在手机上阅读所有教程,随时随地都能学习。

原文链接:https://monkey.blog.xpyvip.top/archives/java-she-ji-mo-shi---gong-chang-mo-shi

posted on 2022-09-18 11:33  程序猿的代码  阅读(63)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3