K
K
Konstantin2020-06-16 14:33:05
OOP
Konstantin, 2020-06-16 14:33:05

Does the following class structure violate OOP and SOLID principles?

Interface description:

export interface NextPrevPage<T> {
  prevUrl: string;
  nextUrl: string;

  getNextPage(): Observable<T>;
  getPrevPage(): Observable<T>;
}


A service that contains http request methods and implements an additional NextPrevPage interface:

export class OrdersDistributionService implements NextPrevPage<Response> {
  constructor(private http: HttpClient) {}

  prevUrl: string;
  nextUrl: string;

  getNextPage(): Observable<Response> {
    return this.http.get<Response>(this.nextUrl, {});
  }

  getPrevPage(): Observable<Response> {
    return this.http.get<Response>(this.nextUrl, {});
  }

    getOrders() {}
}


The question is, is it correct to share the class with an interface, or is it better to put the functionality NextPrevPageinto a separate class and use it as a composition inside the OrdersDistributionService service ?

Which approach would be more correct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twoone, 2020-06-16
@Junart1

solid involves working with the architectural layer, and not the design of individual elements that make up the system. In your case, we are talking about design, since the difficulty arose in defining the boundaries of ordinary algorithmic logic in the context of the OOP paradigm.
You have almost everything correct, except for the logic of the urls mixed with the logic of the service. The role of the service in providing the data it downloads over the network, and its responsibilities, its logic should be limited to this. Working with the http service fits into these responsibilities, but there are no links to urls. They are the state of the service itself, which means that working with them is also the logic of the service, which contradicts the logic mixing rule. This situation can be resolved by placing links in a separate object that stores and rotates links.
And you asked about validation. Validation is also placed in a separate validator, and since you are working with rxjs, it happens in the pipe after loading. But keep in mind that if you validate using filter and for some reason the validation fails, the stream will be interrupted and subscribers will never know about it. This behavior will lead to a leak. Therefore, you yourself must manage this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question