游戏开发中的设计模式

单例模式

实例化单一对象,懒加载

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)
相关推荐
geovindu14 小时前
go: Strategy Pattern
开发语言·设计模式·golang·策略模式
嵌入式学习_force20 小时前
02_state
设计模式·蓝牙
qcx231 天前
Warp源码深度解析(七):Token预算策略——双轨计费、上下文溢出与摘要压缩
人工智能·设计模式·rust·wrap
Cosolar2 天前
提示词工程面试题系列 - Zero-Shot Prompting 和 Few-Shot Prompting 的核心区别是什么?
人工智能·设计模式·架构
geovindu2 天前
go:Template Method Pattern
开发语言·后端·设计模式·golang·模板方法模式
钝挫力PROGRAMER2 天前
贫血模型的改进
java·开发语言·设计模式·架构
qcx232 天前
Warp源码深度解析(二):自研GPU UI框架——WarpUI的ECH模式与渲染管线
人工智能·ui·设计模式·rust
qcx232 天前
Warp源码深度解析(三):Block-Based终端引擎——Grid模型、PTY与Shell Integration
人工智能·设计模式·架构·wrap
mounter6252 天前
Linux Kernel Design Patterns (Part 2):从经典链表到现代 XArray,拆解内核复杂数据结构的设计哲学
linux·数据结构·链表·设计模式·内存管理·kernel
rrr22 天前
【PyQt5】| 多线程设计模式
开发语言·qt·设计模式