r/developersIndia • u/kingpin944 • Mar 26 '25
General Hello Devs, let's teach each other one quick and easy concept
I'll start.
Singleton Design Pattern: Ensures that only one instance of a class exists. This can be achieved in several ways, typically by making the constructor private and exposing a static factory method to return the same instance to all callers.
A poorly implemented Singleton can be affected by the following factors:
- Serialization: If a Singleton class implements
Serializable, deserializing it can create a new instance, breaking the Singleton property. Solution: ImplementreadResolve()to return the same instance during deserialization. - Thread Safety: If two threads access a lazily initialized Singleton simultaneously, they may create multiple instances. Solution: Use synchronization, double-checked locking, or eager initialization.
- Reflection: Even if a class has a private constructor, reflection can be used to access it and create multiple instances. Solution: Throw an exception inside the constructor if an instance already exists.
Using an enum is the safest way to implement a Singleton, as it inherently prevents all the above issues.
public enum Singleton {
INSTANCE;
}
450
Upvotes
•
u/AutoModerator Mar 26 '25
It's possible your query is not unique, use
site:reddit.com/r/developersindia KEYWORDSon search engines to search posts from developersIndia. You can also use reddit search directly.Recent Announcements
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
18
u/[deleted] Mar 26 '25
strategy Pattern my beloved
4
u/FeelingLowSmokeGreen Full-Stack Developer Mar 26 '25
I guess you work with .Net?
241
u/Shot_Double Mar 26 '25
Hashmap is always the answer.
188
u/p-4_ Mar 26 '25
OP should be extra elaborate. Explain it like you are talking to a first year student.
87
u/Away-Tomorrow199 Mar 26 '25
How can I overcome anxiety and avoid forgetting things during interviews?
74
u/arav Site Reliability Engineer Mar 26 '25
A quick and easy concept -> Read only Fridays. Never deploy anything ever on Fridays.
32
u/Ak-xenon15 Software Engineer Mar 26 '25
Then let's votes for the next topic it would be explained by someone senior developer
48
u/Primary-Editor-9288 Backend Developer Mar 26 '25
Love this initiative OP
20
u/[deleted] Mar 26 '25
How does this enum thing work for this case? Can someone explain?
32
u/big-dix-smol-chix Mar 26 '25 edited Mar 26 '25
Can you explain what sterilization means? Is it a language specific concept?
Serialization**
11
u/_DustynotRusty_ Mar 26 '25
How do I remember SOLID principles effectively and how do I recognise where they should be implemented? And about dependency injection.
10
u/teut_69420 Mar 26 '25
This is more a guideline than 1 quick concept for juniors and are pretty easy to learn. These might sound dogmatic but honestly improves the code and the experience by a lot:
Lets say, you are storing a integer x, if you are casting it to a decimal for some reason. I dont mind the name to be xAsDecimal (ofcourse given x is a proper name in the first place).
Secondly, abbreviations. Stop using abbreviations, unless it's worldwide known and has just 1 meaning.
If i write var gc = 1; what does gc mean? I need to read More to find it. Gc might mean garbage collection, google cloud or even group chat.
3. Follow company coding standards. If lets say they use python but camel case, dont bteak it.
4. Invert your if's. Anything beyond 2 nested if's i dont consider. Usually depends on company standards but more than 2, better to invert.
5. Alignment of your code.
6. Write UT for everything. Class is throwing an error, test it. Logging something somewhere, test it, test anything a function is doing.
8
u/[deleted] Mar 26 '25
mutual TLS is a good way to keep your self hosted services private without using a vpn service.
6
u/Individual-Moment-43 Full-Stack Developer Mar 26 '25
This is a question I faced in an interview. So I thought I'd share.
What is the Liskov Substitution Principle (LSP) in SOLID principles, and can you provide an example of its violation?
Answer:
The Liskov Substitution Principle (LSP) states that a subclass should be able to replace its base class without affecting the correctness of the program. In other words, if class B is a subclass of class A, then objects of A should be replaceable with objects of B without causing unexpected behavior.
When a subclass modifies or restricts the behavior of its base class in a way that breaks expectations, it violates LSP.
Example of LSP Violation: Birds and Flying
In nature, not all birds can fly—for example, penguins cannot. If we model a
Birdclass with aFly()method and then inheritPenguinfrom it, we violate LSP because Penguin cannot fly.4
u/Individual-Moment-43 Full-Stack Developer Mar 26 '25
In .NET, a Singleton can be registered using
services.AddSingleton<MySingleton>(). The framework ensures that only one instance exists, and this same instance is injected wherever needed.3
u/zamnbruhh Mar 26 '25
You can always use Meyers singleton for thread safety.
3
u/noJobenn Mar 28 '25
In cybersecurity one of fundamental is cia triad.
1.Confidentiality : simply means that unauthorised person should not get any access from data. And checking the authenticity of author is part of confidentiality. Ex password protected pdf
2.Integrity : simply means that data should not changed or tampered in any from from sender to receiver. This can be achieved from using hash encryption and many more. Ex pdf should be encrypted and that encryption should be Powerful enough for requirement. And when data is tampered or leaked it should be informed.
3.Availability : simply means that data should be available for authenticated user easily like garden approach outer walls are high but when you enter an garden the contents of garden should be easily available. Ex like after getting authenticated for email user should have all control.
In system/application/building/many more this cia triad should be implemented equally not just part of it. If one is compromised then it just hell mary after that.
Extreme cases of triad which is useless in many applications. 1.Confidentiality: data is simply transfer to an harddrive and that hard drive is sent to the space so no unauthorised person gets that data.its just dumb .
2.Integrity: the encryption is sooo large and resources intensive like doing 100 years of computing power to just encrypt an file for a post on insta about an person dog. It's just dumb
3.Availability : so sensitive data is just posted on website without any authentication so people gets there's hands on it without any problem it's just dumb.
Thanks for reading till here 😁
2
u/theStrider_018 Security Engineer Mar 26 '25
You can directly log into your linux-root without password via GRUB by editing the bootloader.
Just in case, you guys are prone to forgetting passwords.
2
u/calm-blue-birdd Fresher Mar 26 '25
With a sufficient number of users of an API,
it does not matter what you promise in the contract:
all observable behaviors of your system
will be depended on by somebody.
1
u/udaayip Mar 26 '25
Does this only apply to OOP languages?
1
u/Fun-Understanding862 Mar 27 '25
history | grep <command>
The best linux line ik and the most useful one.
Example usage : history | grep "docker"
Lists out all the previous docker commands u have used.
If your history is expanding rapidly try : Cat ~/.bash_history | grep "docker"
Additional note : You will get the id of command , if the command is very big to copy paste then :
!id
Example usage : !5 5th id command will get executed
1
u/NoNameDotCPP6769 Staff Engineer Mar 27 '25
Learn SOLID principles very properly. You’ll use them subconsciously.
1
u/Additional-Stable-50 Mar 27 '25
Oh my god, OOP users have to think about these things instead of focusing on solving the problem. :-(
That's why functional programming is superior lmao. The computer doesn't think in an object oriented way. It's just unwanted complexity.
0
u/PurpleIntelligent326 Mar 28 '25
Are you sure there is no typo?
97
u/[deleted] Mar 26 '25
[removed] — view removed comment
9
u/[deleted] Mar 26 '25
[removed] — view removed comment