How can I implement this?

No I shouldn't use a connector. Also it is important that each cube knows with which side of the other cube is connected. One thought solutions is RFID tags but it is costly