游戏开发中的设计模式

单例模式

实例化单一对象,懒加载

typescript 复制代码
//单例模式
class GameManagerSingleton
{
    private constructor(){}
    private static instance:GameManagerSingleton;
    public static Instance()
    {
        if(!GameManagerSingleton.instance)
        {
            this.instance = new GameManagerSingleton();
        }
        return this.instance;
    }


    Init(){}

}

GameManagerSingleton.Instance().Init();

代理模式

根据不同的代理者,具体化实际方法操作

typescript 复制代码
//代理模式
interface ICall
{
    DealNum(x1:number,x2:number):number;
}

class NPC1 implements ICall
{
    DealNum(x1: number, x2: number): number {
        return x1 + x2;
    }
}

class NPC2 implements ICall
{
    DealNum(x1: number, x2: number): number {
        return x1 - x2;
    }
}

class Person
{
    public delegate : ICall;
    public DealNum(x1,x2) : number
    {
        let retNum = this.delegate.DealNum(x1,x2);
        return retNum;
    }
}

观察者模式

设置观察者后,对象的变更会告知观察者

typescript 复制代码
//观察者模式
interface IObserver
{
    nameChanged(newName);
}

class Test implements IObserver
{
  //捕捉变化
    nameChanged(newName : string) 
    {
        console.log("NewName:"+newName)
    }
}

class People
{
    private name : string;
    /**
     *
     */
    constructor() {
        this.name = ""
    }
    public get Name()
    {
       return this.name;
    }

    public set Name(value)
    {
        if(this.name != value)
        {
            this.name = value;
            //发生变化会向观察者发消息
            for(let observer of this.Observers)
            {
                observer.nameChanged(this.name);
            }
        }
        
    }
    Observers : Array<IObserver> = new Array<IObserver>();
}



let People1 = new People()
let observer1  = new Test()
//设置观察者
People1.Observers.push(observer1)
//改变对象
People1.Name = "Tom"

工厂模式

游戏中构建item群体的常用模式

typescript 复制代码
enum CarType
{
    BMW,
    Benz,
    Audi,
}


class Car 
{
   name : string;
   static CreateCar(type :  CarType)
    {
        switch(type)
        {
            case CarType.BMW:
                return new BMW();
            case CarType.Benz:
                return new Benz();
        }
    }
}

class BMW extends Car
{
    /**
     *
     */
    constructor() {
        super();
        this.name = "BMW"
    }
}
class Benz extends Car
{
    /**
     *
     */
    constructor() {
        super();
        this.name = "Benz"
    }
}

Car.CreateCar(CarType.Benz)
Car.CreateCar(CarType.BMW)
相关推荐
2302_805771075 小时前
Day23:POM设计模式(封装)
java·开发语言·设计模式
范中勤1 天前
技术文档:模型工厂 `llm_provider` / `llm_use` 分层设计
设计模式·langchain·架构设计·模型工厂·llm_provider
浅尝辄止;1 天前
设计模式在HIS/LIS医疗系统中的完整实践指南(C#版)
开发语言·设计模式·c#
范中勤1 天前
适配器模式(Adapter Pattern):原理、场景与 LLM 多厂商工程落地
设计模式·架构·langchain·适配器模式·多厂商
408高手2 天前
23种设计模式
设计模式
沫璃染墨2 天前
《Linux工程实践篇(二):线程池实战——从线程安全单例到完整源码实现》
linux·开发语言·c++·单例模式·设计模式·系统架构
sarasuki2 天前
MCP 为什么是一个协议而不是一个框架呢?
设计模式·agent·mcp
geovindu2 天前
rust: Borg Pattern(续)
开发语言·后端·设计模式·rust·博格模式
染指11103 天前
140.Agent-多Agent框架-Agent执行Skills流程
数据库·人工智能·设计模式·langchain·agents
sarasuki3 天前
Agent 是怎么做错误处理的呢?
人工智能·设计模式·agent