简体   繁体   English

场景ThemeFactory的抽象工厂设计模式

[英]Abstract Factory design pattern for the scenario ThemeFactory

I am in the process of implementing the Abstract Factory Design pattern. 我正在实施抽象工厂设计模式。 The scenario that I considered is given below. 我考虑的场景如下。

在此处输入图片说明 . The article and picture credits come from https://medium.com/@hitherejoe/design-patterns-abstract-factory-39a22985bdbf . 文章和图片来源来自https://medium.com/@hitherejoe/design-patterns-abstract-factory-39a22985bdbf I am trying to implement in Python and I am stuck and couldn't move forward due to the below reason 我正在尝试用Python实现,由于以下原因,我被卡住了,无法前进

When you define ThemeFactory as an interface in Python, then whatever the methods you define in ThemeFacory has to be implemented in concrete classes such as LightTheme and DarkTheme. 当您将ThemeFactory定义为Python中的接口时,无论您在ThemeFacory中定义的方法是什么,都必须在诸如LightTheme和DarkTheme之类的具体类中实现。

But my concern is, is there any necessity to provide an implementation to method createLightTheme in Concrete class DarkTheme vice-versa. 但我担心的是,有没有必要提供方法的实现createLightTheme在具体的类DarkTheme反之亦然。 Should I need to redesign the complete architecture? 我是否需要重新设计完整的体系结构?

 import abc

 class ThemeFactory(metaclass=abc.ABCMeta):

     @abc.abstractmethod
     def createdarktheme(self):
         pass

     @abc.abstractmethod
     def createlighttheme(self):
         pass


 class DarkTheme(ThemeFactory):

     def createdarktheme(self):
        print('Hello created dark theme')

     def createlighttheme(self):
        pass


if __name__== '__main__':

   dark = DarkTheme()
   dark.createdarktheme()

Ray's answer is correct, in that the diagram you're referring to is not right. Ray的答案是正确的,因为您所指的图表不正确。

The ThemeFactory should create Themes, and DarkTheme and LightTheme should be implementations of Theme. ThemeFactory应该创建主题,而DarkTheme和LightTheme应该是Theme的实现。

The code would look roughly like: 该代码大致如下所示:

from abc import ABC, abstractmethod

class ThemeFactory(object):
     def createdarktheme(self) -> Theme:
         return DarkTheme()

     def createlighttheme(self) -> Theme:
         return LightTheme()

class Theme(ABC):
   @abstractmethod
   createToolbar() -> ToolBar:
       pass

   @abstractmethod
   createDialog() -> Dialog:
       pass

class DarkTheme(Theme):
   createToolbar() -> ToolBar:
       return DarkToolbar()

   createDialog() -> Dialog:
       return DarkDialog()


if __name__== '__main__':

   factory = ThemeFactory()
   dark = factory.createdarktheme()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM