国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python之常用設(shè)計模式

這篇具有很好參考價值的文章主要介紹了Python之常用設(shè)計模式。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1、 設(shè)計模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

2、接口

Python之常用設(shè)計模式

interface.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/12/1

# class Payment:
#     def pay(self, money):
#         raise NotImplementedError

from abc import ABCMeta, abstractmethod


# 接口
# class Payment(metaclass=ABCMeta):
#     # abstract class
#     @abstractmethod
#     def pay(self, money):
#         pass
#
#
# class Alipay(Payment):
#     def pay(self, money):
#         print("支付寶支付%d元." % money)
#
#
# class WechatPay(Payment):
#     def pay(self, money):
#         print("微信支付%d元." % money)
#
#
#
# p = WechatPay()
# p.pay(100)

#
# class User:
#     def show_name(self):
#         pass
#
# class VIPUser(User):
#     def show_name(self):
#         pass
#
# def show_user(u):
#     res = u.show_name()

class LandAnimal(metaclass=ABCMeta):
    @abstractmethod
    def walk(self):
        pass


class WaterAnimal(metaclass=ABCMeta):
    @abstractmethod
    def swim(self):
        pass


class SkyAnimal(metaclass=ABCMeta):
    @abstractmethod
    def fly(self):
        pass


class Tiger(LandAnimal):
    def walk(self):
        print("老虎走路")


class Frog(LandAnimal, WaterAnimal):
    pass

3、面向?qū)ο笤O(shè)計SOLID原則

Python之常用設(shè)計模式
Python之常用設(shè)計模式

4、設(shè)計模式分類

Python之常用設(shè)計模式

5、創(chuàng)建型模式

1)簡單工廠模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

factory.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/12/1

from abc import ABCMeta, abstractmethod


class Payment(metaclass=ABCMeta):
    # abstract class
    @abstractmethod
    def pay(self, money):
        pass

class Alipay(Payment):
    def __init__(self, use_huabei=False):
        self.use_huaei = use_huabei

    def pay(self, money):
        if self.use_huaei:
            print("花唄支付%d元." % money)
        else:
            print("支付寶余額支付%d元." % money)


class WechatPay(Payment):
    def pay(self, money):
        print("微信支付%d元." % money)


class PaymentFactory:
    def create_payment(self, method):
        if method == 'alipay':
            return Alipay()
        elif method == 'wechat':
            return WechatPay()
        elif method == 'huabei':
            return Alipay(use_huabei=True)
        else:
            raise TypeError("No such payment named %s" % method)



# client
pf = PaymentFactory()
p = pf.create_payment('huabei')
p.pay(100)

2)工廠方法模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

factory_method.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/12/1

from abc import ABCMeta, abstractmethod


class Payment(metaclass=ABCMeta):
    # abstract class
    @abstractmethod
    def pay(self, money):
        pass


class Alipay(Payment):
    def __init__(self, use_huabei=False):
        self.use_huaei = use_huabei

    def pay(self, money):
        if self.use_huaei:
            print("花唄支付%d元." % money)
        else:
            print("支付寶余額支付%d元." % money)


class WechatPay(Payment):
    def pay(self, money):
        print("微信支付%d元." % money)


class BankPay(Payment):
    def pay(self, money):
        print("銀行卡支付%d元." % money)


class PaymentFactory(metaclass=ABCMeta):
    @abstractmethod
    def create_payment(self):
        pass


class AlipayFactory(PaymentFactory):
    def create_payment(self):
        return Alipay()


class WechatPayFactory(PaymentFactory):
    def create_payment(self):
        return WechatPay()


class HuabeiFactory(PaymentFactory):
    def create_payment(self):
        return Alipay(use_huabei=True)


class BankPayFactory(PaymentFactory):
    def create_payment(self):
        return BankPay()


# client

pf = HuabeiFactory()
p = pf.create_payment()
p.pay(100)


3)抽象工廠模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式
Python之常用設(shè)計模式

abstract_factory.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/12/1

from abc import abstractmethod, ABCMeta


# ------抽象產(chǎn)品------

class PhoneShell(metaclass=ABCMeta):
    @abstractmethod
    def show_shell(self):
        pass


