学习Sigil开源项目设计模式之工厂模式的使用

class Importer
{
public:
    Importer(const QString &fullfilepath);
    virtual ~Importer() {}

    virtual QSharedPointer< Book > GetBook() = 0;
};

class ImporterFactory
{
public:
    ImporterFactory();
    ~ImporterFactory();

	Importer *GetImporter(const QString &filename);

private:
    Importer *m_importer_html;
    Importer *m_importer_epub;
    Importer *m_importer_txt;
};
Importer *ImporterFactory::GetImporter(const QString &filename)
{
    QString extension = QFileInfo(filename).suffix().toLower();

    if ((extension == "xhtml") ||
        (extension == "html")  ||
        (extension == "htm")
       ) {
        if (!m_importer_html) {
            m_importer_html = new ImportHTML(filename);
        }
        return m_importer_html;
    }

    if ((extension == "txt")) {
        if (!m_importer_txt) {
            m_importer_txt = new ImportTXT(filename);
        }
        return m_importer_txt;
    }

    if ((extension == "epub")) {
        if (!m_importer_epub) {
            m_importer_epub = new ImportEPUB(filename);
        }
        return m_importer_epub; 
    }

    return NULL;
}

class ImportEPUB : public Importer
{
    Q_DECLARE_TR_FUNCTIONS(ImportEPUB)

public:
    ImportEPUB(const QString &fullfilepath);

    virtual QSharedPointer< Book > GetBook();
}

class ImportHTML : public Importer
{
public:
    ImportHTML(const QString &fullfilepath);

    void SetBook(QSharedPointer< Book > book, bool ignore_duplicates);

    virtual QSharedPointer< Book > GetBook();
}

class ImportTXT : public Importer
{

public:
    ImportTXT(const QString &fullfilepath);

    virtual QSharedPointer< Book > GetBook();
}

ImporterFactory importerFactory;
Importer *importer = importerFactory.GetImporter(fullfilepath);
SetNewBook(importer->GetBook());


你可能感兴趣的:(学习Sigil开源项目设计模式之工厂模式的使用)