{"id":447,"date":"2014-10-29T10:15:46","date_gmt":"2014-10-29T10:15:46","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=447"},"modified":"2014-10-29T10:15:46","modified_gmt":"2014-10-29T10:15:46","slug":"design-pattern-2","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=447","title":{"rendered":"design pattern"},"content":{"rendered":"<p><strong>&#8212;factory and abstract factory pattern in .net&#8212;&#8212;&#8212;-<\/strong><\/p>\n<p><i>Note: \u2013 This is quiet a confusing architect question especially in design pattern section.<\/i><\/p>\n<p><i>Interviewer can take you for a nice ride. So get the difference in your heart.<\/i><\/p>\n<p>First read the definition provided in the first question about both these patterns. The\u00a0common thing they have is that they belong to creational patterns. In short they hide the\u00a0complexity of creating objects.<\/p>\n<p>The main difference between factory and Abstract factory is factory method uses<\/p>\n<p>inheritance to decide which object has to be instantiated while abstract factory uses<\/p>\n<p>delegation to decide instantiation of object. We can say Abstract factory uses factory<\/p>\n<p>259\u00a0method to complete the architecture. Abstract Factory is one level higher in abstraction\u00a0over Factory.<\/p>\n<p>&nbsp;<\/p>\n<p>First figure shows a sample implementation of Factory Patterns. In this figure there are<\/p>\n<p>two basic sections:-<\/p>\n<p>\u221a\u00a0The actual product section i.e. Class \u201cProduct\u201d it inherits from an abstract<\/p>\n<p>class \u201cAbstractProduct\u201d.<\/p>\n<p>\u221a\u00a0The creational aspect section i.e. \u201cConcreteCreator\u201d class which inherits<\/p>\n<p>from class \u201cCreator\u201d.<\/p>\n<p>\u221a\u00a0Now there are some rules the client will have to follow who<\/p>\n<p>will need the \u201cProduct\u201d object. He will never refer directly to the actual \u201cProduct\u201d object<\/p>\n<p>he will refer the \u201cProduct\u201d object using \u201cAbstractProduct\u201d.<\/p>\n<p>\u221a\u00a0Second client will never use \u201cNew\u201d keyword to create the \u201cProduct\u201d object<\/p>\n<p>but will use the \u201cCreator\u201d class which in turn will use the \u201cConcreteCreator\u201d<\/p>\n<p>class to create the actual \u201cProduct\u201d object.<\/p>\n<p>&nbsp;<\/p>\n<p>So what are the benefits from this architecture? All creational and initializing aspects are<\/p>\n<p>now detached from the actual client. As your creational aspect is now been handled in<\/p>\n<p>\u201cConcreteCreator\u201d and the client has reference to only \u201cCreator\u201d, so any implementation<\/p>\n<p>change in \u201cCreateProduct\u201d will not affect the client code. In short now your creational<\/p>\n<p>aspect of object is completely encapsulated from the client\u2019s logic.<\/p>\n<p>260<\/p>\n<p>Now let\u2019s look at the second class diagram which provides an overview of what actually<\/p>\n<p>\u201cAbstract factory\u201d pattern is. It creates objects for families of classes. In short it describes<\/p>\n<p>collection of factor methods from various different families. In short it groups related<\/p>\n<p>factory methods. Example in this the class \u201cCreator\u201d is implemented using the \u201cAbstract\u201d<\/p>\n<p>factory pattern. It now creates objects from multiple families rather one product.<\/p>\n<p><i>Note :- Just stick up to this definition that Abstract factory classifies factory methods or<\/i><\/p>\n<p><i>groups logically related factory method together.<\/i><\/p>\n<p><i>\u00a0<\/i><\/p>\n<p><b>How Singleton is different from Static class?<\/b><\/p>\n<ul>\n<li>Singleton is a design pattern which solves the problem in a particular context (when we want that only one instance of an object can be created). Whereas static class is a concept where class will be defined with static keyword and all the members need to be strictly static<\/li>\n<li>Singleton pattern let us create the object of the class only once whereas we can&#8217;t create the object of static class.<\/li>\n<li>Class which follows singleton pattern can implement other interfaces whereas static class cannot.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>There is one more question normally asked during interviews &#8220;What you will prefer for sharing, static class or singleton pattern&#8221;<\/p>\n<p>First of all both allows us to share, but I personally prefer singleton pattern because we can derive our singleton class from other interfaces and so get polymorphic feature.<\/p>\n<p>&nbsp;<\/p>\n<p>Example: IPerson s=SingletonPerson.GetObject(PersonEnum.Male); \/\/s is Male<\/p>\n<p>: IPerson s=SingletonPerson.GetObject(PersonEnum.Female); \/\/s is Female<\/p>\n<p>&nbsp;<\/p>\n<p>Secondly Singleton class may contain some non-static members as well.<br \/>\nAnd most importantly we can control the life of a singleton object. It can be explicitly destroyed in between the application.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>in pizza example if NYstore act as client<\/p>\n<p>then it get productA, produtB etc from factory<\/p>\n<p>and can directly access.<\/p>\n<p>&nbsp;<\/p>\n<p>but if we treat NYStore as pizzachef<\/p>\n<p>(as suggested by tcarvin ) and client accces it<\/p>\n<p>to get complete pizza it act as builder<\/p>\n<p>(pizzache as directore and ingredient class as builder)<\/p>\n<p>Following image can exact tell what is the exact difference<\/p>\n<p>note : i am putting this image so whoever visit<\/p>\n<p>this post can understand easily.<\/p>\n<p>&nbsp;<\/p>\n<p>NOW I AM HUNGRY also.<\/p>\n<p><a href=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/10\/Untitled1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-448\" alt=\"Untitled\" src=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/10\/Untitled1.png\" width=\"739\" height=\"261\" \/><\/a><\/p>\n<p>Common steps is implemented in Builder<\/p>\n<h3>Protected constructor<\/h3>\n<p>It is possible to use a protected constructor to in order to permit the subclassing of the singeton. This techique has 2 drawbacks that makes singleton inheritance impractical:<\/p>\n<ul>\n<li>First of all, if the constructor is protected, it means that the class can be instantiated by calling the constructor from another class in the same package. A possible solution to avoid it is to create a separate package for the singleton.<\/li>\n<li>Second of all, in order to use the derived class all the getInstance calls should be changed in the existing code from Singleton.getInstance() to NewSingleton.getInstance().<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Multiple singleton instances if classes loaded by different classloaders access a singleton.<\/h3>\n<p>If a class(same name, same package) is loaded by 2 diferent classloaders they represents 2 different clasess in memory.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">&#8212;factory and abstract factory pattern in .net&#8212;&#8212;&#8212;- Note: \u2013 This is quiet a confusing architect question especially in design pattern section. Interviewer can take you for a nice ride. So get the difference in your heart. First read the definition provided in the first question about both these patterns. The\u00a0common thing they have is that they belong to creational patterns.&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=447\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-447","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=447"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/447\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}