2017-01-20 31 views
0

以下是已知的設計模式嗎?這是一個設計模式?具有多種方法從不同輸入格式創建不同對象的類

我有與多種方法來創建不同的對象從不同的輸入格式。

E.g. Apologise for the pseudocode 

// Definition 

CreatorClass() { 
    func createCat(input -> array) -> Cat{ 
    Cat aCat = Cat() 
    //...initialisation from array 
    return aCat; 
    } 

    func createCat(input -> containerClass){ 
    Cat aCat = Cat() 
    //...initialisation from containerClass 
    return aCat; 
    } 

    func createCat(input -> dictionary){ 
    Cat aCat = Cat() 
    //...initialisation from dictionary 
    return aCat; 
    } 

    func createDog(input -> dictionary){ 
    Dog aDog = Dog() 
    //...initialisation from dictionary 
    return aDog; 
    } 
} 

// Usage 
Creator aCreator = Creator() 
Cat aCat = aCreator.createCat(array) 
+0

看起來像我的工廠模式。我沒有完全閱讀它 – byxor

+1

它看起來像一個工廠方法,但這個問題不應該在這裏我猜:https://en.wikipedia.org/wiki/Factory_method_pattern –

回答

1

是的,它被稱爲Factory

+0

謝謝!我見過工廠設計模式的實現,使用協議/接口,並在類中實現「創建者方法」。這是一種不同的設計模式嗎?我有點困惑(例如https://www.tutorialspoint.com/design_pattern/factory_pattern.htm) – mm24

+1

它是相同模式的變體。 – Rik

相關問題