beer
beer

Reputation: 61

Is this code an example of the Adapter pattern?

PageInfo class

public class PageInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int pageNum;
    private int pageSize;
    private int size;
    private int startRow;
    private int endRow;
    private long total;
    private int pages;
    private List<T> list;
}

ResponseDto class

public class ResponseDto<T> implements Serializable {

    private String msg = "success";

    private int code = SUCCESS;

    private T data;
}

PageResponseDto class

public class PageResponseDto<T> extends ResponseDto<List<T>> {

    private int pageNum;

    private int pageSize;

    private int size;

    private long total;

    private int pages;

    public PageResponseDto() {
        super();
    }

    public PageResponseDto(PageInfo<T> p) {
        super(p.getList());
        this.pageNum = p.getPageNum();
        this.pageSize = p.getPageSize();
        this.size = p.getSize();
        this.total = p.getTotal();
        this.pages = p.getPages();
    }
}

use

public PageResponseDto<OrderDto> findStoreOrderByPage(Integer pageNum, Integer pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    List<OrderDto> storeOrderList = basicDataMapper.findStoreOrderByPage();
    PageInfo<OrderDto> p = new PageInfo<>(storeOrderList);
    return new PageResponseDto<>(p);
}

Because the system has a uniform return class 'ResponseDto',I want to transform the class PageInfo to ResponseDto.So wrote this code.I hava a question that this code can be called Adapter Pattern? If not, does this code use design patterns? Or what should this conversion be called?

Upvotes: 2

Views: 133

Answers (1)

Rafael
Rafael

Reputation: 7746

Current Design

As it stands this code does not implement the Adapter pattern. As far as the well-known design patterns go, this code does not satisfy any that I know of. Instead, the design below uses basic software principles: inheritance and pseudo-delegation.

uml diagram

Your goal is to "transform the PageInfo to ResponseDto." I assume you mean you want to pass a ResponseDto instance that encapsulates and delegates to a PageInfo class.

The reason why this is not an example of the Adapter pattern is because there's currently no interface unique to ResponseDto. In fact, it has no interface at all and there's no delegation to PageInfo – Problem 1.

The Adapter pattern is specifically made to encapsulate differing interfaces.


Notes / Suggestions / Issues:

  • PageResponseDTO currently breaks the Law of Demeter; it knows too much about PageInfo's internal structure.
  • PageInfo breaks encapsulation by revealing its entire internal structure.
  • Rather than PageResponseDTO copying everything from PageInfo, why not just keep a reference to a PageInfo instance, and delegate to it when necessary?
  • You can always copy a delegate's interface rather than revealing it, but, if you find yourself copying too many of its functional interfaces, consider exposing it instead. In Refactoring, Martin Fowler refers to this as Encapsulate Delegate and Eliminate Man-in-the-Middle respectively.

Upvotes: 3

Related Questions