简体   繁体   中英

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 . I am trying to implement in Python and I am stuck and couldn't move forward due to the below reason

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.

But my concern is, is there any necessity to provide an implementation to method createLightTheme in Concrete class DarkTheme vice-versa. 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.

The ThemeFactory should create Themes, and DarkTheme and LightTheme should be implementations of 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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