简体   繁体   English

Angular 8:为主题可观察设计创建外观模式,发布/订阅

[英]Angular 8: Create Facade Pattern for Subject-Observable design, Publish/Subscribe

How can I apply Facade Pattern to Subject-Observable (the Publish/Subscribe) Code below?如何将外观模式应用于下面的主题可观察(发布/订阅)代码?

We have parent component subscribing to many services.我们有订阅许多服务的父组件。 Now learned more sibling and other components in different functional areas require Some or All of the service subscriptions.现在了解到更多的兄弟和其他组件在不同的功能领域需要部分或全部的服务订阅。 What is a good way to manage and follow dry (don't repeat yourself principal).什么是管理和遵循干的好方法(不要重复自己的原则)。

Do we have to resubscribe to Same long list of services in the different components?我们是否必须重新订阅不同组件中相同的长服务列表? The services may start looking like a vast interconected cobweb, so trying to be careful.这些服务可能开始看起来像一个巨大的相互连接的蜘蛛网,所以要小心。

Trying to refer off this website:试图参考这个网站:

https://medium.com/@balramchavan/best-practices-building-angular-services-using-facade-design-pattern-for-complex-systems-d8c516cb95eb https://medium.com/@balramchavan/best-practices-building-angular-services-using-facade-design-pattern-for-complex-systems-d8c516cb95eb

Parent Receiver:家长接收者:

constructor
(
  @Inject('AddressMailingStandardService') private addressMailingStandardService: BasicService,
  @Inject('AddressMailingRuralService') private addressMailingRuralService: BasicService,



  @Inject('TypeService') private typeService: BasicService,
  @Inject('SourceService') private sourceService: BasicService,
  @Inject('PurposeOfUseService') private purposeOfUseService: BasicService,


ngOnInit() {

 this.typeService.currentMessage.subscribe(currentAddressTypeMessage => {
      this.addressTypeMessage = currentAddressTypeMessage;
      this.addressTypeData = this.addressTypeMessage;
    });

    this.sourceService.currentMessage.subscribe(currentAddressSourceMessage => {
      this.sourceMessage = currentAddressSourceMessage;
      this.sourceData = this.addressSourceMessage;
    });

    this.purposeOfUseService.currentMessage.subscribe(currentAddressPurposeMessage => {
      this.purposeOfUseMessage = currentAddressPurposeMessage;
      this.purposeOfUseData = this.addressPurposeOfUseMessage;
    });




    this.addressMailingStandardService.currentMessage.subscribe(currentMessage => {
      this.addressStandardMessage = currentMessage;
      this.addressMailingStandardData = new AddressMailingData(this.addressStandardMessage.value);
    });


    this.addressMailingRuralService.currentMessage.subscribe(currentRuralMessage => {
      this.addressRuralMessage = currentRuralMessage;
      this.addressMailingRuralData = new AddressMailingData(this.addressRuralMessage.value);
    });

Basic Service:基础服务:

export class BasicService {

  private messageSource = new Subject();
  currentMessage = this.messageSource.asObservable();

  constructor() { }

  changeMessage(currentMessage) {
    this.messageSource.next(currentMessage);
  }
}

Sender Example:发件人示例:

AddressMailingStandardService地址邮寄标准服务

this.subs.add(this.standardAddressForm.valueChanges.subscribe(data => {
  this.basicService.changeMessage(this.editAddressForm);
}));

TypeService类型服务

statusSelectedItemChanged(e) {
  this.selectedItemOutput = e;
  this.typeService.changeMessage(e);

} }

We use the Facade pattern in our application, and I think it's perfectly normal to have to subscribe to similar observables across components.我们在应用程序中使用 Facade 模式,我认为必须跨组件订阅类似的 observable 是完全正常的。 That's what gives you flexibility in the first place.这就是首先为您提供灵活性的原因。

That said, you can consider creating a Subscription in your Facade service for commonly used combinations of observables.也就是说,您可以考虑在 Facade 服务中为常用的可观察对象组合创建Subscription

Then, if a sibling component requires a similar set of observables, it can use that Subscription piecemeal from the Facade instead of having to individually subscribe to each observable within the component.然后,如果同级组件需要一组类似的 observable,它可以使用 Facade 中的Subscription ,而不必单独订阅组件中的每个 observable。

Please review this StackBlitz:请查看此 StackBlitz:

https://stackblitz.com/edit/angular-4phytx?file=app.facade.ts https://stackblitz.com/edit/angular-4phytx?file=app.facade.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM