{"id":188,"date":"2019-07-18T16:50:38","date_gmt":"2019-07-18T22:50:38","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=188"},"modified":"2019-07-15T18:39:06","modified_gmt":"2019-07-16T00:39:06","slug":"implementing-the-factory-pattern-in-xojo","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/07\/18\/implementing-the-factory-pattern-in-xojo\/","title":{"rendered":"Implementing the Factory pattern in Xojo"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When using the &#8220;factory&#8221; pattern you <em>should<\/em> only be able to get a valid instance from the factory and NO other way. Thats kind of the point of the pattern &#8211; to reduce the number of points at which instances can be created. Normally in Xojo you might have a module with a method that can create instances, and its the only one allowed to do this. The constructors for the classes the factory method can create should be protected or private so they cannot be directly invoked using New outside the module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Usually it might look something like (note this code will not compile)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Module People\n\n  Public Interface IPerson\n      Public Function GetName() as string\n  End Interface\n\n  Public Class Villager\n    Implements IPerson\n      Public Function GetName() as string\n        return \"Village Person\"\n      End Function\n\n      Protected Sub Constructor()\n      End Sub\n  End Class\n\n  Public Class CityPerson \n    Implements IPerson\n      \n      Public Function GetName() as string\n        return \"City Person\";\n      End Function\n\n      Protected Sub Constructor()\n      End Sub\n  End Class\n\n  Public Enum PersonType\n    Rural\n    Urban\n  End Enum\n\n  Public Function GetPerson(type as PersonType) as IPerson\n  \n    select case type\n    case PersonType.Rural\n      return new Villager()\n    case PersonType.Urban\n      return new CityPerson()\n    else\n        raise new UnsupportedOperationException\n    end select\n  End Function\n\nEnd Module<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An alternative would be to not use an Interface for IPerson but to make it a base class &#8211; they end result is similar.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But, you cant do either in Xojo. At least not quite like my code above shows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have a factory method in the module it cannot invoke their constructors. They are only callable by items in the class hierarchy. It has no special means to access the private or protected constructors of the classes it contains.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So you cant easily restrict construction to ONLY the factory method since what you really need is a &#8220;module&#8221; or &#8220;namespace&#8221; scope. And that doesn&#8217;t exist. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What you need to do is create an INTERFACE for the various classes in the namespace you intend to expose and make the classes in your namespace implement these interfaces. In addition you need to make the interfaces PUBLIC so they can be used outside the module. As well you need to make the classes that implement the interfaces PRIVATE so you can&#8217;t actually try and instantiate them outside the module. This has the unfortunate side effect of making it so you can only use the classes in the module via their interfaces. Remember what we really wanted was just to make it so the only legal way to get an instance was to use the factory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that by having to do everything via an interface this means that &#8220;properties&#8221; are exposed by pairs of getter \/ setter methods. Fortunately in Xojo this has little semantic impact except that you have to write the code for it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All code not in the module must use the interfaces as that&#8217;s all you have available. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But now because the classes in the module are PRIVATE you can put as many public constructors on them as you desire. They won&#8217;t be callable by anything outside of the module so instances cannot be created in ANY way except by calling the factory method in the module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Module scope would help reduce this work needed to implement the Factory Pattern by making it possible to implement the module&#8217;s classes with &#8220;module&#8221; scoped constructors. This way you could have the factory return any of the parent or subclass instances and you could skip all the interfaces.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A sample of the initial implementation that doesnt work and the fixed version is <a href=\"http:\/\/great-white-software.com\/miscellaneous\/cant%20implement%20factory%20properly.zip\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using the &#8220;factory&#8221; pattern you should only be able to get a valid instance from the factory and NO other way. Thats kind of the point of the pattern &#8211; to reduce the number of points at which instances can be created. Normally in Xojo you might have a module with a method that &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/07\/18\/implementing-the-factory-pattern-in-xojo\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Implementing the Factory pattern in Xojo&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[27,28,3],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-advanced-techniques","tag-factory","tag-xojo"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}