代码拉取完成,页面将自动刷新
// 语言、字体类型
interface ICharacterInterface {
index: number;
language: ILanguage;
fontFamily: IFontFamily;
}
interface ILanguage {
type: string;
name: string;
}
interface IFontFamily {
fontFamily: FontFamilyEnum;
}
export enum FontFamilyEnum {
Song = "宋体",
Hei = "黑体"
}
// 默认构建方法
class LanguageClass implements ILanguage {
public type: string;
public name: string;
constructor(type: string, name: string) {
this.type = type;
this.name = name;
}
}
class FontFamilyClass implements IFontFamily {
public fontFamily: FontFamilyEnum;
constructor(type: FontFamilyEnum) {
this.fontFamily = type;
}
}
// 基础类
class CharacterRegistry {
private static _instance: CharacterRegistry;
private _language: LanguageClass;
private _fontFamilyMap: Map<FontFamilyEnum, IFontFamily> = new Map();
public static get instance() {
if (!this._instance) {
this._instance = new CharacterRegistry();
}
return this._instance;
}
public get language() {
if (!this._language) {
// 懒汉策略,延时创建对象
this._language = new LanguageClass("Chinese", "中文");
}
return this._language;
}
public getFontFamilyByType(type: FontFamilyEnum) {
// 懒汉策略,延时创建对象
if (!this._fontFamilyMap.has(type)) {
this._fontFamilyMap.set(type, new FontFamilyClass(type));
}
return this._fontFamilyMap.get(type);
}
}
class FlyweightCharacterClass implements ICharacterInterface {
public language: ILanguage = CharacterRegistry.instance.language; // 内部共享属性
public index: number;
public fontFamily: IFontFamily;
constructor(index: number, type: FontFamilyEnum) {
this.index = index; // 外部属性
this.fontFamily = CharacterRegistry.instance.getFontFamilyByType(type);
}
}
class SaveCharacters {
public static flyweightWay() {
const charactersList: ICharacterInterface[] = [];
for (let i = 0; i < 200000; i++) {
// 模拟随机生成类型
const type =
Math.random() > 0.5 ? FontFamilyEnum.Song : FontFamilyEnum.Hei;
charactersList.push(new FlyweightCharacterClass(i, type));
}
return charactersList;
}
}
const flyweightWayCharacters = SaveCharacters.flyweightWay();
console.log(flyweightWayCharacters);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。