Reputation: 61
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
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.
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:
Upvotes: 3