From the course: Java Design Patterns: Structural
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Solution: The Adapter pattern - Java Tutorial
From the course: Java Design Patterns: Structural
Solution: The Adapter pattern
( upbeat music ) - [Instructor] Let's take a look at my solution to the exercise. Yours might be a bit different to mine and that's fine. So, I've created a class called Adapter and this class implements City. It has a field called city and in the constructor, a city object is passed in and assigns to that field. I've overwritten all of the methods from the city interphase. For most of these methods. I can just call the same method on the city object and it will return what I want. For example, for the get name method. I can just call city.getName. For one method that is sightly more complicated is the get temperature method, because the city is returning a temperature in Celsius and for the weather warnings class to work, it needs to have a temperature in Fahrenheit. So, I've got the temperature with the get temperature method but then I've multiplied it by 1.8 and added 32, to convert it into Fahrenheit. The…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.