From the course: Spring: Design Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Factory pattern in action

Factory pattern in action

From the course: Spring: Design Patterns

Start my 1-month free trial

Factory pattern in action

- [Instructor] So now that we've talked about Factory pattern, it's time to see it in action. If you open up the exercise files in your IDE, you'll see that I've already started the application for you by creating a Spring Boot starter and adding a couple of controller class as well as an interface and some implementations for our Factory. Let's take a look at the interface real quick. You'll see my Pet interface has several methods. Things that make sense when dealing with various types of pets that are really equal across all pets. And that's really how we want to start when dealing with the Factory. So we're going to create a new class called PetFactory. And this Factory we're going to want Spring to actually manage. So we're going to do an @Component annotation on it. Now there's going to be one method on our Factory. And that method is the Factory method itself. So it's going to return a Pet and it's going to be called createPet and it's going to take a string of animalType. Now…

Contents