java - Best design to share class instance between many classes -


i going create utility class apiutility, wraps unique object token generated server engine, once used username , password object token, door opening can access engine anytimes if token still alive.

i want use existing 'apiutility' once access avoid unnecessary authentication effort. , 'apiutility' directly call many functions server engine. right now, have else classes, under different place take different responsibility: e.g. build data, logic validation, condition elevation, these classes both need have base line use apiutility access engine data, have design this? because fell every class have variable apiutility need set create instance of these classes not design.

you're on right track in opinion; simple best.

just have classes need apiutility take instance dependency in constructor.

that way, if need/want to, can instantiate apiutility once , have shared.

fyi, people call "poor man's dependency injection".


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -