Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
JS Design Patterns - ¿Fábrica de métodos?

tengo un componente

 export default function PressableText({ children = "", detectionOptions = Object.keys(PATTERNS).map((type) => ({ type })), style = commonStyles.text }) { ... } PressableText.propTypes = { children: PropTypes.string, detectionOptions: PropTypes.arrayOf( PropTypes.shape({ type: PropTypes.oneOf(Object.keys(PATTERNS)).isRequired, onPress: PropTypes.func, }) ), ... };

Como puede ver, 'detectionOptions' recibe una matriz de (type, onPress) y el método onPress es opcional.

Mi objeto PATRONES actual se ve así:

 export const PATTERNS = { username: /@[a-zA-Z0-9_.-]{3,30}/, uri: regexForUri, phone: regexForPhone, };

Entonces... básicamente, lo que quiero hacer es proporcionar una acción onPress predeterminada para cada tecla.

He pensado en implementar una fábrica, pero no soy muy bueno en Design Patterns. ¿Esto se considera una fábrica?

 // // CODE INSIDE MY COMPONENT, AS IT ACCESS COMPONENT RELATED STUFF // const handleOnPressUri = () => { ... }; const handleOnPressPhone = () => { ... }; const handleOnPressUsername = () => { ... }; const defaultPressableActions = useMemo( // IS THIS A FACTORY? () => ({ uri: handleOnPressUri, phone: handleOnPressPhone, username: handleOnPressUsername, }), [] ); const getPatterns = () => detectionOptions.map(({ type, onPress }) => { const pattern = PATTERNS[type]; if (pattern) { return { pattern, onPress: onPress ?? defaultPressableActions[type], }; } throw new Error(`Invalid pattern type.`); } );

Pregunto esto, porque hasta donde yo sé, una fábrica es un generalizado que devuelve cosas personalizadas, de forma centralizada. Pero… ¿qué tal un mapa que, por cada tecla, devuelva una acción?

¿Se le ocurre alguna otra solución mejor para refactorizar este código?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!