rewrite with pattern factory