ผลต่างระหว่างรุ่นของ "Prg2/design patterns 1"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 71: แถว 71:
  
 
=== Observer Pattern (functions) ===
 
=== Observer Pattern (functions) ===
 +
{{synfile|gamelib.py}}
 +
<syntaxhighlight lang="python">
 +
class GameApp(ttk.Frame):
 +
    def __init__(self, parent, canvas_width=800, canvas_height=500, update_delay=33):
 +
        # ...
 +
 +
        self.on_key_pressed_handlers = []
 +
 +
    # ...
 +
 +
    def register_on_key_pressed_handler(self, f):
 +
        self.on_key_pressed_handlers.append(f)
 +
 +
    def on_key_pressed(self, event):
 +
        for f in self.on_key_pressed_handlers:
 +
            f(event)
 +
</syntaxhighlight>
 +
 +
{{synfile|monkeys.py}}
 +
<syntaxhighlight lang="python">
 +
class MonkeyGame(GameApp):
 +
    def handle_speed_adjustment_key_pressed(self, event):
 +
        if event.char == '+':
 +
            if self.speed < 10:
 +
                self.speed += 1
 +
                self.update_speed_text()
 +
               
 +
        if event.char == '-':
 +
            if self.speed > 1:
 +
                self.speed -= 1
 +
                self.update_speed_text()
 +
 +
    def handle_banana_throwing_key_pressed(self, event):
 +
        if event.char == ' ':
 +
            if not self.banana.is_moving:
 +
                self.banana.set_speed(3 * self.speed, 5 * self.speed)
 +
                self.banana.reset()
 +
                self.banana.start()
 +
    # ...
 +
 +
    def init_game(self):
 +
        # ...
 +
 +
        self.register_on_key_pressed_handler(self.handle_speed_adjustment_key_pressed)
 +
        self.register_on_key_pressed_handler(self.handle_banana_throwing_key_pressed)
 +
</syntaxhighlight>
  
 
=== Command Pattern ===
 
=== Command Pattern ===
  
 
=== State Pattern ===
 
=== State Pattern ===

รุ่นแก้ไขเมื่อ 23:39, 15 มีนาคม 2564

This is part of Programming 2 2563

Basic information

Codes

Observer Pattern (OO version)

File: gamelib.py
class GameApp(ttk.Frame): 
    def __init__(self, parent, canvas_width=800, canvas_height=500, update_delay=33):
        # ...
        self.on_key_pressed_observers = []
    
    # ...
 
    def register_on_key_pressed_observer(self, observer):
        self.on_key_pressed_observers.append(observer)

    def on_key_pressed(self, event):
        for observer in self.on_key_pressed_observers:
            observer.notify(event)
File: monkeys.py
class MonkeyGame(GameApp):
    class AppObserver:
        def __init__(self, app):
            self.app = app

    class SpeedAdjustmentObserver(AppObserver):
        def notify(self, event):
            app = self.app
            if event.char == '+':
                if app.speed < 10:
                    app.speed += 1
                    app.update_speed_text()
                    
            if event.char == '-':
                if app.speed > 1:
                    app.speed -= 1
                    app.update_speed_text()

    class BananaThrowingObserver(AppObserver):
        def notify(self, event):
            app = self.app
            if event.char == ' ':
                if not app.banana.is_moving:
                    app.banana.set_speed(3 * app.speed, 5 * app.speed)
                    app.banana.reset()
                    app.banana.start()

    # ...
        
    def init_game(self):
        # ...

        self.register_on_key_pressed_observer(MonkeyGame.SpeedAdjustmentObserver(self))
        self.register_on_key_pressed_observer(MonkeyGame.BananaThrowingObserver(self))

Observer Pattern (functions)

File: gamelib.py
class GameApp(ttk.Frame): 
    def __init__(self, parent, canvas_width=800, canvas_height=500, update_delay=33):
        # ...

        self.on_key_pressed_handlers = []

    # ...

    def register_on_key_pressed_handler(self, f):
        self.on_key_pressed_handlers.append(f)

    def on_key_pressed(self, event):
        for f in self.on_key_pressed_handlers:
            f(event)
File: monkeys.py
class MonkeyGame(GameApp):
    def handle_speed_adjustment_key_pressed(self, event):
        if event.char == '+':
            if self.speed < 10:
                self.speed += 1
                self.update_speed_text()
                
        if event.char == '-':
            if self.speed > 1:
                self.speed -= 1
                self.update_speed_text()

    def handle_banana_throwing_key_pressed(self, event):
        if event.char == ' ':
            if not self.banana.is_moving:
                self.banana.set_speed(3 * self.speed, 5 * self.speed)
                self.banana.reset()
                self.banana.start()
    # ...

    def init_game(self):
        # ...

        self.register_on_key_pressed_handler(self.handle_speed_adjustment_key_pressed)
        self.register_on_key_pressed_handler(self.handle_banana_throwing_key_pressed)

Command Pattern

State Pattern