Unity 抽象工厂模式(实例详解)

文章目录

简介

抽象工厂模式是一种创建型设计模式,它提供了一种方式来封装一组相关或相互依赖对象的创建过程,而无需指定具体类。这种模式常用于系统中有多组相关产品族,且客户端需要使用不同产品族中的对象时。
在Unity中,抽象工厂模式可以用于创建一组相关对象,例如不同类型的UI元素(按钮、文本框等)。这里给出一个简化版的实例:

实例1

首先,定义抽象工厂和抽象产品接口:

public interface IUIFactory
{
   
    IUIButton CreateButton();
    IUITextBox CreateTextBox();
}

public interface IUIButton
{
   
    void Display();
}

public interface IUITextBox
{
   
    void Display();
}

然后,实现两个具体的工厂类,每个对应一种风格的UI元素:

public class ModernUIFactory : IUIFactory
{
   
    public IUIButton CreateButton()
    {
   
        return new ModernUIButton();
    }

    public IUITextBox CreateTextBox()
    {
   
        return new ModernUITextBox();
    }
}

public class RetroUIFactory : IUIFactory
{
   
    public IUIButton CreateButton()
    {
   
        return new RetroUIButton();
    }

    public IUITextBox CreateTextBox()
    {
   
    return new RetroUITextBox();
    }
}

// 实现现代风格的具体按钮和文本框
public class ModernUIButton : IUIButton
{
   
    public void Display()
    {
   
        Debug.Log("Displaying a modern style button.");
    }
}

public class ModernUITextBox : IUITextBox
{
   
    public void Display()
    {
   
        Debug.Log("Displaying a modern style text box.");
    }
}

// 实现复古风格的具体按钮和文本框
public class RetroUIButton : IUIButton
{
   
    public void Display()
    {
   
        Debug.Log("Displaying a retro style button.");
    }
}

public class RetroUITextBox : IUITextBox
{
   
    public void Display()
    {
   
        Debug.Log("Displaying a retro style text box.");
    }
}

最后,在客户端代码中使用抽象工厂来创建和操作UI元素:

public class UIManager
{
   
    private readonly IUIFactory _factory;

    public UIManager(IUIFactory factory)
    {
   
        _factory = factory;
    }

    public void CreateAndDisplayUIElements()
    {
   
        var button = _factory.CreateButton();
        var textBox = _factory.CreateTextBox();

        button.Display();
        textBox.Display();
    }
}

// 使用方式:
var modernManager = new UIManager(new ModernUIFactory());
modernManager.CreateAndDisplayUIElements(); // 输出现代风格的UI元素

var retroManager = new UIManager(new RetroUIFactory());
retroManager.CreateAndDisplayUIElements(); // 输出复古风格的UI元素

在这个例子中,ModernUIFactoryRetroUIFactory 分别代表了两种不同的UI风格的抽象工厂。通过传递不同的工厂给 UIManager,我们可以根据需求动态创建并显示不同风格的UI元素,且不关心具体实现细节。如果未来需要添加新的UI风格,只需新增对应的工厂和产品类即可。

实例2

抽象工厂模式在很多场景下都具有实用价值,下面再举一个游戏开发中的例子:

假设我们正在开发一款支持多种主题(如:科幻、中世纪和卡通)的游戏,每种主题都有其独特的角色外观、武器外观和背景音乐。我们可以使用抽象工厂模式来处理不同主题资源的创建和管理。

public interface IThemeFactory
{
   
    ICharacter CreateCharacter();
    IWeapon CreateWeapon();
    ISoundtrack CreateSoundtrack();
}

public interface ICharacter
{
   
    void Display();
}

public interface IWeapon
{
   
    void Display();
}

public interface ISoundtrack
{
   
    void Play();
}

// 科幻主题实现
public class SciFiThemeFactory : IThemeFactory
{
   
    public ICharacter CreateCharacter()
    {
   
        return new SciFiCharacter();
    }

    public IWeapon CreateWeapon()
    {
   
        return new LaserGun();
    }

    public ISoundtrack CreateSoundtrack()
    {
   
        return new SpaceMusic();
    }
}

// 中世纪主题实现
public class MedievalThemeFactory : IThemeFactory
{
   
    public ICharacter CreateCharacter()
    {
   
        return new Knight();
    }

    public IWeapon CreateWeapon()
    {
   
        return new Sword();
    }

    public ISoundtrack CreateSoundtrack()
    {
   
        return new MedievalMusic();
    }
}

// 游戏客户端代码
public class GameClient
{
   
    private readonly IThemeFactory _themeFactory;

    public GameClient(IThemeFactory themeFactory)
    {
   
        _themeFactory = themeFactory;
    }

    public void InitializeGame()
    {
   
        var character = _themeFactory.CreateCharacter();
        var weapon = _themeFactory.CreateWeapon();
        var soundtrack = _themeFactory.CreateSoundtrack();

        character.Display();
        weapon.Display();
        soundtrack.Play();
    }
}

// 使用方式:
var sciFiGame = new GameClient(new SciFiThemeFactory());
sciFiGame.InitializeGame(); // 初始化并展示科幻主题的游戏元素

var medievalGame = new GameClient(new MedievalThemeFactory());
medievalGame.InitializeGame(); // 初始化并展示中世纪主题的游戏元素

在这个例子中,IThemeFactory 是抽象工厂接口,它定义了创建游戏角色、武器和音乐的方法。而 SciFiThemeFactoryMedievalThemeFactory 则是具体工厂类,根据不同的主题生成相应的游戏元素。通过传递不同的工厂给 GameClient,可以灵活地切换游戏的主题风格,并且在不修改原有代码的情况下扩展新的主题。

python推荐学习汇总连接:
50个开发必备的Python经典脚本(1-10)

50个开发必备的Python经典脚本(11-20)

50个开发必备的Python经典脚本(21-30)

50个开发必备的Python经典脚本(31-40)

50个开发必备的Python经典脚本(41-50)
————————————————

​最后我们放松一下眼睛
在这里插入图片描述

相关推荐

  1. 【设计模式抽象工厂模式详解

    2024-01-23 08:22:04       18 阅读
  2. MQL语言实现抽象工厂模式

    2024-01-23 08:22:04       16 阅读
  3. Python如何实现抽象工厂模式

    2024-01-23 08:22:04       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-23 08:22:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-23 08:22:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-23 08:22:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-23 08:22:04       20 阅读

热门阅读

  1. 2695. 包装数组

    2024-01-23 08:22:04       30 阅读
  2. 【最新!超详细C++入门】

    2024-01-23 08:22:04       26 阅读
  3. Spring事件发布ApplicationEventPublisher原理

    2024-01-23 08:22:04       33 阅读
  4. 排序算法——冒泡排序算法详解

    2024-01-23 08:22:04       35 阅读
  5. Angular: 配置 proxy 解决跨域

    2024-01-23 08:22:04       28 阅读
  6. WPF行为

    2024-01-23 08:22:04       32 阅读