Factory Design Pattern

Get everything here:

Best Design Patterns Book :

Welcome to my Factory design pattern tutorial. This is a continuation of my design patterns video tutorial.

You use the Factory design pattern when you want to define the class of an object at runtime. It also allows you to encapsulate object creation so that you can keep all object creation code in one place.

The Factory pattern is presented in many ways to help you learn. Refer to the code to completely understand it.






29 responses to “Factory Design Pattern”

  1. Elias Oquendo Avatar

    Do you know of any books that have projects, so that you can practice these patterns?

  2. darkfafi Avatar

    I have a question. Would it not be better to make the factory static instead of making an instance out of it? Also, can't you rule out the entire pattern if you have access to Reflection/ Generic methods. Just give the type and get the instance created with it?

  3. DobermaNoviSad Avatar

    In EnemyShipFactory:

    there is no need for EnemyShip newShip = null;

  4. Troll Axe Thrower Avatar

    If else If else If else – just use switch.

  5. chitra raj Avatar

    Your examples are too good!! it becomes easy to get into head 🙂

  6. E M Avatar

    8:50 "Show you what is so factory about this factory"! LOL Derek! Great movie.

  7. T4l0nITA Avatar

    9:16 I don't quite get why you are creating that EnemyShip newShip Object if you end up never using it

  8. Remco Hoeneveld Avatar

    Hey man, I am a student at the university of applied sciences in Rotterdam ( the netherlands ) and I would like to thank you from the bottom of my heart for all these amazing informative videos, they really help me out!

  9. Hirh Karn Avatar

    Thank you very much! Very useful 🙂 Just one question: why do you declare the `doStuffEnemy()` (minute 6:00) method as a `static` method?

  10. Moritz Richter Avatar

    hi there, when exactly do i prefer abstract class rather then interfaces and vice versa?

  11. Ciri Avatar

    I'm so fucked; I just don't understand java and am on the verge of losing my Masters because of this. Exam in two weeks and here I am trying to understand concepts that just seem foreign to me. Maybe I am dumb.
    But, I'm not a quitter, so I'm working hard these two weeks to surprise myself and make myself proud.

    Also, this is about object factories, isn't it?

  12. Evgeny Dan Avatar

    Doesn't having 'if' in your factory violates the open/closed principal? what if at some point in time I have other options aside from U or R

  13. John Sutton Avatar

    Humble Brag:

    Completed 1HR Tutorials: Go, Python, Java, Elixir, Erlang, C++, jQuery %50, Haskell %80
    Tkinker: MessageBox, Calculator, started TextEditor Before matching video will compare results and add line numbers when watching. I couldn't figure that part out using the tkinker website.
    Four or Five of the Python ones.
    Patterns Observer, Sort and General working on Factory as I type.

    Completed means typed in everything myself and got to compile and run. I push pause and rewind like thousands of times while watching the videos.

    Setups Atom, PyCharm and Eclipse already was using Brackets for JavaScript.

    I have also expiremeted with NodeJS, Elm, JQuery, Bacon and Haskell, before watching your videos. The server side is my weakness. I did get the Django server to start.

    Looking forward to PyGame.

    Failures fSharp and Kivy. I can't get either of those to work on linux.

    General comment thanks for the free PHD.

    Thanks a bunch.

  14. John Sutton Avatar

    Dude you are on like Donkey Kong. Much respect.

  15. John Sutton Avatar

    I did all that and more. The fsharp extension is loaded. Just no NEW PROJECT. Erlang, Elixir, Haskell, Python, C++, Java all worked fairly easy but not fsharp.

  16. Munya Sabkar Avatar

    I have to design a system to produce phones with the same make but different model specifications and that a customer can choose and mix between the offered options. WHICH PATTERN TO USE? help please

  17. Rukshan Perera Avatar

    Thank you.. btw I think line 5 on EnemyShipFactory.java is not needed

  18. tapan pattanaik Avatar

    can you give any real time project example where we should use factory design pattern.

  19. Gustavo Vargas Castilhos Avatar

    This series should be called "Java Tutorials in a Sexy Voice"

  20. Steve Lee Avatar

    You need to spend 25% of the time to explain the concept and reason why a Factory Pattern is used. Provide a Use Case like ordering coffee at Starbucks or ordering a Pizza at a Restaurant. Let the student know How and Why a Factory Pattern is used. Simply spending all this time coding does nothing for the student. It only confuses and leaves the audience to think that code is more important than the reason to use this Pattern. All your videos should begin with a Real Life Sample coding is only used to implement the Coffee Shop or Pizza Rest.

  21. puneet sohi Avatar

    Reverend Lovejoy?

  22. Hang Du Avatar

    In this example, EnemyShip is an abstract class but there are no abstract methods in this class.Maybe it is better to have some abstract methods to make more sense.

  23. Saravuth Seng Avatar

    i learn from this list more than i learn at school four years.

  24. Jin Izzraeel Avatar

    The easiest DP out there

  25. Trend House Avatar

    One thing about Factory pattern that I don't understand, is that every single example recommends creating a separate factory class. Why not just make a static method inside a base class instead of doing that? Not only you will avoid creating a useless class, but you'll be able to call private members that belong to base class, instead of having to make them public.

  26. Rustam Islamov Avatar

    Куда торопишься, быстро говоришь

  27. Man Inblack Avatar

    I still didn't get what special about Factory Pattern?

  28. 張芭樂 Avatar

    Can i change the makeEnemyShip of EnemyShipFactory to static method?

Leave a Reply

Your email address will not be published. Required fields are marked *