Is it a bad idea to create a class which will only have one instance?

Is it bad coding practice/design to make a class which will only be instantiated once?

I have some variables and functions that can be grouped together under a class to “look good” (for a lack of a better description) since they are somewhat related, but they can just be global variables and global functions.

(Btw, I am using JavaScript, AngularJS, Express, MongoDB.)

5

A single instance for a class makes sense if the object represents a single resource, like a ethernet connexion or the operating system task manager for instance.

Functions are put in a class only if they act on the variables of instances of that class, otherwise the maintainer will be confused about the intention of that class.

Usually, a good reason exists why your app has global variables. Try to find the common purpose of them and design a class around this purpose. It will not only make the design clear, but your mind as well.

1

There is no problem with writing a class that ends being instanced only once.

If putting some variables and functions together within a class makes sense, has semantic value or makes the code simpler to read and manipulate, then so be it, maintainers will be thankful.

What can be very wrong though, is to enforce uniqueness from within.

Many people tend to fire the singleton pattern as soon as they think they’ll only need one instance of something. See for example how you haven’t described your context in depth, yet most answers already suggest you to use a singleton. And then the design might be screwed because if you need another instance at some point for whatever reason, the class will prevent you from doing it without changing a lot of code.

So as a conclusion, one instance is fine, but make sure to know whether:

  1. you only need one instance, or
  2. you cannot have more than one instance (a very, very rare case in my experience).

13

Is it bad coding practice/design to make a class which will only be instantiated once?

No, even if only instantiated once, you need a class for it. But don’t suppose you will always only need one instance, now and in the future.

Suppose you make a game, and you have a player class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Player {
...
}
</code>
<code>class Player { ... } </code>
class Player {
    ...
}

You will instantiate one player class in your game, and use always the same object. Nothing wrong with it.

However, don’t make a Singleton out of it! Today you design your game and think “Well, it will always be a one-player game. I will use a Singleton for my Player”. But in version 2, you may want to implement multiplayer – and then you are getting into trouble. You will have to rewrite and adapt a lot of code, because your design does not support instantiating multiple players.

The same is with caches. Or loggers. Or network interfaces. Today, you think you will only need one, always, for sure. But in a future version you need to add a second. Be aware of this situation.

You are asking two questions in one.

  1. No, there’s nothing bad having a class that will only have one instance.
  2. Grouping variables and methods in a class without a real reason for it is bad design.

The singleton pattern is common. It allows you to enforce single instance of class.

But for some it is too common, so it becomes an anti-pattern. The reason for this is because it is practically same as global state. And any global state is hard to mock and test and hard to debug. But at the same time, having to explicitly pass this one instance everywhere in your code can cause code to bloat up a lot. But this will make you think about proper architecture. If bunch of classes use this one singleton, you might thing they are related somehow and create common ancestor for them, that will encapsulate this one instance for them.

There are singleton classes and that is a common design pattern.

There are also classes that never get instantiated. If you have functions that DO NOT operate on any persistent state (e.g., they take the input and transform it), you can usually use a static method. In some languages (like Java) these static methods would be declared in a class, but the class itself will never be instantiated.

It’s bad to assume something is universally bad. In your case, maybe it would be a good idea, but given that you’re using a language that’s not OO at the core it’s more appropriate probably to just use loose methods and variables and put them in a common library that you include in your other work.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật