Adapter Pattern Reuse a method of a certain class Do not modify reused classes </ font>
There are two methods for Adapter Pattern: -Use a class that extends the class to be reused -Instantiate and use the reused class
Check with the following class structure
class | Explanation |
---|---|
sam.class | Class to be reused |
adapt.class | sam.class sam(), Adapt.class exec()Wrap in and reuse |
user(Main.class) | Use Adapter Pattern |
sam.class
class sam {
void sam(){System.out.print("base");}
}
adapt.class
class adapt extends sam{
void exec(){
System.out.print("<<< "); // A
sam(); // sam.class sam()Method
System.out.println(" >>>");} // A
} //A at sam()Wrap
user(Main.class)
public static void main(String[] args){
adapt ad= new adapt();
ad.exec();
}}
The configuration is the same as above, but modify adapt.class
adapt.class
class adapt extends sam{
sam s=new sam();
void exec(){
System.out.print("<<< ");
s.sam();
System.out.println(" >>>");}
}
Recommended Posts