From No Factory to Factory Method. This article will tell about the very known design pattern . It is one of the most controversial and confused Creational Pattern. There is a alot of confusion in People to understanding this pattern . First of all let me make all of you clear there is nothing like Factory Pattern (as per GOF design pattern list), GOF has something called Factory Method Pattern. We will see how Alexander progresses his knowledge about Factory Method Pattern day by day and how finally he come up with a great solution. Before you starts reading this article, a small advice from my side, keep some snacks ready besides you, because it’s going to be a weary 6 day story. Basically I am trying to play with your patience. It’s gonna be fun, you will enjoy reading this article and towards the end of this article you can proudly say “I know Factory Method Pattern”. Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages. Last shift as the Ford factory in Broadmeadows closes down. Picture: David Crosling Source:News Corp Australia. In the end, Australians will have bought more than 3.8 million Falcons over the past 56 years. Meal and Rest Periods Frequently Asked Questions (FAQ). A factory includes a. Why China Is 'The World's Factory' By Prableen Bajpai, CFA. He said around 75 people had been scheduled to work the overnight shift, which was to end at 7 a.m. The major factory accidents in 20 prompted Bangladesh's government. The factory has a reputation as a. So relax and let’s start our journey to learn Factory Method Pattern. Alexander (our . NET developer) collects requirements from Thomas (our Programmer. X7 CEO). One of his primary requirements was to build a module which will manage online courses offered in the Programmer. X7 (technical institute). I) The system Alexander is going to develop is Course Management System, so the first logical step was to start creating Course Library. II) Next step will be creating instance of courses and building the UI – User Interface. Following is the most obvious code every programmer thinks about and even Alexander did the same. Abstract. Course obj. Course = null. switch(Str. User. Input). . Alexander’s efforts were well appreciated but, it all didn't end there. He then took the requirement to next logical level where he shared his further plan with regards to the application. He said,He is planning to add new online courses like C++, VC++ etc. Well it came to Alexander as a Big Surprise. It was like out of the frying pan but into the fire situation for Alexander.“Adding a new course means”, open the existing UI Code existing logic (add some more switch cases and in future remove some existing case.)Consider a situation of builder, who is asked to add one more floor between 1st and 2nd floor after the building construction is over taking care that building doesn't collapse. Problem with the Approach 1. SOLID principle OCP (Open Closed Principle) will be violated when Course library modifies. Well let’s see how we can do that. Move to Index. How Alexander Proceeded? A factory siren indicating the end of a shift has a frequency of 80 Hz. What frequency is perceived by the occupant of a car traveling towards the factory at 30 m/s? The speed of sound in air is 343 m/s. A factory siren indicating the end of a shift has. A factory siren indicating the end of a shift has a frequency of 80 Hz. What frequency is perceived by the occupant of a car traveling away from the factory at 30 m/s? Alexander woke up early morning next day. Coffee was in his hand but mind was still thinking about the problem in hand. It was very difficult for him to find the right star from dark sky but finally he managed to find the Moon and there came Approach 2. Sky – My Brain. Stars – Thoughts. Moon – Solution for Course Library problem. Approach 2 – Simple Factory Approach. In the first approach UI was creating Course Objects. Now how it will be if the power of object creation is taken away from UI and give it to someone else. Here someone else is called Simple Factory class. What is Simple Factory? Simple Factory is a class with a public static method which will actually do the object creation task according to the input it gets. Course. Factory. . In short Course logic now can be reused. Final step – demonstration. Later in the evening Alexander reached Programmer. X7 office to demonstrate this revised architecture. But before Alexander started to express his anxiety, there came another surprising request. Thomas said, “I think, it’s better if we add one more UI for corporate schedule as well. Reason is that, Now- a- days my corporate training count has increased a lot, so it’s becoming impossible for me to manage.”I) Now the very first thing we should do is, changing course library (because we need to add corporate courses as well). II) Add some more case conditions to our Simple Factory class. Course. Factory. . Good or Bad? Small transcript of the big discussion we had after looking into the architecture. Thomas: Sorry Alexander but I don’t think this solution will work. Alexander: Why Sir? Thomas: See, it’s great that there exist a separate Factory for every Course group. I meant how you will make sure that every factory is creating objects properly, for instance how will you be sure that each factory is following a company standard and creates course materials and schedule prior to returning the object. It may possible that in future one more kind of factory say Offline. Course. Factory is added for managing offline courses and unknowingly it just violates the company standard and creates course materials in his own way. Alexander: You are right Sir. Let me think about a full proof and final solution, Give me a day. Thomas: No problem Alexander. Have a nice day. Problem with the Approach 3. Every factory is independent here. There is no strict rule for defining factories. In this approach each factory can have their own structure and standards. Move to Index. Let’s list down all our requirements. We need Factory for online Courses. We need Factory for corporate Courses. We don’t want to create single Factory for both modules. There should be some rule how to define factories, which everybody should follow. How it would be if we use combination of approach 3 (that is whenever required create new factory)plus Inheritance or Composition. Move to Index. What Alexander was talking about last night? Put all common functionalities in a single place (class) and reuse them in all factories. We call it “Reusbale. Class” for discussion purpose. We need reusability - we can do it by 2 ways. Factory Method lets a class defer instantiation to subclasses. It’s that simple. That’s all about the Factory Method patterns. Finally Thomas seems happy because all his requirements seem satisfied and Alexander is also happy because now he has become champ in Factory Method Pattern. Move to Index. Is parent factory has to be abstract always? Answer for this question is No. We can make it non abstract if required and add default implementation to Get. Course method making it virtual. Is switch is must in factories? No. We can replace switch loop with the Dictionary in . NET. Is inheritance from second level factory is possible? Yes absolutely and that’s how factory method works. Initially we had Abstract. Course. Factory with its default implementation for Get. Course method, which will be later extended by Online. Course. Factory adding its own implementation for Get. Course method, which in turn can be a virtual method. So that later say when a new Online. Advance. Course is introduced in Programmer. X7 existing, Online. Course. Factory can easily be extended. What will be the advantage of using Multi. Level inheritance in case of Factory Method Pattern? Well, consider following scenario. Please don't forget to leave your comments below.. Your positive and negative comments will encourage me to write better.. Move to Index. Click Following links for complete source code. For any training related to other design patterns and . NET you can contact me at Sukesh. Marla@Gmail. com or at www. Click and go here to get more on . NET and C# learning stuffs. Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |