{"id":810,"date":"2022-08-01T10:52:13","date_gmt":"2022-08-01T08:52:13","guid":{"rendered":"https:\/\/olkn.myvnc.com\/?p=810"},"modified":"2022-08-01T11:01:44","modified_gmt":"2022-08-01T09:01:44","slug":"factory-method-in-python","status":"publish","type":"post","link":"https:\/\/olkn.myvnc.com\/?p=810","title":{"rendered":"factory method in python"},"content":{"rendered":"<p><code><br \/>\nclass ObjectStore:<br \/>\n   \"\"\" a generic class to store objects either to SQL or the filesystem<br \/>\n       the interface storeaway uses a specific storeformat and gets the associated implementation storer\"\"\"<br \/>\n   def storeaway(self, storable, storeformat):<br \/>\n      storer = factory.get_storeaway(storeformat) \"\"\"get implementation for storeaway using the storeformat\"\"\"<br \/>\n      storeable.storeaway(storer) \"\"\"invoke the implementation of storeaway for the given format\"\"\"<br \/>\n      return<\/p>\n<p>class storeawayFactory:<br \/>\n   def __init__(self):<br \/>\n      self._creators = {}<br \/>\n   def register_format(self, storeformat, creator):<br \/>\n      self._creators[storeformat] = creator<br \/>\n   def get_storeaway(self, storeformat):<br \/>\n     creator = self._creators.get(storeformat)<br \/>\n     if not creator:<br \/>\n        raise ValueError(storeformat)<br \/>\n     return creator()<\/p>\n<p>factory = storeawayFactory()<br \/>\nfactory.register_format(\"SQL\", SQLStoreaway)<br \/>\nfactory.register_format(\"FILE\", FileStoreaway)<\/p>\n<p><code><\/p>\n<p>In the final object to work with we need to implement the <\/p>\n","protected":false},"excerpt":{"rendered":"<p>class ObjectStore: &#8220;&#8221;&#8221; a generic class to store objects either to SQL or the filesystem the interface storeaway uses a specific storeformat and gets the associated implementation storer&#8221;&#8221;&#8221; def storeaway(self, storable, storeformat): storer = factory.get_storeaway(storeformat) &#8220;&#8221;&#8221;get implementation for storeaway using the storeformat&#8221;&#8221;&#8221; storeable.storeaway(storer) &#8220;&#8221;&#8221;invoke the implementation of storeaway for the given format&#8221;&#8221;&#8221; return class storeawayFactory: &hellip; <a href=\"https:\/\/olkn.myvnc.com\/?p=810\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">factory method in python<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,10],"tags":[203,204,202],"class_list":["post-810","post","type-post","status-publish","format-standard","hentry","category-knowledgebase","category-sniplets","tag-design-pattern","tag-factory","tag-python"],"_links":{"self":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=810"}],"version-history":[{"count":3,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/810\/revisions"}],"predecessor-version":[{"id":813,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=\/wp\/v2\/posts\/810\/revisions\/813"}],"wp:attachment":[{"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/olkn.myvnc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}