[JAVA] Adapter Pattern

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

Design Pattarm MENU

Extends classes to be reused

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
  • User Includes the meaning that other developers use this 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();
}}

Instantiate a class to be reused

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

Adapter Pattern
[Java] Adapter pattern
Design pattern ~ Adapter ~
Prototype pattern
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Design pattern ~ Visitor ~
Java design pattern
java callback pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Design pattern ~ Command ~
Abstract Factory pattern
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Flyweight ~
Java pattern memo