结构型/享元模式/TextEditor.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

namespace DesignModeConsole.结构型.享元模式;

/*
 这里的文本编辑器设定只实现文字编辑功能,不包含图片、表格等复杂的编辑功能,我们只是在内存中表示一个文本文件,
只需要记录文字和格式两个部分的信息,格式又包含文字的字体、大小、颜色等信息。
 */

#region 使用享元模式之前

/// <summary>
/// 创建用来保存每个文字具体信息的类
/// </summary>
public class Character1
{
    public Character1(char c, string font, int size, int colorRgb)
    {
        C = c;
        Font = font;
        Size = size;
        ColorRgb = colorRgb;
    }

    public char C { get; set; }

    public string Font { get; set; }

    public int Size { get; set; }

    public int ColorRgb { get; set; }
}

/// <summary>
/// 创建文本编辑器  缺点:里面保存了大量的Character1对象
/// </summary>
public class Editor1
{
    private readonly List<Character1> list = new List<Character1>();

    public void AddCharacter(char c, string font, int size, int colorRgb)
    {
        var character = new Character1(c, font, size, colorRgb);
        list.Add(character);
    }
}

#endregion

#region 使用享元模式

public class CharacterStyle
{
    public CharacterStyle(string font, int size, int colorRgb)
    {
        Font = font;
        Size = size;
        ColorRgb = colorRgb;
    }

    public string Font { get; set; }

后面还有 62 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。