设计模式实践

Posted on Aug 10, 2022

策略模式

class D {
    createCode(parentId, followTaskId): string {
        const parent = this.getTaskById(parentId);
        let code = '';
        if (parent) {
            const parentCode = parent.taskNbr;
            if (followTaskId) {
                code = `${parentCode}${parentCode ? '.' : ''}${nbrArr.length ? Number(nbrArr[nbrArr.length - 1]) + 1 : ''}`;
            } else {
                code = `${parentCode}${parentCode ? '.' : ''}${children.length + 1}`;
            }
        } else {
            code = `${index + 2}`;
        }
        return code;
    }

    public createTaskResolver(data, mode) {
        const task: any = {};
        const parentId = String(data.parentId ? data.parentId : '')
        if (parentId) {
            task.parent = {id: parentId}
        }
        if (data && data.followTaskIndex >= 0) {
            task.followTaskIndex = data.followTaskIndex;
        }
        return task
    }

    public setCreatePullData(task: IProjectTask) {
        let topLevel = this.getTopLevelTasks();
        if (!oc(task.parent).id('')) {
        } else {
        }
        if (task.followTaskId && !task[F_ProjectTask_scheduledStart]) {
        }
        return toJS(task);
    }

    public getFollowTaskId(data, allData): string {
        if (data.followTaskId) {
        } else if (data.parentId) {
        }
    }
}

xx模式

class E {
    private showDialog = (
        mode: BizFormModeEnum,
        dataId: string,
        options: Optional<IBizFormDialogOptions> = {},
    ) => {
        // 如果dialogId存在,说明弹窗已经生成,那么就先关闭那个弹窗,防止多次点击出现多个弹窗
        if (this.mediator.dialogId) this.mediator.closeTaskDialog();

        this.presenter.getBean(PageBeanNames.NestFormController).showNestBizFormDialog(
            {
                fieldName: F_ProjectSchedule_tasks,
            },
            {
                title: '任务详情',
                size: 'largest',
                dataId: dataId,
                mode: mode,
                options: {
                    ...options,
                    projectScheduleAPI: this.presenter.options.projectScheduleAPI,
                    displayOptions: {
                        suppressDetailAttachmentUpload: [F_ProjectTask_deliverables]
                    },
                    onSave: (dialogOptions) => {
                        setTimeout(() => {
                            this.updateGanttData(true, dialogOptions.dataId, dialogOptions.options.taskContext.followTaskIndex);
                        }, 300);
                    }
                },
                customizeButtons: options.customizeButtons,
                presenterClass: TaskFormPresenter,
                presenterOptions: {
                    passParams: {
                        // showDialog: this.showDialog,
                        mediatorKey: this.mediatorKey,
                        passOptions: {
                            mainPresenterAttachment: (task, newFiles) => {
                                this.setTaskAttachmentAppendRow(task, newFiles);
                            },
                            mainPresenterSave: () => {
                                this.presenter.api.save()
                            },
                            mainPresenterIsEdit: () => {
                                return this.presenter.api.stateController.isEditing;
                            }
                        }
                    },
                },
            }
        )
    }
}

showNestBizFormDialog的参数是一个大型的option,而此方法中用到了其中一部分参数

interface NestBizFormDialogOption {
    nestFieldName?: string,
    title: string,
    size: string,
    dataId: string,
    mode: string,
    suppressDetailAttachmentUpload: [F_ProjectTask_deliverables],
    onSave: () => void,
    customizeButtons,
    mediatorKey,
    mainPresenterAttachment: (task, newFiles) => void,
    mainPresenterSave: () => void,
    mainPresenterIsEdit: () => void,
}