Loading video player…

Introducing the Factory Method Pattern

00:00 In this lesson, you’ll get to grips with the basics of the Factory Method pattern. But you might have done some research online and you might have come across quite a few different descriptions of the Factory Method pattern.

00:14 And some of those are very much based on how the method is implemented as opposed to what it actually is. So I’d like to start with what it actually is. Then you look at what it’s trying to achieve.

00:28 And only then once all that is clear, will you look at how it’s implemented.

00:34 So what is this pattern? Well, the Factory Method pattern is a creational design pattern, used to create concrete implementations of a common interface.

00:46 Hmm, I don’t know about you, but that’s not very clear to me, so let’s dig into that a bit more. Firstly, it’s a creational design pattern. Now, all that means is that it’s a design pattern related to the creation of objects.

01:03 Okay. So we are going to be creating objects. That’s good. What else is there to know? Well, it’s used to create concrete implementations of a common interface.

01:13 So firstly, let’s have a look at what is an interface. Well, the interface can be interpreted as the way you interact with an object. So for example, if your object is a class, then the way you interact with a class is through its public attributes and its public methods, so that would be the interface.

01:33 Now, concrete implementations of a common interface for that, I refer to a serializer example that we will see shortly.

01:43 So what is this method trying to achieve? Well, a key feature of the method is that it separates the process of creating an object from the code that depends on the interface of the object.

01:56 In other words, the user of the interface, or the client, shouldn’t worry about how the objects they are using are being created.

02:07 Right. I think we need an example to bring all these abstract concepts together.

02:12 So, this is our example. We have a client who has a library of songs and wants to convert those songs to a more convenient format, such as XML or JSON. Now, as a note of the process of converting an object’s state into a format that can be stored or transmitted, often as a sequence of bytes or characters, that is called serialization.

02:36 And so we are looking at a serialization example.

02:42 Here’s our client, and our client has a problem. Client says, I’d like to serialize my data, but I don’t know how. So the client does their research and comes across this creator.

02:54 And the creator says, no problem, just send us your data and tell us the format you need and we will convert that data for you.

03:02 So what’s happened is an interface has been established between the client and the creator. They have agreed on a way of communicating, which involves the client sending to the creator the data and the format that that client wants the data to be converted into.

03:21 And this makes life a lot easier for the client because from now on, the creator will then call the factory. The factory creates the data serializers and those products, the data serializers, are then returned to the client who can use them, but doesn’t have to worry about how they were created.

03:40 So that’s our example. Now remember the key feature of the Factory Method, which separates the process of creating an object from the code that depends on the interface of the object.

03:54 So have we established that? Well, the client works with the creator using the interface. So there’s our interface, and there is an agreed way in which the client and the creator are communicating.

04:06 And the creator then takes care of creating the products via the factory. The client doesn’t have to worry about that. In other words, the client code is separated from the code that creates the products.

04:19 And now that you know what the Factory Method is, you learn about its advantages in the next lesson.

Become a Member to join the conversation.