Adapter model and the decorator pattern

Theory is a powerful tool to guide practice

Adapter mode

The concept : a class interface adapted to the user is looking for. An adapter allows usually because of incompatible interfaces can not work together, work together in class, practice is like its own interface wrapped in an existing class.
Role :
· target role (target): a specific target may be abstract classes, interfaces can also be
· fit those roles (Adapte E ): an interface or implement their own business
-adapter device roles (Adapte r ): the interface and the interface to convert their business objectives for their needs class

Demo
We have a business interface class ReaderData his function is used to read the data, but read data formats need to achieve alone.
Our objective data in two formats, one is binary, hexadecimal one is
reading binary data interface class DataReaderBinary ourselves has been achieved.
Read hex data interface class DataReaderHexadecimal need our own access

Reading data interface class ReaderData

public interface ReaderData {

   String reader();
   
}

ReaderData implementation class

public class ReaderDataImpl implements ReaderData {

   public String reader() {
       return "能够读取数据的格式: ";
   }
}

Binary data interface class DataReaderBinary

public interface DataReaderBinary {
   //读取二进制数据的方法
   String reader();
}

Binary data interface class class

public class DataReaderBinaryImpl implements DataReaderBinary {

   private ReaderData readerData;
// 让该类获得获取数据的能力  
   public DataReaderBinaryImpl(ReaderData readerData) {
       this.readerData = readerData;
   }

   public String reader() {
       return readerData.reader()+" 二进制数据";
   }
}

Hexadecimal data interface class DataReaderHexadecimal

public interface DataReaderHexadecimal {

    //读取十六进制的方法
    String reader();
}
  • DataReaderHexadecimal here is we want to access the function interface is the goal of his role
  • And our binary implementation class has the ability to read binary data, he is fitter.
  • Now we have to use an adapter, so that the fitter has the function of the target role.

Adapter is divided into two modes

  • Object Adapter mode
  • Class Adapter pattern

He Object Adapter feature is wrapped inside a target character that is the target character object

public  class AdaptiveClass  implements DataReaderHexadecimal {

    private DataReaderBinary dataReaderBinary;

    public AdaptiveClass(DataReaderBinary  dataReaderBinary) {

        this.dataReaderBinary =dataReaderBinary;
    }

    public String readerHexadecimal() {
        return "十六进制的数据";
    }
}

test

public class Test {

    public static void main(String[] args) {
        ReaderData readerData =new ReaderDataImpl();
        DataReaderBinary dataReaderBinary = new DataReaderBinaryImpl(readerData);
        AdaptiveClass adaptiveClass = new AdaptiveClass(dataReaderBinary);
        System.out.println(dataReaderBinary.reader()+" "+adaptiveClass.readerHexadecimal());
    }

Test Results
Here Insert Picture Description

  • This way you can kind of binary interface (that is, under any implementation of adaptation Interface) adapter and target role

Class Adapter pattern

public  class AdaptiveClass extends DataReaderBinaryImpl implements DataReaderHexadecimal {

    public AdaptiveClass(ReaderData readerData) {
        super(readerData);
    }

    public String readerHexadecimal() {
        return "十六进制的数据";
    }
}

Test Results

public class Test {

    public static void main(String[] args) {
        ReaderData readerData =new ReaderDataImpl();

        DataReaderBinary dataReaderBinary = new DataReaderBinaryImpl(readerData);

        AdaptiveClass adaptiveClass = new AdaptiveClass(readerData);
        String reader = dataReaderBinary.reader() +"  "+ adaptiveClass.readerHexadecimal();
        System.out.println(reader);
    }
}

Here Insert Picture DescriptionAnd this way is selected to achieve the flexibility of adaptation's bad

We look at the IO java is how to use the fitter mode
Here Insert Picture Description

  • InputStreamReader inherited from Reader and Reader achieved Readable Closeable
  • That is how he functions in association with the InputStream InputStreamReader it up
  • In the source code we saw him come in is passed as a parameter InputStream
  • Here Insert Picture Description
  • Here is adapted to read who is the target audience and InputStream InputStreamReader is like adapter
  • Similarly outputStreamWriter inherited write this relationship also with outpStream

Decorator

The concept : to enhance the method based on the original

  • Roles:
  • Abstract component role: given an abstract interface to regulate ready to receive additional responsibilities.
  • DETAILED member (ConcreteComponent) Roles: define a responsibility to be received additional classes.
  • Decoration (Decorator) role: like a booster, using his enhanced method.
  • Specific decoration (ConcreteDecorator) Role: General inherit booster to enhance the achievement of specific logic.

Demo
abstract class role member
Saucer plate

/**
 * 碟子
 */
public abstract class Saucer {
    /**
     * 形状
     */
    protected abstract String shape();

    /**
     * 外观
     */
    protected abstract String facade();
}

Implement particular plate type member

public class ASaucer extends Saucer {
    protected String shape() {
        return "方形";
    }

    protected String facade() {
        return "黑色条纹";
    }
}

Decorative role

public class SaucerDecorator extends Saucer {

    private Saucer saucer;

    public SaucerDecorator(Saucer saucer) {
        this.saucer = saucer;
    }

    protected String shape() {
        return saucer.shape();
    }

    protected String facade() {
        return saucer.facade();
    }
}

Specific decorative role

public class SaucerBlue extends SaucerDecorator {

    private Saucer saucer;

    public SaucerBlue(Saucer saucer) {
        super(saucer);
    }

    @Override
    protected String shape() {
        return super.shape()+"还能变形成圆形";
    }

    @Override
    protected String facade() {
        return super.facade()+"还能是蓝色条纹";
    }
}

Test category

public class Test  {
    public static void main(String[] args) {
        Saucer saucer ;
        saucer = new ASaucer();
        saucer = new SaucerBlue(saucer);
        System.out.println(saucer.shape());
        System.out.println(saucer.facade());
    }
}

Here Insert Picture Description

We look at the Decorator Pattern in java some use
BufferedReader under IO package
Here Insert Picture Description

Reader is an abstract class

Here Insert Picture Description
BufferedReader inherited Reader, and Reader are combined into class

Here Insert Picture Description
Let us look at his constructor
Here Insert Picture Description

The same is the Reader as their parameters by using Reader to construct a BufferedReader
here Reader is an abstract component role is decorative role and BufferedReader

  • Like there BufferedInputStream he inherited from ** FilterInputStream **
  • The FilterInputStream inherited from InputStream they are the InputStream as a parameter into the constructor.
  • Which InputStream is an abstract class, he is the abstract component role, FilterInputStream decorative role, BufferedInputStream concrete decorative role.
  • The following specific FilterInputStream decorator such as BufferedInputStream there are many, such as LineInputStream, DataInputStream.
  • Reflected in the spring of

Difference adapter mode with the Decorator Pattern

  • Adapter mode
    is the external interface of functional integration in their business.
  • The decorator pattern
    is a method to enhance functionality, add more functions on the basis of the original.

Here Insert Picture Description

Published 10 original articles · won praise 3 · Views 2388

Guess you like

Origin blog.csdn.net/qq_44647212/article/details/104157952