ios - Swift: Factory Pattern, Subclass methods are not visible -


i new programming in swift , have reached blocker in constructing factory design pattern. below code:

protocol iinterface {    func interfacemethod() -> void }  public class subclass: iinterface {    init() {}     func interfacemethod() { }     public func subclassmethod() { } }  public class factory() {     class func createfactory() -> iinterface {          return subclass()     } } 

finally, trying access below

let mgr = factory.createfactory()  //calling interface method mgr.interfacemethod() -- works, when keep dot (mgr.) shows method name //calling subclass method mgr.subclassmethod() -- doesn't work, when keep dot (mgr.) doesnt show subclass method name 

even if use mgr.subclassmethod, throws error saying

value of type iinterface has no member subclassmethod

i believe getting exposed protocol methods though subclass object returned via factory

all examples browsed , have seen shows how consume methods specified protocol, haven't seen example shows how consume subclass's own methods apart protocols methods

you miss point of factory pattern. idea of factory pattern provide methods have specific return type , return instances either has type or inherits type (if it's class type) or conforms type (if it's protocol).

protocol animal {     func voice() -> string }  class dog: animal {     func voice() -> string {         return "bark"     }      func sit() {     } }  class cat: animal {     func voice() -> string {         return "meow"     } }  class animalfactory {     func getanimal() -> animal {         return dog()     } } 

client code calling factory method should not speculate return value , try cast concrete class. that's totally ruining point of using factory pattern.

as can see in above example animalfactory.getanimal() return instance of type conforms animal protocol. code calling method not know , should not know specific type of instance.

if code calling factory method expects returning instance has type dog or inherits type should create , use separate dogfactory:

class evildog: dog {     override func voice() -> string {         return "bark-bark"     } }  class dogfactory {     func getdog() -> dog {         return evildog()     } } 

you can have situation when client code implement different behavior dependent on real type of instance returned factory method. in case animalfactory should implement methods providing instances of types used in client code:

class animalfactory {     func getdog() -> dog {         return evildog()     }      func getcat() -> cat {         return cat()     } }  func trainanimal(ilikedogs: bool, animalfactory: animalfactory) {     if ilikedogs {         let dog = animalfactory.getdog()         dog.voice()         dog.sit() // dog can sit     } else {         let cat = animalfactory.getcat()         cat.voice()     } } 

actually there 3 patterns - factory, abstract factory , factory method. can read differences here.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -