Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Adapter Pattern in Java
https://grokonez.com/design-pattern/adapter-pattern-java
Adapter Pattern in Java
Adapter Pattern is a Structural Pattern in which, an interface of a class is converted into another interface that Client expects to work. With Adapter Pattern, we can reuse existing code without changing it.
I. OVERVIEW
Adapter Pattern defines an Adapter that can adapt a target class/interface (that we called Adaptee) to Client's requirement.
The Adapter contains an instance of Adaptee (also hides it from Client). It helps that, Client calls Adapter methods without knowing anything about Adaptee, then Adapter uses Adaptee instance inside to call its appropriate methods.
II. PRACTICE
1. Project Overview
We have Person 'fullname' information, but Client want to use 'first name' and 'last name' as Customer information. So we create a Person-to-Customer-Adapter that Client will work with.

2. Step by Step
2.1- Create Person
class:
package com.javasampleapproach.adapter.pattern;
public class Person {
private String fullName;
public Person(String fullName) {
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Override
public String toString() {
return "Person [fullName=" + fullName + "]";
}
}
2.1- Create
Person
class:
package com.javasampleapproach.adapter.pattern;
public class Person {
private String fullName;
public Person(String fullName) {
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Override
public String toString() {
return "Person [fullName=" + fullName + "]";
}
}
2.2- Create ICustomer
interface:
package com.javasampleapproach.adapter.pattern;
public interface ICustomer {
public String getFirstName();
public String getLastName();
}
2.3- Create PersonToCustomerAdapter
class that implements ICustomer
interface:
More at:
https://grokonez.com/design-pattern/adapter-pattern-java
Adapter Pattern in Java




