Press "Enter" to skip to content

Java设计模式—适配器模式(adapter pattern)


class TypeCSocket {
    void connectWith(TypeCPlug plug){
Interface TypeCPlug {
    void connectTypeC();
class ThreeDotFivePlug {
    void connectThreeDotFive() { 
        System.out.println("Success to connect !");
class Adapter1 extends threeDotFivePlug implements TypeCPlug {
    void connectTypeC() {
class Adapter2 implements TypeCPlug {
    ThreeDotFivePlug earphones = new ThreeDotFivePlug();
    void connectTypeC() {
public class Test {
    public static void main(String[] args) {
        TypeCSocket socket = new TypeCSocket();
        TypeCPlug plug1 = new Adapter1();
        TypeCPlug plug2 = new Adapter2();
        socket.connectWith(plug1); //output: Success to connect !
        socket.connectWith(plug2); //output: Success to connect !

Be First to Comment


电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see