Easy.
Using a DP/FLEX, 10 player roster.
Any 9 may play defense at any time.
The first 9 in the line-up must include the DP; the FLEX is always the 10th. The first 9 must bat to remain in the game.
IF the FLEX is used on offense, that player must bat for the DP. The DP has left the game, the line-up is reduced to 9. The DP may re-enter the game in accordance with the rules governing that action.
IF the DP plays defense for the FLEX, the FLEX has left the game, the line-up is reduced to 9 and the FLEX may re-enter the game in accordance with the rules governing that action.
It should be noted that "DP" and "FLEX" are positions in the line-up, not a particular player or defensive position.