0
0
Angularframework~20 mins

Facade service pattern in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Facade Service Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Angular facade service component output?

Consider this Angular component using a facade service to get user data.

import { Component } from '@angular/core';
import { UserFacade } from './user.facade';

@Component({
  selector: 'app-user',
  template: `User: {{ userName }}`
})
export class UserComponent {
  userName = '';
  constructor(private userFacade: UserFacade) {
    this.userFacade.getUserName().subscribe(name => this.userName = name);
  }
}

The UserFacade returns an observable emitting 'Alice'.

What will the component display?

Angular
import { Injectable } from '@angular/core';
import { of } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class UserFacade {
  getUserName() {
    return of('Alice');
  }
}
AUser: undefined
BUser: Alice
CUser: null
DUser: '' (empty string)
Attempts:
2 left
💡 Hint

Think about what the observable emits and how the subscription updates userName.

state_output
intermediate
1:30remaining
What is the value of isLoading after facade call?

Given this Angular facade service and component snippet:

export class DataFacade {
  isLoading = false;
  fetchData() {
    this.isLoading = true;
    setTimeout(() => this.isLoading = false, 1000);
  }
}

@Component({ selector: 'app-data', template: `Loading: {{ facade.isLoading }}` })
export class DataComponent {
  constructor(public facade: DataFacade) {
    this.facade.fetchData();
  }
}

Immediately after fetchData() is called, what is the value of facade.isLoading?

Atrue
Bfalse
Cundefined
Dnull
Attempts:
2 left
💡 Hint

Consider the synchronous and asynchronous parts of fetchData().

📝 Syntax
advanced
2:30remaining
Which option correctly injects a facade service in Angular 17 standalone component?

Angular 17 supports standalone components and the new inject() function.

Which code snippet correctly injects UserFacade in a standalone component?

A
@Component({ standalone: true, selector: 'app-user', template: '' })
export class UserComponent {
  userFacade = inject(UserFacade);
}
B
const userFacade = inject(UserFacade);

@Component({ standalone: true, selector: 'app-user', template: '' })
export class UserComponent {
  constructor() {
    console.log(userFacade);
  }
}
C
@Component({ standalone: true, selector: 'app-user', template: '' })
export class UserComponent {
  constructor(private userFacade: UserFacade) {}
}
D
@Component({ standalone: true, selector: 'app-user', template: '' })
export class UserComponent {
  userFacade = new UserFacade();
}
Attempts:
2 left
💡 Hint

Remember that inject() is used inside class bodies, not outside.

🔧 Debug
advanced
2:30remaining
Why does this facade service cause a memory leak?

Look at this Angular facade service:

@Injectable({ providedIn: 'root' })
export class ProductFacade {
  products$;
  constructor(private api: ApiService) {
    this.products$ = this.api.getProducts();
  }
}

The component subscribes to products$ but never unsubscribes. Why is this a problem?

ABecause <code>products$</code> is a cold observable and never completes, causing subscriptions to stay active.
BBecause the facade service is not provided in the component, so Angular creates multiple instances.
CBecause the component should unsubscribe manually to avoid memory leaks from long-lived subscriptions.
DBecause the facade does not use <code>shareReplay</code> or caching, causing multiple API calls.
Attempts:
2 left
💡 Hint

Think about Angular subscriptions and component lifecycle.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the main benefit of the facade service pattern in Angular?

Choose the best explanation of why using a facade service is helpful in Angular applications.

AIt forces all components to share the same service instance, preventing any state duplication.
BIt replaces Angular's dependency injection system with a manual service locator pattern.
CIt automatically caches all API responses to improve performance without extra code.
DIt hides complex state management and API calls behind a simple interface, reducing component complexity and improving maintainability.
Attempts:
2 left
💡 Hint

Think about how facades simplify component code and separate concerns.