1 / 5

Object Adapter Pattern in Java

The Object Adapter Pattern allows objects with incompatible interfaces to work together by converting one object's interface to another. This design pattern is exemplified through a CustomerAdapter in Java, demonstrating how geocoded addresses can be processed and saved in a database. The CustomerAdapterImpl class showcases the implementation of the adapter, along with its advantages and disadvantages. While adapters can enhance functionality for multiple adaptees, they may pose challenges in overriding adaptee behavior.

Download Presentation

Object Adapter Pattern in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Adapter Pattern Structural Pattern

  2. Intent • To allow objects to work together that have otherwise incompatible interfaces • To convert the interface of an Object to another that the client expects* • Also known as a Wrapper

  3. Description

  4. Example classCustomer{ void setRestaurantData(String address, String city, String state, String zip){ // do some calculation } } – Standard customer interface CustomerAdapter{ void setRestaurantData(String lat, String lng); } - Certain customers give us different geocoded addresses. - Allows us to save to our database for later retrieval - Create a new object, and tailor the parameters to fit into existing structure class CustomerAdapterImpl implements CustomerAdapter{ private Customer customer = new Customer(); public void setRestaurantData(String lat, String lng){ // calculate latitude and longitude // return address, city, state, zip customer.setAddress(address, city, state, zip); } }

  5. Advantages and Disadvantages • Advantage: Adapter can add functionality to many Adaptees. CustomerAdapter can be more abstract and adapter more than just customer object. • Disadvantage: Harder to override Adaptee behavior. Customer object behavior can’t be changed without subclassing it.

More Related