How can I implement @Singleton annotation?

user1401472

Possibly a repeated question. But I need to implement something like

@Singleton
public class Person {
}

That will ensure only single instance of Person object.

One way is to make constructor private. But that makes the Singleton annotation redundant.

I could not really understand if I can really restrict object creation to single object without making the constructor private.

Is that even possible?

gd1

No annotation can prevent a class from being instantiated. However, if you plan to implement something like a Dependency Injection framework, or just a simple object factory, then you can use reflection to read the annotation and prevent the class from being instantiated more than once, but I understand this is not the answer you were looking for.

You can actually think about dropping the singleton pattern and moving to some more modern solution like a proper DI framework, which can give you the same result - with more flexibility.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How can I implement jQuery validation like MVC data annotation?

From Dev

Can I implement Singleton Design Pattern like this in Scheme?

From Dev

Can I implement Singleton Design Pattern like this in Scheme?

From Dev

How can I implement IServiceProvider?

From Dev

How i can use context in Singleton?

From Dev

How can I test a store singleton?

From Dev

How can I debug a custom annotation in Lombok?

From Dev

How can I add an Annotation in dygraph?

From Dev

How can I get a generic type annotation

From Dev

How can I add annotation in ggplotly animation?

From Dev

How can I know if an annotation is already on the mapview

From Dev

How to implement a singleton provider for qmlRegisterSingletonType?

From Dev

How to Implement Singleton class in Swift

From Dev

How to implement singleton in shell script

From Dev

How to implement singleton pattern in subclass

From Dev

How can i implement a scanning bar animation?

From Dev

How can I implement this logic better

From Java

how can i implement syntax coloring on a jtextpane

From Dev

How can I implement a RESTful Progress Indicator?

From Dev

How can I implement a '-Help' switch in PowerShell?

From Dev

Load balancing server, how can I implement it?

From Dev

How can I implement OnClickListener of an ImageView in a fragment?

From Dev

How can I implement comparable interface in go?

From Dev

How can I implement a dynamic selection?

From Dev

How can I implement asyncio websockets in a class?

From Dev

How can I implement a fixed size hashmap?

From Dev

How can I implement the observer pattern in Rust?

Related Related

  1. 1

    How can I implement a Singleton class that can be derived from in WPF?

  2. 2

    How can I implement a Singleton class that can be derived from in WPF?

  3. 3

    How can I implement jQuery validation like MVC data annotation?

  4. 4

    Can I implement Singleton Design Pattern like this in Scheme?

  5. 5

    Can I implement Singleton Design Pattern like this in Scheme?

  6. 6

    How can I implement IServiceProvider?

  7. 7

    How i can use context in Singleton?

  8. 8

    How can I test a store singleton?

  9. 9

    How can I debug a custom annotation in Lombok?

  10. 10

    How can I add an Annotation in dygraph?

  11. 11

    How can I get a generic type annotation

  12. 12

    How can I add annotation in ggplotly animation?

  13. 13

    How can I know if an annotation is already on the mapview

  14. 14

    How to implement a singleton provider for qmlRegisterSingletonType?

  15. 15

    How to Implement Singleton class in Swift

  16. 16

    How to implement singleton in shell script

  17. 17

    How to implement singleton pattern in subclass

  18. 18

    How can i implement a scanning bar animation?

  19. 19

    How can I implement this logic better

  20. 20

    how can i implement syntax coloring on a jtextpane

  21. 21

    How can I implement a RESTful Progress Indicator?

  22. 22

    How can I implement a '-Help' switch in PowerShell?

  23. 23

    Load balancing server, how can I implement it?

  24. 24

    How can I implement OnClickListener of an ImageView in a fragment?

  25. 25

    How can I implement comparable interface in go?

  26. 26

    How can I implement a dynamic selection?

  27. 27

    How can I implement asyncio websockets in a class?

  28. 28

    How can I implement a fixed size hashmap?

  29. 29

    How can I implement the observer pattern in Rust?

HotTag

Archive