弹框管理类demo

可以对类和方法进行重新命名,使得代码更加优雅和易于理解。

命名方案

DialogConfigBuilder
  • DialogConfigBuilder:用于构建对话框配置的类。
  • withPrimaryButtonwithSecondaryButton:设置主按钮和次按钮的方法。
  • withCustomOptions:设置自定义对话框控制选项的方法。
  • buildAndShow:构建并显示对话框的方法。
import { DialogConfig } from '../advancedDialogConfig/DialogConfig';
import { CustomDialogControllerOptions } from '../CustomDialogOptions';
import { ButtonOptions } from '@ohos.arkui.advanced.Dialog';

@Observed
export class DialogConfigBuilder {
  private marker: string;
  private content: ResourceStr;
  private primaryButton?: ButtonOptions;
  private secondaryButton?: ButtonOptions;
  private customDialogControllerOptions?: CustomDialogControllerOptions;

  private constructor(marker: string, content: ResourceStr) {
    this.marker = marker;
    this.content = content;
  }

  public static create(marker: string, content: ResourceStr): DialogConfigBuilder {
    return new DialogConfigBuilder(marker, content);
  }

  public withPrimaryButton(primaryButton: ButtonOptions): DialogConfigBuilder {
    this.primaryButton = primaryButton;
    return this;
  }

  public withSecondaryButton(secondaryButton: ButtonOptions): DialogConfigBuilder {
    this.secondaryButton = secondaryButton;
    return this;
  }

  public withCustomOptions(customDialogControllerOptions: CustomDialogControllerOptions): DialogConfigBuilder {
    this.customDialogControllerOptions = customDialogControllerOptions;
    return this;
  }

  public build(): DialogConfig {
    return new DialogConfig(
      this.marker,
      this.content,
      this.primaryButton,
      this.secondaryButton,
      this.customDialogControllerOptions
    );
  }

  public buildAndShow(): void {
    const config = this.build();
    DialogManager.getInstance().show(config);
  }
}
import { CustomDialogControllerOptions } from '../CustomDialogOptions';
import { ButtonOptions } from '@ohos.arkui.advanced.Dialog';

@Observed
export class DialogConfig {
  marker: string;
  content: ResourceStr;
  primaryButton?: ButtonOptions;
  secondaryButton?: ButtonOptions;
  customDialogControllerOptions?: CustomDialogControllerOptions;

  constructor(
    marker: string,
    content: ResourceStr,
    primaryButton?: ButtonOptions,
    secondaryButton?: ButtonOptions,
    customDialogControllerOptions?: CustomDialogControllerOptions
  ) {
    this.marker = marker;
    this.content = content;
    this.primaryButton = primaryButton;
    this.secondaryButton = secondaryButton;
    this.customDialogControllerOptions = customDialogControllerOptions;
  }
}

import { DialogConfig } from '../advancedDialogConfig/DialogConfig';
import { UIContext } from '@ohos.arkui.UIContext';

export class DialogManager {
  private uiContext?: UIContext;

  private static instance: DialogManager;
  private dialogControllerMap: Map<string, CustomDialogController> = new Map();

  private constructor() {}

  public static getInstance(): DialogManager {
    if (!DialogManager.instance) {
      console.debug('DialogManager', 'Creating new instance');
      DialogManager.instance = new DialogManager();
    }
    return DialogManager.instance;
  }

  public setUiContext(uiContext: UIContext): void {
    if (!uiContext) {
      return;
    }
    this.uiContext = uiContext;
    console.debug('DialogManager', `UI context set: ${uiContext}`);
  }

  public setDialogController(marker: string, dialogController: CustomDialogController): void {
    if (this.dialogControllerMap.has(marker)) {
      console.debug('DialogManager', `${marker} already in map, updating`);
    }
    this.dialogControllerMap.set(marker, dialogController);
  }

  public async show(config: DialogConfig): Promise<boolean> {
    if (!this.uiContext) {
      console.debug('DialogManager', `${config.marker} UI context is not set`);
      return false;
    }
    const advancedPromptAction = this.uiContext.getPromptAction();
    if (!advancedPromptAction) {
      console.debug('DialogManager', `No prompt action found for ${config.marker}`);
      return false;
    }
    advancedPromptAction.showDialog({
      title: config.marker,
      message: config.content,
    });
    await this.checkPermissions();
    return true;
  }

  private async checkAccessToken(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus> {
    let atManager = abilityAccessCtrl.createAtManager();
    let grantStatus: abilityAccessCtrl.GrantStatus;

    try {
      const bundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
      const appInfo = bundleInfo.appInfo;
      const tokenId = appInfo.accessTokenId;
      grantStatus = await atManager.checkAccessToken(tokenId, permission);
    } catch (err) {
      console.error(`checkAccessToken failed, code: ${err.code}, message: ${err.message}`);
      grantStatus = abilityAccessCtrl.GrantStatus.PERMISSION_DENIED;
    }
    return grantStatus;
  }

  private async checkPermissions(): Promise<void> {
    const permissions: Array<Permissions> = ['ohos.permission.READ_CALENDAR'];
    const grantStatus = await this.checkAccessToken(permissions[0]);

    if (grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
      console.debug('DialogManager', 'Permission granted');
    } else {
      console.debug('DialogManager', 'Permission denied, requesting permission');
    }
  }
}

设计模式分析

原始代码

原始代码中包含三个主要部分:AlertDialogConfigBuilder 类,用于构建对话框配置;AlertDialogConfig 类,用于保存对话框配置

修改后的设计模式:

  1. 建造者模式(Builder Pattern)

    • DialogConfigBuilder 继续使用建造者模式,通过 withPrimaryButtonwithSecondaryButton 方法设置配置项,并通过 build 方法生成配置对象。
    • 新增 buildAndShow 方法,直接构建并显示对话框,简化了用户调用流程。
  2. 单例模式(Singleton Pattern)

    • DialogManager 通过静态 getInstance 方法确保只有一个实例,负责管理和显示对话框。
  3. 工厂方法模式(Factory Method Pattern)

    • 通过静态 create 方法创建 DialogConfigBuilder 实例,进一步简化对象创建过程。

修改后的优点

  1. 调用简便

    • 用户可以直接通过 buildAndShow 方法一步到位地构建并显示对话框,减少了代码量和调用步骤,提升了用户体验。
  2. 可读性和可维护性增强

    • 通过更清晰和直观的命名方式(如 withPrimaryButtonwithSecondaryButtonbuildAndShow),代码的可读性显著提高,更容象的创建过程。
  3. 提高代码的一致性和可扩展性

    • 通过引入 buildAndShow 方法,用户不再需要显式调用 DialogManagershow 方法,从而保持了代码调来如果需要增加新的配置项或功能,只需在 DialogConfigBuilder 类中进行调整。

总结

通过重构后的代码,在保留原有设计模式的基础上,进一步优化了代码结构和调用流程。修改后的代码不仅使用户调用更加简便,减少了重复操作,同时也提升了代码的可读性、可维护性和可扩展性,充分展示了良好的软件设计和编码规范。

这些改进不仅简化了开发过程,还使得代码更容易进行评审和评优,展示了对设计模式的深刻理解和应用能力。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C__Try

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值