python:reflection 反射

C#面象对象的语言也有类似的反射  reflection  近十年前用CSharp在博客园写过。

便于根据配置文件,方便切换数据库。切换DAL层即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# encoding: utf-8
# 版权所有 2024 ©涂聚文有限公司
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# Datetime  : 2024/11/19 20:36
# User      : geovindu
# Product   : PyCharm
# Project   : IctGame
# File      : AbstractFactory.py
# explain   : 学习
 
from __future__ import annotations
from abc import ABC, abstractmethod
import os
import sys
from sqlserverinterface.school import ISchool
 
from common.jsonHelper import JsonHelper
 
class AbstractFactory(ABC):
    """
    工厂方法类
    """
 
    def __init__(self):
        """
        sqlserver、mysql、postgresql
        """
        self.__strsql=self.getDal() #"postgresql"  # 可以根据配置文件,进行切换数据库如是mysql,postgreSQL,sql server
 
    def getDal(self)->str:
        """
 
        :return:
        """
        jsn = JsonHelper()
        self.__strsql = jsn.readDal()
        jsn.readDal()
        return jsn.readDal()
 
    @abstractmethod
    def createSchool(self) -> ISchool:
        """
        生成(创建)接口
        :return:
        """
        #dal = SchoolDal()
        module = __import__(self.__strsql + 'dal.school')
        dal = getattr(module, "SchoolDal")()
        return dal

  

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示