Created
May 27, 2025 09:27
-
-
Save Jeihunn/ef09d1470606608f8a11781fddb597d5 to your computer and use it in GitHub Desktop.
Python-da Prototype dizayn nümunəsi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
from abc import ABC, abstractmethod | |
# Abstract class defining the prototype interface | |
class Character(ABC): | |
def __init__(self, name="", weapon="", armor="", ability=""): | |
self.name = name | |
self.weapon = weapon | |
self.armor = armor | |
self.ability = ability | |
@abstractmethod | |
def clone(self): | |
pass | |
def __str__(self): | |
return ( | |
f"Character(name={self.name}, weapon={self.weapon}, " | |
f"armor={self.armor}, ability={self.ability})" | |
) | |
# Concrete prototype: Warrior | |
class Warrior(Character): | |
def __init__(self): | |
super().__init__( | |
name="Warrior", weapon="sword", armor="heavy armor", ability="strength" | |
) | |
def clone(self): | |
# Creates a deep copy to ensure independence from the original | |
return copy.deepcopy(self) | |
# Concrete prototype: Mage | |
class Mage(Character): | |
def __init__(self): | |
super().__init__(name="Mage", weapon="staff", armor="robe", ability="fireball") | |
def clone(self): | |
# Creates a deep copy to ensure independence from the original | |
return copy.deepcopy(self) | |
# Registry to store and manage prototype characters | |
class CharacterRegistry: | |
def __init__(self): | |
self._characters = {} | |
def add_character(self, name, character): | |
# Adds a prototype to the registry | |
self._characters[name] = character | |
def get_character(self, name): | |
# Returns a cloned instance of the requested prototype | |
if name in self._characters: | |
return self._characters[name].clone() | |
return None | |
# Client code demonstrating the usage | |
if __name__ == "__main__": | |
# Create a registry | |
registry = CharacterRegistry() | |
# Add prototype characters to the registry | |
registry.add_character("warrior", Warrior()) | |
registry.add_character("mage", Mage()) | |
# Clone and display a Warrior | |
warrior = registry.get_character("warrior") | |
print("Original Warrior:", warrior) | |
# Clone and customize a Warrior | |
custom_warrior = registry.get_character("warrior") | |
custom_warrior.weapon = "axe" | |
custom_warrior.armor = "light armor" | |
print("Customized Warrior:", custom_warrior) | |
# Clone and display a Mage | |
mage = registry.get_character("mage") | |
print("Mage:", mage) | |
# Verify the original Warrior remains unchanged | |
original_warrior = registry.get_character("warrior") | |
print("Original Warrior (unchanged):", original_warrior) | |
# Output: | |
# Original Warrior: Character(name=Warrior, weapon=sword, armor=heavy armor, ability=strength) | |
# Customized Warrior: Character(name=Warrior, weapon=axe, armor=light armor, ability=strength) | |
# Mage: Character(name=Mage, weapon=staff, armor=robe, ability=fireball) | |
# Original Warrior (unchanged): Character(name=Warrior, weapon=sword, armor=heavy armor, ability=strength) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment