The Flyweight design pattern uses sharing to support large numbers of fine-grained objects efficiently.
简单来说~ Flyweight模式使用共享来提供大量的细节对象
学习目标: 享元模式的概念及实务
学习难度: ☆☆☆
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
//建立一个生产game的工厂
public class GameFactory
{
public Game GetGame(string factory) //回传Game物件
{
Game game = null;
if (factory == "Ensemble")
{
game = new AOE();
}
else if (factory == "VALVE")
{
game = new CSGO();
}
return game;
}
}
//建立一个抽象类,用於定义物件的细节
public abstract class Game
{
protected string type;
protected string name;
public string version;
public int price;
public abstract void Demo();
}
//实作物件1
public class AOE : Game
{
// Constructor
public AOE()
{
type = "RTS";
name = "AOE3";
version = "1.0";
price = 250;
}
public override void Demo()
{
Console.WriteLine(" Create " + this.GetType().Name + " Game " + "And the Game's version is " + version);
}
}
//实作物件2
public class CSGO : Game
{
// Constructor
public CSGO()
{
type = "FPS";
name = "CSGO";
version = "1.0";
price = 200;
}
public override void Demo()
{
Console.WriteLine(" Create " + this.GetType().Name + " Game " + "And the Game's version is " + version);
}
}
public class MainProgram
{
public static void Main(string[] args)
{
GameFactory factory = new GameFactory();
string factory1 = "Ensemble";
Game aoe = factory.GetGame(factory1);
aoe.version = "2.0";
aoe.Demo();
string factory2 = "VALVE";
Game csgo = factory.GetGame(factory2);
csgo.version = "2.0";
csgo.Demo();
}
}
}
参考资料:
https://www.dofactory.com/net/flyweight-design-pattern
<<: 【C#】Behavioral Patterns Mediator Mode
今天来做选单啦 先来看一下 Vuetify 提供的 Menus组件,他这个比较适合选单复杂的程序, ...
为什麽标题会是这个呢,说来话长, 我开启永丰证券的文件阅读时, 看到下一个是ticks API,我瞬...
Figma 的介面布局与一般的设计软件很类似,上方(1)为工具列,左方(2)可切换 Layer, P...
人工智慧7 前言 系列文章简介 大家好,我们是 AI . FREE Team - 人工智慧自由团队,...
在学习前端的过程,有做过真实需求的网站,会是珍贵的经验。而并不是时时刻刻都会有充足的案子,带每一个...