I have been trying to figure out when and where to use different patterns in Python. I came across this document: https://python-patterns.guide/gang-of-four/abstract-factory/
Now this made me wonder how can I do what he says in my code. Here is my implementation is an abstract factory. Not sure if its correct.
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
Here is how I am calling these methods:
posts_manager = PostsManagerFactory.get_posts_manager()
posts = posts_manager.get_posts()
My first question, is it the right way to use abstract factory pattern? If, not please let me know, I will probably ask a new question. Anyway if it is, why does that document say about using callables is better than using abstract factory pattern, how do I do that in this case?
PostsFactory
class does not derive from an abstract class. Anyway, you could use a callable instead of the class by extracting theget_posts_factory()
method and making it a function.get_posts_factory()
that may or may not become a function depending on what you choose is not right. This factory does not return aPostFactory
but an instance of one ofPostManager
subclass. It should then be renamed asget_posts_manager()
instead. If you decide to keep the classPostsFactory
, you should then also rename it toPostsManagerFactory
, but in my opinion, the use of a class is not necessary as it contains only a static method.