Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from abc import ABC, abstractmethod
- import time
- import threading
- # Interfejs dla usług dostarczających kursy walut
- class ExchangeRateService(ABC):
- @abstractmethod
- def get_rate(self):
- pass
- # Implementacja konkretnej usługi i jej adaptera
- class ServiceA:
- def fetch_rate(self):
- # Symulacja pobierania kursu
- return 4.55
- class ServiceAAdapter(ExchangeRateService):
- def __init__(self, service):
- self.service = service
- def get_rate(self):
- return self.service.fetch_rate()
- class ServiceB:
- def retrieve_rate(self):
- # Symulacja pobierania kursu
- return 4.53
- class ServiceBAdapter(ExchangeRateService):
- def __init__(self, service):
- self.service = service
- def get_rate(self):
- return self.service.retrieve_rate()
- # Klasa do porównywania kursów i wybierania najlepszego
- class CurrencyBuyer:
- def __init__(self, services):
- self.services = services
- self.best_rate = float('inf')
- def find_best_rate(self):
- for service in self.services:
- rate = service.get_rate()
- if rate < self.best_rate:
- self.best_rate = rate
- print(f"New best rate found: {self.best_rate}")
- # Funkcja do cyklicznego odczytu kursów
- def fetch_rates_periodically(buyer, interval):
- while True:
- buyer.find_best_rate()
- time.sleep(interval)
- # Testowanie aplikacji
- def main():
- service_a = ServiceA()
- service_a_adapter = ServiceAAdapter(service_a)
- service_b = ServiceB()
- service_b_adapter = ServiceBAdapter(service_b)
- services = [service_a_adapter, service_b_adapter]
- buyer = CurrencyBuyer(services)
- # Start periodic rate fetching in a separate thread
- fetch_thread = threading.Thread(target=fetch_rates_periodically, args=(buyer, 10))
- fetch_thread.start()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement