如何在 Python 中使用可调用对象而不是抽象工厂模式?

How to use callables instead of Abstract Factory Pattern in Python?

我一直在努力找出何时何地在 Python 中使用不同的模式。我看到了这份文件:https://python-patterns.guide/gang-of-four/abstract-factory/

现在这让我想知道我怎样才能按照他在我的代码中所说的那样做。这是我的实现是一个抽象工厂。不确定是否正确。

from abc import abstractmethod, ABC

from sqlalchemy.orm import Session

from managers.database_manager import DatabaseManager
from managers.log_manager import LogManager
from managers.sqlalchemy_manager import get_db
from models.bal.post import Post
from models.dal.models import Post as ORMPost


class PostsManager(ABC):
    def __init__(self):
        pass

    @abstractmethod
    def get_posts(self):
        pass

    @abstractmethod
    def get_post(self, post_id):
        pass

    @abstractmethod
    def create_post(self, post: Post):
        pass

    @abstractmethod
    def delete_post(self, post_id):
        pass

    @abstractmethod
    def update_post(self, post_id, post: Post):
        pass


class PostsManagerFactory:
    @staticmethod
    def get_posts_manager(use_orm=True) -> PostsManager:
        if use_orm:
            return PostsManagerWithORM()
        else:
            return PostsManagerWithoutORM()


class PostsManagerWithORM(PostsManager):

    def get_posts(self):
        db: Session = get_db()
        posts = db.query(ORMPost).all()
        return posts

    def get_post(self, post_id):
        pass

    def create_post(self, post: Post):
        pass

    def delete_post(self, post_id):
        pass

    def update_post(self, post_id, post: Post):
        pass


class PostsManagerWithoutORM(PostsManager):
    def __init__(self):
        super().__init__()
        self.db_manager = DatabaseManager()

    def get_posts(self):
        posts = self.db_manager.execute_query("select * from posts")
        return posts

    def get_post(self, post_id):
        post = self.db_manager.execute_query("SELECT * FROM posts WHERE id='%s'", (post_id,), single_record_flag=True)
        return post

    def create_post(self, post: Post):
        post = self.db_manager.execute_query("INSERT INTO posts (title, content) VALUES (%s, %s) RETURNING *",
                                             (post.title, post.content), single_record_flag=True)

        return post

    def delete_post(self, post_id):
        post = self.db_manager.execute_query("DELETE FROM posts WHERE id = %s RETURNING *", (post_id,),
                                             single_record_flag=True)
        return post

    def update_post(self, post_id, post: Post):
        post = self.db_manager.execute_query(
            "UPDATE posts SET title = %s, content = %s, published = %s WHERE id= %s RETURNING *",
            (post.title, post.content, post.published, post_id),
            single_record_flag=True)
        return post

我是这样调用这些方法的:

posts_manager = PostsManagerFactory.get_posts_manager()
posts = posts_manager.get_posts()

我的第一个问题,使用抽象工厂模式是否正确?如果不是,请告诉我,我可能会问一个新问题。无论如何,如果是的话,为什么该文档说使用可调用对象比使用抽象工厂模式更好,在这种情况下我该怎么做?

关注你最后一条评论。

我不会在这个用例中使用抽象工厂模式。请问你为什么要用它?

当我建议通过将 get_posts_manager 提取到函数中来删除 PostsManagerFactory class 时,我的意思是替换此代码片段

class PostsManagerFactory:
    @staticmethod
    def get_posts_manager(use_orm=True) -> PostsManager:
        if use_orm:
            return PostsManagerWithORM()
        else:
            return PostsManagerWithoutORM()

有了这个

def get_posts_manager(use_orm=True) -> PostsManager:
    if use_orm:
        return PostsManagerWithORM()
    else:
        return PostsManagerWithoutORM()

你可能想这样缩短

def get_posts_manager(use_orm=True) -> PostsManager:
    return PostsManagerWithORM() if use_orm else PostsManagerWithoutORM()

然后,您只需调用函数即可在代码中使用它

posts_manager = get_posts_manager() # <----
posts = posts_manager.get_posts()