Careful With “Singleton” Lookalikes (WAY TOO COMMON)

  Рет қаралды 16,402

Essential Developer

Essential Developer

Күн бұрын

Пікірлер: 41
@jasonokoro8061
@jasonokoro8061 4 жыл бұрын
I was looking for a video not only describing but showing the pitfalls of singletons with unit testing and yours was about as close as I could get to an actual walkthrough. Great content, great advice, great inspiration.
@EssentialDeveloper
@EssentialDeveloper 4 жыл бұрын
Glad it was helpful!
@tityseptiani8584
@tityseptiani8584 6 жыл бұрын
Wow, thank you very much for the explanations! I was excited when you said you'd create a video about singleton. I had to replay the video twice to really get everything you're talking about. I think I'm currently on the Step 3 for my own API client. And it's true that looking at diagrams always scare me because most of the time I don't know how to read them and understand them. Nowadays, I've been trying to get use to creating diagrams whenever I want to do something, to get the better picture of the whole thing. I'll try to implement the step 4 and see how it works. Anyway, I'm still waiting for your premium courses :D Thank you very much Caio and Mike!
@gordonsmith6326
@gordonsmith6326 6 жыл бұрын
Such awesome content again, incredibly refreshing to stumble across a channel like yours. Thank you for breaking this down 👌🏽
@waheedafolabi6929
@waheedafolabi6929 3 жыл бұрын
Wow...thanks Mike and Caio for this great content!
@bibinjacob123
@bibinjacob123 6 жыл бұрын
Hey,,I like your style. Could you help developers build a singleton networking layer that can be used in all of their projects.
@incelemeTRe
@incelemeTRe 2 жыл бұрын
This session was awesome. Thank you very much.
@EssentialDeveloper
@EssentialDeveloper 2 жыл бұрын
Glad you enjoyed it!
@kanishkaralasi7408
@kanishkaralasi7408 3 жыл бұрын
OMGG! THIS IS EXACTLY WHAT I NEEDED! SO THOUGHTFUL AMD DETAILED 🙌🏼 💯
@IngviMcFly
@IngviMcFly 4 жыл бұрын
That the main topic I've been facing for many years when work with legacy code projects. Guys, Thanks for additional information on this topic.
@EssentialDeveloper
@EssentialDeveloper 4 жыл бұрын
Glad it was helpful!
@jeraldo4571
@jeraldo4571 4 жыл бұрын
Great video. Focusing from start to a path that leads to a good solution. One thing Im trying to understand about modularization, what if the models are shared between different modules? And modules are maintained in different projects? Like an authorization framework should return a loggedInUser that is also used in another module?
@EssentialDeveloper
@EssentialDeveloper 4 жыл бұрын
Hi Jeraldo, you can create a shared module to hold those shared models. But beware that the more clients depend on a module, the harder it can be to change it without breaking clients and redeploy it to all clients. When needed, a solution is to create domain-specific models and DTOs to transfer them without coupling those modules. ✅
@glebcherkashyn2357
@glebcherkashyn2357 6 жыл бұрын
Another super useful lesson! Once again I see some evident improvements in my projects. Looks like now I understand last of SOLID principles :D Still, at level 4 you can not just take module and use it in another project. You'll have to write adapter, and I'm not sure that it can save too much time... And you have to be sure, that another project has generic ApiClient, otherwise you can't write the adapter =). But if you want replace module to YOUR project, 99% that ApiClient is same there, and the only thing you need is to define new requests, and in this look like you don't need adapter. Am I wrong? In my practice I use generic ApiClient based on Alamofire and Endpoint protocol (provides path, headers, attributes etc.), and generic mapper based on Decodable. So when I need to get something I just say 'hey, give me result with endpoint converted to some type" where endpoint is enum instance that conform 'Endpoint' protocol, and I provide type of model object that conforms Decodable. I wonder which level I'm on now =)
@EssentialDeveloper
@EssentialDeveloper 6 жыл бұрын
Hi Gleb! In our example, the extensions *are* the adapters! In Swift, we can have cross-module extensions which facilitate this approach. Your solution sounds similar to this. As long as the dependencies are clear and pointing in the right direction, your code can be simple and composable. ✅⛩
@chaitanyadeshpande6060
@chaitanyadeshpande6060 3 жыл бұрын
Dammmm this was so well explained 👏
@VirenderDall
@VirenderDall 2 жыл бұрын
Hi Caio, Do we need to use weak here or it's not required? class AppCache { static var shared = AppCache() private let queue = DispatchQueue(label: "com.demo", attributes: .concurrent) private var sessionData: [String : String] = [:] func setSessionData(key: String, value: String) { queue.async(flags: .barrier) {[weak self] in self?.sessionData[key] = value } } }
@EssentialDeveloper
@EssentialDeveloper 2 жыл бұрын
Hi Virender! The queue will eventually release the closure after the async call is done (it doesn't retain the closure after the async operation returns). So weak is not needed in this case to prevent retain-cycles because there won't be a memory leak if it's a strong reference. You only need to make it weak if you want to prevent holding the instance for longer than needed (in case the cache can be deallocated while async operations are scheduled) ✅
@david-tracy
@david-tracy 6 жыл бұрын
So using final allows you subclass or does it allow you to create extensions?
@EssentialDeveloper
@EssentialDeveloper 6 жыл бұрын
Hey, you cannot subclass a "final class." But you can create extensions on any class (even if it's final!). ✅⛩ For more info: docs.swift.org/swift-book/LanguageGuide/Inheritance.html
@abhishekbedi1432
@abhishekbedi1432 6 жыл бұрын
Hey Ciao . Can u share the gist on github
@EssentialDeveloper
@EssentialDeveloper 6 жыл бұрын
Hey, here it is gist.github.com/caiozullo/547ed5483221e6cb9509e4134cb1ce09 ✅⛩
@theprimalstocks
@theprimalstocks 3 жыл бұрын
There is no swizzing in swift?
@EssentialDeveloper
@EssentialDeveloper 3 жыл бұрын
Swizzling only works for obj-c methods because it requires the obj-c runtime. So you'd need to mark the methods as @objc. Latest Swift versions allow dynamic replacement, but it's more limited.
@VirenderDall
@VirenderDall 2 жыл бұрын
abstract public class Printer { abstract void print(); } public class HPPrinter extends Printer { private static HPPrinter instance; private HPPrinter() { // private constructor } synchronized public static HPPrinter getInstance() { if (instance == null) { instance = new HPPrinter(); } return instance; } @Override void print() { } } Is this we consider Singleton or singleton or some other type of $ingleton
@EssentialDeveloper
@EssentialDeveloper 2 жыл бұрын
Hi Virender, that's a Singleton because it enforces only a single instance (static lazy instance with a private initializer). In Swift: ``` class HPPrinter { static let instance = HPPrinter() private init() {} } ``` *static let is lazy and synchronized by default in Swift
@VirenderDall
@VirenderDall 2 жыл бұрын
@@EssentialDeveloper and we can easily use this in testing
@EssentialDeveloper
@EssentialDeveloper 2 жыл бұрын
Ideally, you shouldn't access the Singletons directly in your components. Otherwise, you can't replace them while testing.
@pankajjha9052
@pankajjha9052 4 жыл бұрын
In MockAPIClient class I am getting"super.init isn't called" and I think its a valid error ,why it is not there in your case here , am I missing something here?
@EssentialDeveloper
@EssentialDeveloper 4 жыл бұрын
Hello! You do need to call `super.init`. Xcode didn't show the error in the video until we built the project.
@pankajjha9052
@pankajjha9052 4 жыл бұрын
@@EssentialDeveloper Thanks for clarifying
@hithakshikulal2320
@hithakshikulal2320 2 жыл бұрын
@@EssentialDeveloper Calling `super.init` will throw an error "'APIClient' initialiser is inaccessible due to 'private' protection level". Does it mean there is no difference in using static var instead of static let ? In this case, do we have to remove private access from initialiser to make it testable?
@EssentialDeveloper
@EssentialDeveloper 2 жыл бұрын
Hi Hithakshi. In this case, the initializer cannot be private. That's the "lowercase" singleton version (there's a singleton shared instance, but you're still able to create new instances - like URLSession does, for example).
@hithakshikulal2320
@hithakshikulal2320 2 жыл бұрын
@@EssentialDeveloper Okay. Got it. Thank you 😊
@PS-dp8yg
@PS-dp8yg 6 жыл бұрын
Love your videos, but where to start for beginners? Are you planning to do beginners videos?
@EssentialDeveloper
@EssentialDeveloper 6 жыл бұрын
Hi! It depends on your level. If you're a beginner but familiar with the basics of programming already, we recommend to you our course "Swift Feed App Case Study" - academy.essentialdeveloper.com . We're covering advanced topics but with an approachable methodology. It's been popular among beginners as well. We believe you'll greatly benefit from it as others have so far. ✅⛩
Composable Code Can Be Simple - Intro to dependency diagrams and composition
18:35
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
iOS Developer Mock Interview | Tech Round (Round-2)
31:37
"Stop Using Singletons in .NET!" | Code Cop #009
13:52
Nick Chapsas
Рет қаралды 77 М.
Singleton - Good or Bad?
9:41
iCode
Рет қаралды 11 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 84 М.
How to Become a Highly Employable C# Developer in 2025
20:10
Gavin Lon
Рет қаралды 2 М.
go is great i hate it
14:44
SST
Рет қаралды 28 М.
The Biggest Mistake Intermediate React Developers Make
18:32
Cosden Solutions
Рет қаралды 39 М.