How can I implement this?

Can you add connectors to the cubes? And when you place a cube, you actually push it in a connector of the opposite gender.