-2

I'm using a singleton in a Swift 3project I've built. As part of the project I have registration and login view controllers and I figured I might want registration/login in future projects. So for code reuse I thought about having the registration and login functions in separate .swfit classes and then I'd instantiate these classes in the singleton.

Or just have the registration/login functions as static functions in separate files.

I just wasn't sure if that breaks the idea of a singleton and that all the registration/login functions should be in the singleton.

It's a bit hard to come by practical examples of design patterns in my college degree studies, teachers like data structures, and many other coding topics but not so much about design patterns and how to write better structured code.

Mike
  • 1,281
  • 3
  • 14
  • 41
  • I removed your second question " Also, if anyone knows where I can find more practical code examples for use of closures, @escaping, completion handlers that'd be great." You should ask that in another SO question, but actually that is not a valid SO question, that is a Google search and read Apple's documentation – Brian Ogden Sep 17 '17 at 16:38
  • Also, I fixed multiple spelling errors to your question, you should really write proper English, that is clearly understandable at all times – Brian Ogden Sep 17 '17 at 16:39
  • If you have code that works and you're soliciting code review, visit http://codereview.stackexchange.com (including the relevant code in your question). In answer to your question, I'm wondering why this is a singleton at all. It wouldn't seem like it meets the criteria for a good candidate for singleton. See https://stackoverflow.com/questions/1300655/whats-alternative-to-singleton for discussion of alternatives to singletons. Or search the web for "singletons are evil" (which despite the leading search phrase, will turn up links for when to use singletons and when not to). – Rob Sep 17 '17 at 16:46
  • I disagree with you @Rob, though you may be correct that this question is not appropriate for Stackoverflow, if you are, I think that the Stackoverflow community is missing something very important to Software Development and that is, obviously: answering questions about Design Patterns – Brian Ogden Sep 17 '17 at 16:50
  • 1
    @BrianOgden I disagree with your disagree. The question is essentially asking for opinions. That's disallowed. And in any case it is extremely broad and open-ended. – matt Sep 17 '17 at 16:56
  • 1
    @BrianOgden If you want questions about Software Engineering and design patterns, consider [Software Engineering](https://softwareengineering.stackexchange.com/). Stackoverflow is meant for more concrete programming issues. – rmaddy Sep 17 '17 at 17:24
  • @rmaddy nice, thanks – Brian Ogden Sep 17 '17 at 17:38
  • @rmaddy when referring other sites, it is often helpful to point that [cross-posting is frowned upon](https://meta.stackexchange.com/tags/cross-posting/info) – gnat Sep 17 '17 at 20:47
  • @gnat - Cross posting may be frowned upon, but so is posting on the wrong forum. We’re not suggesting he post to multiple sites, just pointing out that this simply isn’t the right one. – Rob Sep 18 '17 at 03:56

2 Answers2

1

For better learning Design Pattern I recommend the fun and easy reading Head First Design Patterns

Regarding your question about a Singleton pattern in Swift code is always best learned with an example:

final class AccountUtility {
    //here is object instantiation to meet the Singleton Design Pattern single instance requirement
    static let shared = AccountUtility()

    //a private constructor ensures no one can create an instance of this class
    private init() {

    }

    func login() {

    }

    func register() {

    }
}

To use this Singleton Swift class:

AccountUtility.shared.login()
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • @Rob that is interesting, so if I make it a class that issue would solved correct? – Brian Ogden Sep 17 '17 at 17:47
  • Hello all. I would just like to say there have been quite a few comments so whoever gave me a -2 are you sure that shouldn't be a +2. And yep...design patterns and coding patterns in general including the use of closures, delegates, etc. I think are covered poorly both at the college level and possibly on stackoverflow as well. And thanks for the links to the various sites and books @rmaddy. Cheers. – Mike Sep 18 '17 at 06:00
  • no sorry, @Mike, the question is not appropriate for Stackoverflow as matt said: "The question is essentially asking for opinions. That's disallowed. And in any case it is extremely broad and open-ended." Design patterns are great and I am glad you have awareness of them outside of what your college has show you so far, your question would have been appropriate in https://softwareengineering.stackexchange.com/. Stackoverflow is for code, most questions that do not include a code attempt will typically be marked down on this website – Brian Ogden Sep 18 '17 at 19:18
1

In general, you should regard view controllers as not reusable, because they mediate between this particular data and this particular interface (model-view).

But if you can isolate this particular functionality into an independent object, that would make it reusable. What I usually do is make a custom struct and have the view controller own this as a helper object. I don't quite see the point of the putting the struct declaration in a separate file, since it's easy to copy and paste, but you can certainly do that if you like.

(Nor do I see what any of this has to do with Singleton.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I did worry that the OP was thinking of reusing the ViewControllers themselves but the OP does state that he/she wants to move login/registration functionality to separate Swift classes – Brian Ogden Sep 17 '17 at 16:52
  • @BrianOgden In Swift, structs will almost always be a better choice. Apple has a 2016 WWDC video discussing the use of helper structs. – matt Sep 17 '17 at 16:55
  • matt, out of curiosity, to which video are you referring? The [Protocol and Value Oriented Programming in UIKit Apps](https://developer.apple.com/videos/play/wwdc2016/419/)? – Rob Sep 17 '17 at 17:39
  • Lol, I'm not seeing the reference to which you allude. If you ever stumble across it, let me know. – Rob Sep 17 '17 at 18:05
  • @matt, see Rob's comments in my answer, he has a good point about the downside of using value types to implement the Singleton pattern – Brian Ogden Sep 17 '17 at 18:17
  • @BrianOgden - (Sorry, I deleted those comments because with the changed answer, they no longer made sense.) My point was merely that value-type singleton is a flawed concept. But when matt talks about "helper" type, he wasn't necessarily talking about singleton. – Rob Sep 17 '17 at 18:22
  • @Rob I think hit on a very good point though, let accountUtility = AccountUtility.shared would allow more than one instance of the AccountUtility to be available, using a class prevents this – Brian Ogden Sep 17 '17 at 18:33