Performance: Facade service pattern
MEDIUM IMPACT
This pattern affects the interaction complexity between components and services, impacting rendering speed and responsiveness by reducing redundant data fetching and change detection triggers.
export class FacadeService { private data$ = new BehaviorSubject<Data | null>(null); constructor(private http: HttpClient) { this.loadData(); } private loadData() { this.http.get<Data>('/api/data').subscribe(data => this.data$.next(data)); } getData() { return this.data$.asObservable(); } } export class ComponentA { data$ = this.facade.getData(); constructor(private facade: FacadeService) {} } export class ComponentB { data$ = this.facade.getData(); constructor(private facade: FacadeService) {} }
export class ComponentA { constructor(private service: DataService) {} ngOnInit() { this.service.getData().subscribe(data => { /* update UI */ }); } } export class ComponentB { constructor(private service: DataService) {} ngOnInit() { this.service.getData().subscribe(data => { /* update UI */ }); } } @Injectable({providedIn: 'root'}) export class DataService { constructor(private http: HttpClient) {} getData() { return this.http.get('/api/data'); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple components call service independently | Multiple subscriptions causing repeated DOM updates | Multiple reflows triggered per component update | Higher paint cost due to frequent UI changes | [X] Bad |
| Facade service shares single data stream | Single subscription source with shared updates | Single reflow triggered for shared data update | Lower paint cost with batched UI updates | [OK] Good |