{"id":16,"date":"2007-08-15T21:16:01","date_gmt":"2007-08-16T03:16:01","guid":{"rendered":"https:\/\/www.chrisedwards.dreamhosters.com\/blog\/?p=16"},"modified":"2011-08-21T15:14:50","modified_gmt":"2011-08-21T21:14:50","slug":"when-to-use-a-factory","status":"publish","type":"post","link":"http:\/\/architester.com\/blog\/2007\/08\/15\/when-to-use-a-factory\/","title":{"rendered":"When To Use a Factory"},"content":{"rendered":"<p>All too often I see people creating factory or factory methods for objects that could have just as easily been created by their constructor.<\/p>\n<p>The factory pattern is meant to encapsulate the complexity of object creation. If the creation process is not complex, there is little need for a factory.<\/p>\n<p>Here is one example I see quite often:<\/p>\n<pre class=\"csharp\">\tclass WidgetFactory\r\n\t{\r\n\t\tpublic static Widget Create()\r\n\t\t{\r\n\t\t\treturn new Widget();\r\n\t\t}\r\n\t}<\/pre>\n<p>In this case, the factory is completely unnecessary. I would venture to say that the following is likewise unnecessary:<\/p>\n<pre class=\"csharp\">\tclass WidgetFactory\r\n\t{\r\n\t\tpublic static Widget Create(string name, int size, string description)\r\n\t\t{\r\n\t\t\tWidget widget = new Widget();\r\n\t\t\twidget.name = name;\r\n\t\t\twidget.size = size;\r\n\t\t\twidget.description = description;\r\n\r\n\t\t\treturn widget;\r\n\t\t}\r\n\t}<\/pre>\n<p>In this case, a constructor could have easily taken care of creating this object. So what kind of class needs a factory? The answer is any class that has complex creation. Here is a good example:<\/p>\n<pre class=\"csharp\">\tclass WidgetFactory\r\n\t{\r\n\t\tpublic static Widget FromString(string widgetExpression)\r\n\t\t{\r\n\t\t\tWidgetExpressionParser parser = new WidgetExpressionParser();\r\n\t\t\tWidgetBuilder builder = new WidgetBuilder();\r\n\t\t\tWidget newWidget = builder.ConstructWidgetFromParsedTree(parser.Parse(widgetExpression));\r\n\t\t\treturn newWidget;\r\n\t\t}\r\n\t}<\/pre>\n<p>Because constructing this object is pretty complex, it is good to hide the complexity behind a factory. Factories are not bad, but they are often overused. Any time you use a pattern, make sure there is a compelling reason to use it. If you are unsure why you are using a pattern, it is very possible that you are not using it right. Never use a pattern just for the sake of using a pattern.<\/p>\n<p>-=CE=-<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All too often I see people creating factory or factory methods for objects that could have just as easily been created by their constructor. The factory pattern is meant to encapsulate the complexity of object creation. If the creation process is not complex, there is little need for a factory. Here is one example I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[72,78],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-design","category-patterns","tag-design","tag-patterns"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":2,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":374,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions\/374"}],"wp:attachment":[{"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/architester.com\/blog\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}