refactoring of TogglePattern and ExpandCollapsePattern