This is a design pattern single tone question (novice concept question)

Asked 2 years ago, Updated 2 years ago, 114 views

It hasn't been long since I started coding, so I'm lacking in concept I'm going to study design patterns for the first time

A single-toned description says, "A pattern that guarantees that there is one instance of the class and provides a global approach to access, which is the most fundamental of the design pattern."

I don't know why I have to guarantee that there is only one instance of the class

Wasn't the purpose of creating a class easy to reuse in the first place? If you create a class, you create multiple instances through it, and if you need to change it, if you change the class, each instance changes, right? But if you use a single tone, doesn't it lose the meaning of using class?

I wonder what the benefit of guaranteeing one is.

design-pattern

2022-09-21 16:36

1 Answers

It is usually required when managing system resources.

Let's say there's a shared memory called A. What if you create a class to manage it and create objects here and there? It's like you have multiple managers. Then there's going to be a lot of reading and writing. There's no way to stop someone from writing, even if they're writing somewhere else. There's no point in calling Mutex or Lock. It's a different object.

But it's simple if you can have only one administrator through a single tone. No matter where you read, write, and use this shared memory, there is only one administrator, so you can easily solve it by locking read and write.


2022-09-21 16:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.