0

In my code I have 2 separate login types. I have a factory that decides which one to create based on an enum.

Each login type has a different type of credential. Currently my factory method takes just 1 type of credential. How can I generalise my method to accept both an enum for login type with different credential types.

class LoginType1 
{
    var credentials:Credentials
}

class LoginType2 
{
    var credentials:Credentials2
}
enum LoginType 
{
   case : type1,
   case : type2

}

struct Credentials
{
  var username : String,
  var pwd : String
}
struct Creditentials2 
{
  var key : String,
  var id : String
}

public func setupProvider(type: LoginType, credentials: Credentials, ) {
        switch type {
        case .type1:
            provider = LoginType1(credentials: credentials, container: view)
        case .type2:
            provider = LoginType2(credentials: credentials)
        }

    }
4
  • 1
    I'm not familiar with Swift, so I won't post an answer, but it seems logical that you would make Credentials a base class with different implementations if you expect the information contained within to change one from another. I don't know how you're using Credentials, but for whatever action, create an overrideable method to generalize behavior. – Neil Jan 30 '19 at 9:55
  • 1
    Possible duplicate of Choosing the right Design Pattern – gnat Jan 30 '19 at 9:59
  • 1
    I think stack exchange might be the wrong forum for asking naive or novice questions... – dubbeat Jan 30 '19 at 10:05
  • 1
    It's more like it is expected from peoples to have make their own research on the subject and you don't present any research based on how you would have tryed using a factory or strategy patterns to solve your problem. Furthermore digging your own problem generally help question to be more accurate (though your is in this case). As an hint you can check how Spring did it : docs.spring.io/spring-security/site/docs/4.2.4.RELEASE/apidocs/… What you're calling LoginType is what they call AuthenticationToken I guess – Walfrat Jan 30 '19 at 10:19
2

My first choice would be to get rid of the enum and have different factory methods. If Swift can do function overloading based on parameter types, that would let you keep the same name for both - but there's usually not much reason to.

If you have good reasons to keep it in a single method, my second choice would be to create a common interface for the credentials, so that the factory method can accept both. That interface should provide enough functionality to be used by the LoginTypes without having to down-cast to the concrete Credentials. Perhaps you could make the Credentials consume LoginTypes instead

If that's not possible, the last option would be to use a "dummy" interface which doesn't expose any functionality, and then down-cast to the types you "know" are allowed. This really is a last resort, and (based just on the example code) I see no reason not to pick the first option in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.