class CPU(metaclass=ABCMeta):
    @abstractmethod
    def show_cpu(self):
        pass


class OS(metaclass=ABCMeta):
    @abstractmethod
    def show_os(self):
        pass


# ------抽象工廠------

class PhoneFactory(metaclass=ABCMeta):
    @abstractmethod
    def make_shell(self):
        pass

    @abstractmethod
    def make_cpu(self):
        pass

    @abstractmethod
    def make_os(self):
        pass


# ------具體產(chǎn)品------


class SmallShell(PhoneShell):
    def show_shell(self):
        print("普通手機小手機殼")


class BigShell(PhoneShell):
    def show_shell(self):
        print("普通手機大手機殼")


class AppleShell(PhoneShell):
    def show_shell(self):
        print("蘋果手機殼")


class SnapDragonCPU(CPU):
    def show_cpu(self):
        print("驍龍CPU")


class MediaTekCPU(CPU):
    def show_cpu(self):
        print("聯(lián)發(fā)科CPU")


class AppleCPU(CPU):
    def show_cpu(self):
        print("蘋果CPU")


class Android(OS):
    def show_os(self):
        print("Android系統(tǒng)")


class IOS(OS):
    def show_os(self):
        print("iOS系統(tǒng)")


# ------具體工廠------


class MiFactory(PhoneFactory):
    def make_cpu(self):
        return SnapDragonCPU()

    def make_os(self):
        return Android()

    def make_shell(self):
        return BigShell()


class HuaweiFactory(PhoneFactory):
    def make_cpu(self):
        return MediaTekCPU()

    def make_os(self):
        return Android()

    def make_shell(self):
        return SmallShell()


class IPhoneFactory(PhoneFactory):
    def make_cpu(self):
        return AppleCPU()

    def make_os(self):
        return IOS()

    def make_shell(self):
        return AppleShell()


# ------客戶端------


class Phone:
    def __init__(self, cpu, os, shell):
        self.cpu = cpu
        self.os = os
        self.shell = shell

    def show_info(self):
        print("手機信息:")
        self.cpu.show_cpu()
        self.os.show_os()
        self.shell.show_shell()



def make_phone(factory):
    cpu = factory.make_cpu()
    os = factory.make_os()
    shell = factory.make_shell()
    return Phone(cpu, os, shell)


p1 = make_phone(IPhoneFactory())
p1.show_info()

4)建造者模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

builder.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/12/1

from abc import ABCMeta, abstractmethod

class Player:
    def __init__(self, face=None, body=None, arm=None, leg=None):
        self.face = face
        self.body = body
        self.arm = arm
        self.leg = leg

    def __str__(self):
        return "%s, %s, %s, %s" % (self.face, self.body, self.arm, self.leg)


class PlayerBuilder(metaclass=ABCMeta):
    @abstractmethod
    def build_face(self):
        pass

    @abstractmethod
    def build_body(self):
        pass

    @abstractmethod
    def build_arm(self):
        pass

    @abstractmethod
    def build_leg(self):
        pass


class SexyGirlBuilder(PlayerBuilder):
    def __init__(self):
        self.player = Player()

    def build_face(self):
        self.player.face = "漂亮臉蛋"

    def build_body(self):
        self.player.body = "苗條"

    def build_arm(self):
        self.player.arm = "漂亮胳膊"

    def build_leg(self):
        self.player.leg = "大長腿"


class Monster(PlayerBuilder):
    def __init__(self):
        self.player = Player()

    def build_face(self):
        self.player.face = "怪獸臉"

    def build_body(self):
        self.player.body = "怪獸身材"

    def build_arm(self):
        self.player.arm = "長毛的胳膊"

    def build_leg(self):
        self.player.leg = "長毛的腿"


class PlayerDirector: # 控制組裝順序
    def build_player(self, builder):
        builder.build_body()
        builder.build_face()
        builder.build_arm()
        builder.build_leg()
        return builder.player


# client

builder = Monster()
director = PlayerDirector()
p = director.build_player(builder)
print(p)

5)單例模式

Python之常用設(shè)計模式

singleton.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/12/1

from abc import abstractmethod, ABCMeta

class Singleton:
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance


class MyClass(Singleton):
    def __init__(self, a):
        self.a = a


a = MyClass(10)
b = MyClass(20)

print(a.a)
print(b.a)
print(id(a), id(b))


6)創(chuàng)建型模式小結(jié)

Python之常用設(shè)計模式

6、結(jié)構(gòu)型模式

1)適配器模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

adapter.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/12/1

from abc import ABCMeta, abstractmethod


class Payment(metaclass=ABCMeta):
    # abstract class
    @abstractmethod
    def pay(self, money):
        pass


class Alipay(Payment):
    def pay(self, money):
        print("支付寶支付%d元." % money)


class WechatPay(Payment):
    def pay(self, money):
        print("微信支付%d元." % money)



class BankPay:
    def cost(self, money):
        print("銀聯(lián)支付%d元." % money)


class ApplePay:
    def cost(self, money):
        print("蘋果支付%d元." % money)


# # 類適配器
# class NewBankPay(Payment, BankPay):
#     def pay(self, money):
#         self.cost(money)


# 對象適配器
class PaymentAdapter(Payment):
    def __init__(self, payment):
        self.payment = payment

    def pay(self, money):
        self.payment.cost(money)


p = PaymentAdapter(BankPay())
p.pay(100)


# 組合

# class A:
#     pass
#
# class B:
#     def __init__(self):
#         self.a = A()

2)橋模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式
Python之常用設(shè)計模式

bridge.py

# coding: utf-8
# author: ztypl
# date:   2018/12/20

from abc import ABCMeta, abstractmethod

class Shape(metaclass=ABCMeta):
    def __init__(self, color):
        self.color = color

    @abstractmethod
    def draw(self):
        pass


class Color(metaclass=ABCMeta):
    @abstractmethod
    def paint(self, shape):
        pass


class Rectangle(Shape):
    name = "長方形"
    def draw(self):
        # 長方形邏輯
        self.color.paint(self)


class Circle(Shape):
    name = "圓形"
    def draw(self):
        # 圓形邏輯
        self.color.paint(self)


class Line(Shape):
    name = "直線"
    def draw(self):
        # 直線邏輯
        self.color.paint(self)


class Red(Color):
    def paint(self, shape):
        print("紅色的%s" % shape.name)


class Green(Color):
    def paint(self, shape):
        print("綠色的%s" % shape.name)


class Blue(Color):
    def paint(self, shape):
        print("藍(lán)色的%s" % shape.name)



shape = Line(Blue())
shape.draw()

shape2 = Circle(Green())
shape2.draw()

3)組合模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

composite.py

# coding: utf-8
# author: ztypl
# date:   2018/12/21

from abc import ABCMeta, abstractmethod


# 抽象組件
class Graphic(metaclass=ABCMeta):
    @abstractmethod
    def draw(self):
        pass


# 葉子組件
class Point(Graphic):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return "點(%s, %s)" % (self.x, self.y)

    def draw(self):
        print(str(self))


# 葉子組件
class Line(Graphic):
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2

    def __str__(self):
        return "線段[%s, %s]" % (self.p1, self.p2)

    def draw(self):
        print(str(self))


# 復(fù)合組件
class Picture(Graphic):
    def __init__(self, iterable):
        self.children = []
        for g in iterable:
            self.add(g)

    def add(self, graphic):
        self.children.append(graphic)


    def draw(self):
        print("------復(fù)合圖形------")
        for g in self.children:
            g.draw()
        print("------復(fù)合圖形------")


p1 = Point(2,3)
l1 = Line(Point(3,4), Point(6,7))
l2 = Line(Point(1,5), Point(2,8))
pic1 = Picture([p1, l1, l2])

p2 = Point(4,4)
l3 = Line(Point(1,1), Point(0,0))
pic2 = Picture([p2, l3])

pic = Picture([pic1, pic2])
pic.draw()


4)外觀模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

facade.py

# coding: utf-8
# author: ztypl
# date:   2018/12/26


class CPU:
    def run(self):
        print("CPU開始運行")

    def stop(self):
        print("CPU停止運行")


class Disk:
    def run(self):
        print("硬盤開始工作")

    def stop(self):
        print("硬盤停止工作")


class Memory:
    def run(self):
        print("內(nèi)存通電")

    def stop(self):
        print("內(nèi)存斷電")


class Computer: # Facade
    def __init__(self):
        self.cpu = CPU()
        self.disk = Disk()
        self.memory = Memory()

    def run(self):
        self.cpu.run()
        self.disk.run()
        self.memory.run()

    def stop(self):
        self.cpu.stop()
        self.disk.stop()
        self.memory.stop()


# Client

computer = Computer()
computer.run()
computer.stop()

5)代理模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

proxy.py

# coding: utf-8
# author: ztypl
# date:   2018/12/26

from abc import ABCMeta, abstractmethod

class Subject(metaclass=ABCMeta):
    @abstractmethod
    def get_content(self):
        pass

    @abstractmethod
    def set_content(self, content):
        pass


class RealSubject(Subject):
    def __init__(self, filename):
        self.filename = filename
        f = open(filename, 'r', encoding='utf-8')
        print("讀取文件內(nèi)容")
        self.content = f.read()
        f.close()

    def get_content(self):
        return self.content

    def set_content(self, content):
        f = open(self.filename, 'w', encoding='utf-8')
        f.write(content)
        f.close()





class VirtualProxy(Subject):
    def __init__(self, filename):
        self.filename = filename
        self.subj = None

    def get_content(self):
        if not self.subj:
            self.subj = RealSubject(self.filename)
        return self.subj.get_content()


    def set_content(self, content):
        if not subj:
            self.subj = RealSubject(self.filename)
        return self.subj.set_content(content)



class ProtectedProxy(Subject):
    def __init__(self, filename):
        self.subj = RealSubject(filename)

    def get_content(self):
        return self.subj.get_content()

    def set_content(self, content):
        raise PermissionError("無寫入權(quán)限")



#subj = RealSubject("test.txt")
#subj.get_content()

subj = ProtectedProxy("test.txt")
print(subj.get_content())
subj.set_content("abc")

7、行為型模式

Python之常用設(shè)計模式

1)責(zé)任鏈模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

chain_of_responsibility.py

# coding: utf-8
# author: ztypl
# date:   2018/12/26

from abc import ABCMeta, abstractmethod

class Handler(metaclass=ABCMeta):
    @abstractmethod
    def handle_leave(self, day):
        pass


class GeneralManager(Handler):
    def handle_leave(self, day):
        if day <= 10:
            print("總經(jīng)理準(zhǔn)假%d天" % day)
        else:
            print("你還是辭職吧")


class DepartmentManager(Handler):
    def __init__(self):
        self.next = GeneralManager()

    def handle_leave(self, day):
        if day <= 5:
            print("部門經(jīng)理準(zhǔn)假%s天" % day)
        else:
            print("部門經(jīng)理職權(quán)不足")
            self.next.handle_leave(day)


class ProjectDirector(Handler):
    def __init__(self):
        self.next = DepartmentManager()

    def handle_leave(self, day):
        if day <= 3:
            print("項目主管準(zhǔn)假%d天" % day)
        else:
            print("項目主管職權(quán)不足")
            self.next.handle_leave(day)

# Client


day = 12
h = ProjectDirector()
h.handle_leave(day)

2)觀察者模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

observer.py

# coding: utf-8
# author: ztypl
# date:   2018/12/27

from abc import ABCMeta, abstractmethod


class Observer(metaclass=ABCMeta): # 抽象訂閱者
    @abstractmethod
    def update(self, notice): # notice 是一個Notice類的對象
        pass


class Notice:  # 抽象發(fā)布者
    def __init__(self):
        self.observers = []

    def attach(self, obs):
        self.observers.append(obs)

    def detach(self, obs):
        self.observers.remove(obs)

    def notify(self): # 推送
        for obs in self.observers:
            obs.update(self)


class StaffNotice(Notice): # 具體發(fā)布者
    def __init__(self, company_info=None):
        super().__init__()
        self.__company_info = company_info

    @property
    def company_info(self):
        return self.__company_info

    @company_info.setter
    def company_info(self, info):
        self.__company_info = info
        self.notify() # 推送


class Staff(Observer):
    def __init__(self):
        self.company_info = None

    def update(self, notice):
        self.company_info = notice.company_info


# Client

notice = StaffNotice("初始公司信息")
s1 = Staff()
s2 = Staff()
notice.attach(s1)
notice.attach(s2)
notice.company_info = "公司今年業(yè)績非常好,給大家發(fā)獎金?。。?
print(s1.company_info)
print(s2.company_info)
notice.detach(s2)
notice.company_info = "公司明天放假?。?!"
print(s1.company_info)
print(s2.company_info)

3)策略模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

strategy.py

# coding: utf-8
# author: ztypl
# date:   2018/12/27

from abc import ABCMeta,abstractmethod

class Strategy(metaclass=ABCMeta):
    @abstractmethod
    def execute(self, data):
        pass


class FastStrategy(Strategy):
    def execute(self, data):
        print("用較快的策略處理%s" % data)


class SlowStrategy(Strategy):
    def execute(self, data):
        print("用較慢的策略處理%s" % data)


class Context:
    def __init__(self, strategy, data):
        self.data = data
        self.strategy = strategy

    def set_strategy(self, strategy):
        self.strategy = strategy

    def do_strategy(self):
        self.strategy.execute(self.data)


# Client

data = "[...]"
s1 = FastStrategy()
s2 = SlowStrategy()
context = Context(s1, data)
context.do_strategy()
context.set_strategy(s2)
context.do_strategy()

4)模板方法模式

Python之常用設(shè)計模式
Python之常用設(shè)計模式

template_method.py

# coding: utf-8
# author: ztypl
# date:   2018/12/27

from abc import ABCMeta, abstractmethod
from time import sleep


class Window(metaclass=ABCMeta):
    @abstractmethod
    def start(self):
        pass

    @abstractmethod
    def repaint(self):
        pass

    @abstractmethod
    def stop(self): # 原子操作/鉤子操作
        pass

    def run(self):  # 模板方法
        self.start()
        while True:
            try:
                self.repaint()
                sleep(1)
            except KeyboardInterrupt:
                break
        self.stop()


class MyWindow(Window):
    def __init__(self, msg):
        self.msg = msg

    def start(self):
        print("窗口開始運行")

    def stop(self):
        print("窗口結(jié)束運行")

    def repaint(self):
        print(self.msg)


MyWindow("Hello...").run()

8、視頻鏈接

【Python之常用設(shè)計模式-嗶哩嗶哩】 https://b23.tv/P8D78qx文章來源地址http://www.zghlxwxcb.cn/news/detail-430897.html

到了這里,關(guān)于Python之常用設(shè)計模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 設(shè)計模式——接口隔離原則

    設(shè)計模式——接口隔離原則

    客戶端不應(yīng)該依賴它不需要的接口,即一個類對另一個類的依賴應(yīng)該建立在最小的接口上 先看一張圖: 類 A 通過接口 Interface1 依賴類 B,類 C 通過接口 Interface1 依賴類 D,如果接口 Interface1 對于類 A 和類 C來說不是最小接口,那么類 B 和類 D 必須去實現(xiàn)他們不需要的方法。 按隔

    2024年02月11日
    瀏覽(25)
  • 設(shè)計模式_spring框架中常用的8種設(shè)計模式

    設(shè)計模式_spring框架中常用的8種設(shè)計模式

    spring框架中常用到的8種設(shè)計模式清單如下: 設(shè)計模式 使用地方 備注 工廠模式 BeanFactory ApplicationContext 單例模式 Spring中的Bean 代理模式 Spring AOP java反射實現(xiàn)動態(tài)代理 模板方法模式 Spring中以Template結(jié)尾的類 使用繼承的方式實現(xiàn) 觀察者模式 Spring事件驅(qū)動模型 適配器模式 Spri

    2023年04月08日
    瀏覽(21)
  • 什么是設(shè)計模式?常用的設(shè)計有哪些?

    單例模式 工廠模式 代理模式(proxy) 設(shè)計模式是前輩們經(jīng)過無數(shù)次實踐所總結(jié)的一些方法(針對特定問題的特定方法) 這些設(shè)計模式中的方法都是經(jīng)過反復(fù)使用過的。 1、單例模式(懶漢式、餓漢式) 步驟: 1、構(gòu)造方法私有化,讓除了自己類能創(chuàng)建,其他類都不能創(chuàng)建。

    2024年02月13日
    瀏覽(26)
  • 前端常用設(shè)計模式

    在上述案例中, UserInfoComponent 負(fù)責(zé)兩個職責(zé):顯示用戶信息和發(fā)送請求獲取用戶信息。如果我們將發(fā)送請求的邏輯提取到一個單獨的服務(wù)或鉤子函數(shù)中,可以更好地遵守單一職責(zé)原則。 在上述案例中,主題切換功能違反了開放封閉原則,因為每次添加新的主題都需要修改切

    2024年02月10日
    瀏覽(16)
  • 常用設(shè)計模式

    游戲開發(fā)中常用的設(shè)計模式有很多種,以下是一些常見的設(shè)計模式: 1. 單例模式(Singleton Pattern):用于創(chuàng)建一個全局唯一的對象實例,比如游戲中的資源管理器或者游戲設(shè)置。 2. 工廠模式(Factory Pattern):用于創(chuàng)建對象的過程抽象,可以根據(jù)參數(shù)的不同返回不同類型的對象

    2024年02月09日
    瀏覽(16)
  • 【常用設(shè)計模式】待補充

    【常用設(shè)計模式】待補充

    Github倉庫地址 23中設(shè)計模型分為常見的三大類:創(chuàng)建型模式、結(jié)構(gòu)型模式和行為型模式 描述 簡單工廠模式不是23中設(shè)計模式中的。簡單工廠模式不直接向客戶端暴露對象創(chuàng)建的細(xì)節(jié),而是通過一個工廠類來負(fù)責(zé)創(chuàng)建產(chǎn)品類的實例 角色 抽象產(chǎn)品角色:給具體產(chǎn)品角色提供接口

    2024年02月08日
    瀏覽(21)
  • 常用的設(shè)計模式

    游戲開發(fā)中常用的設(shè)計模式有很多種,以下是一些常見的設(shè)計模式: 1. 單例模式(Singleton Pattern):用于創(chuàng)建一個全局唯一的對象實例,比如游戲中的資源管理器或者游戲設(shè)置。 2. 工廠模式(Factory Pattern):用于創(chuàng)建對象的過程抽象,可以根據(jù)參數(shù)的不同返回不同類型的對象

    2024年02月09日
    瀏覽(16)
  • 前端常用的設(shè)計模式

    設(shè)計模式:是一種抽象的編程思想,并不局限于某一特定的編程語言,而是在許多語言之間是相通的;它是軟件設(shè)計中常見的問題的通用、可反復(fù)使用、多少人知曉的一種解決方案或者模板。一般對與從事過面向?qū)ο缶幊痰娜藖碚f會更熟悉一些。 設(shè)計模式的意義:指導(dǎo)我們?nèi)?/p>

    2024年01月24日
    瀏覽(25)
  • 前端常用設(shè)計模式初探

    設(shè)計模式一直是程序員談?wù)摰摹案叨恕痹掝}之一,總有一種敬而遠(yuǎn)之的心態(tài)。在了解后才知道在將函數(shù)作為一等對象的語言中,有許多需要利用對象多態(tài)性的設(shè)計模式,比如單例模式、 策略模式等,這些模式的結(jié)構(gòu)與傳統(tǒng)面向?qū)ο笳Z言的結(jié)構(gòu)大相徑庭,實際上已經(jīng)融入到了語

    2024年02月05日
    瀏覽(16)
  • PHP常用六大設(shè)計模式

    特點 三私一公 :私有的靜態(tài)變量(存放實例),私有的構(gòu)造方法(防止創(chuàng)建實例),私有的克隆方法(防止克隆對象),公有的靜態(tài)方法(對外界提供實例) 應(yīng)用場景 程序應(yīng)用中,涉及到數(shù)據(jù)庫操作時,如果每次操作的時候連接數(shù)據(jù)庫,會帶來大量的資源消耗??梢酝ㄟ^單例

    2024年02月09日
    瀏覽(89)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包