Member-only story
Advanced Angular Design Patterns — Factory, Facade, Proxy, and Dependency Injection
2 min readDec 24, 2024
Continuing our exploration of Angular design pattern, this part focuses on Factory, Facade, proxy and Dependency injection- essential tools for creating scalable , efficient application.
Factory Pattern
Purpose: Creates objects without specifying their exact class.
Angular Use: Configurable services via useFactory.
Example
export function configFactory() {
return new ConfigService({ apiEndpoint: 'https://api.example.com' });
}
@NgModule({
providers: [{ provide: ConfigService, useFactory: configFactory }],
})
export class AppModule {}
Facade Pattern
Purpose: Provides a simplified interface to complex systems.
Angular Use: Centralized logic for managing state or services.
Example
@Injectable({ providedIn: 'root' })
export class UserFacade {
constructor(private userService: UserService, private authService: AuthService) {}
getUserDetails(userId: string) {
return this.authService.isLoggedIn() ? this.userService.getUserById(userId) : throwError('Not logged in');
}
}
Proxy Pattern