Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 752ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
a

abc-class

Recently created Least recently created Recently updated Least recently updated

timmy / 簡單實作資料庫依賴注入

Last active 5 hours ago abc-class database dependency-injection interface mysql oop python

用 Python 實現資料庫介面,輕鬆切換不同資料庫實作。

0 0 0 1
1 from abc import ABC, abstractmethod
2
3 # 定義一個資料庫介面,規定大家都要有 connect 方法
4 class IDatabase(ABC):
5 @abstractmethod
6 def connect(self):
7 pass
8
9 # 具體實作 MySQL 版資料庫
10 class MySQLDatabase(IDatabase):