Software Engineering Design Patterns: Factory Method in Java with Examples

Опубликовано: 22 Октябрь 2024
на канале: Oum Saokosal
1,496
38

This is tutorial of Factory Method in Java with examples from Software Engineering Design Patterns. There are 2 types of Factory in Design Patterns: Factory Method and Abstract Factory. In this tutorial, we will start with Factory Method.

Here is link about Top 10 Facts of Abstract Class in Java:    • Top 10 Facts That You Should Know Abo...  

Factory Method is one of the most important patterns in Software Engineering Design Patterns. In this tutorial, I will explain what is Factory Method in Java with Examples. Even though it is in Java, you can apply this technique in your programming language.

Now, what is the Factory Method? By the definition, the Factory Method defines an interface for creating an object, but let subclasses decide which class to instantiate. You might not get what it means by now? But that's OK. That's why I created this video tutorial just to explain it.

OK, you know what a factory is, right? A factory of shoes, for example. If you place an inquiry to a factory of shoes, then you will get a catalog of shoes that the factory can produce. Maybe there are 3 types of shoes that the factory can make. What if you wanna order a shirt from the factory of shoes? will the factory produce the shirt for you? Absolutely not. Because the factory is specialized in making good shoes, only shoes. Do you get what I mean? So that Factory is the Factory Method. The Factory Method will let you place an order into the shoes factory so that you get a shoes object back. But the shoes object must be one of those in the catalog that the factory can make. If you order any other object beside the list, then you won't get any. In fact, you may get NullPointerException.

Let's go back to the definition, Factory Method defines an interface. Let's cut this part. The interface here means that you will not be allowed to order directly to a shoes staff to make a pair of high heels for you. You must place the order just right at the factory receptionist. That is the meaning of the interface. A Factory Method won't let your order pass directly to any of its core. OK, let's go on with the definition. The Factory Method is an interface for creating an object... this part you already know. And, let subclasses decide which class to instantiate. So a receptionist or a front desk receives your order of maybe a hundred pairs of high heels.Then he decides, OK which department is producing the high heels. Department A for example. OK let's the department A produces the high heels for the client. That is the meaning of the definition.

OK, let's see an example in this video.

To get all the latest update Android app development tutorial videos, I encourage you to subscribe to my channel, and don't forget to share this video to your friends on your social networks like Facebook, Twitter, G+ or even Reddit. If you have any question, please comment below and I will try to answer as many as I can.

Please follow me at:

For English language:
   / oumsaokosal  
  / kosalgeek  
  / okosal  
https://github.com/kosalgeek
https://slideshare.net/oumsaokosal
http://kosalgeek.com

For Khmer language:
   / kosalkhmertech  
  / kosalkhmertech  
http://kosalkhmertech.com


Thank you very much,

Oum Saokosal

The Creator of Channel Oum Saokosal and Kosal Khmer Tech

Credit Music:
"Cheery Monday" Kevin MacLeod (incompetech.com)
Licensed under Creative Commons: By Attribution 3.0 License
http://creativecommons.org/licenses/b...