要求子类在不公开的情况下实现方法

时间:2017-10-27 02:54:16

标签: javascript typescript

我有以下界面:

import * as Bluebird from "bluebird";
import { Article } from '../../Domain/Article/Article';

export interface ITextParsingService {
    parsedArticle : Article;
    getText(uri : string) : Bluebird<any>;
}

以及实现这种接口的以下类:

import * as Bluebird from 'bluebird';
import * as requestPromise from 'request-promise';
import ApplicationConfiguration from '../../../../config/ApplicationConfiguration';
import { ITextParsingService } from '../ITextParsingService';
import { Article } from '../../../Domain/Article/Article';

export class DiffbotTextParsingService implements ITextParsingService {
    public parsedArticle: Article;
    private articleApiEndpoint;

    constructor() {
        this.articleApiEndpoint = "https://api.diffbot.com/v3/article";
    }

    public getText(url: string) : Bluebird<any> {
        return requestPromise(this.articleApiEndpoint, {
            qs : {
                "url" : url,
                "token" : ApplicationConfiguration.Diffbot.developerToken
            },
            headers : {
                'User-Agent' : 'Request-Promise'
            },
            json : true
        }).then((diffbotResponse) => {
            this.mapReponseToArticle(diffbotResponse.objects[0]);
        }).catch((errorMessage) => {
            return errorMessage;
        })
    }

    private mapReponseToArticle(response : any) {
        this.parsedArticle = new Article(response.diffbotUri,
                                         response.title,
                                         response.author,
                                         new URL(response.authorUrl),
                                         response.text,
                                         new URL(response.pageUrl),
                                         new Date(response.date),
                                         response.humanLanguage);
    }
}

如何要求所有实现ITextParsingService的类都实现mapResponseToArticle,它基本上接受服务的响应并将其映射到公共域对象?我没有看到为什么它应该是一个公共方法的任何原因,但我也看不出如何通过实现类来强制执行这样的要求。有关替代模式的任何建议吗?

1 个答案:

答案 0 :(得分:1)

  

ITextParsingService还实现了mapResponseToArticle,它基本上接受了服务的响应并将其映射到公共域对象?我不明白为什么它应该是一种公共方法,但我也无法看到如何通过实施类来强制执行这样的要求。

没有办法实现这一目标。

  

对替代模式的任何建议?

使用通用命名约定并将其命名为_mapResponseToArticle_前缀是内部资源的传统JS,在原始JS中是真实的,private不可用(今天......即将推出)

相关问题