Make the constructor private. Create a static factory method that takes an instance of a class that is allowed access. Ask the factory method to create a suitable object and use the installer on the object that is allowed access to the created object to give this class a copy.
public class AllowedAccess { private SecureClass secure; public setSecureClass( SecureClass secure ) { this.secure = secure; } ... } public class SecureClass { private SecureClass() {} public static void Create( AllowedAccess allowed ) { allowed.setSecureClass( new SecureClass() ); } ... }
By the way, I am suspicious of this design. I seem to be too attached to me.
source share