In the first part, we explored the significance of the Strategy pattern in React projects and outlined its implementation. However, the approach presented might have be a little bit of an overkill. This article aims to introduce a more simple and practical way of applying the Strategy pattern.
1️⃣ Example: Unit Conversion
Problem Statement
Consider a scenario where you need to convert weight units (e.g. converting 1000g to 1kg). A straightforward solution involves a recursive function, as shown below:
export enum Weight {
ย Gram = 'g',
ย Kilogram = 'kg',
ย Tonne = 't',
ย Megatonne = 'Mt',
ย Gigatonne = 'Gt',
}
const WEIGHT_UNIT_ORDER = Object.values(Weight);
const WEIGHT_UNIT_CONVERSION_THRESHOLD = 1_000;
export const convertWeightUnit = (weightInGram: number, unit = Weight.Gram): ConvertUnitResult => {
ย if (weightInGram < WEIGHT_UNIT_CONVERSION_THRESHOLD || unit === WEIGHT_UNIT_ORDER.at(-1)) {
ย ย return { newValue: weightInGram, newUnit: unit };
ย }
ย const nextUnit = WEIGHT_UNIT_ORDER[Math.min(WEIGHT_UNIT_ORDER.indexOf(unit) + 1, WEIGHT_UNIT_ORDER.length - 1)];
ย return convertWeightUnit(weightInGram / WEIGHT_UNIT_CONVERSION_THRESHOLD, nextUnit);
};
This example sets the stage for our discussion but let’s not dwell on the recursive algorithm itself. Instead, let’s explore how to apply this logic to another unit set, such as file size, without duplicating code. Here, the Strategy pattern offers an elegant solution.
Applying the Strategy Pattern
First, we’ll have to define the additional TypeScript enum and interfaces that are needed for adding the new set of units:
export enum Weight {
ย Gram = 'g',
ย Kilogram = 'kg',
ย Tonne = 't',
ย Megatonne = 'Mt',
ย Gigatonne = 'Gt',
}
export enum FileSize {
ย Byte = 'B',
ย Kilobyte = 'KB',
ย Megabyte = 'MB',
ย Gigabyte = 'GB',
ย Terabyte = 'TB',
ย Petabyte = 'PB',
}
export type GeneralUnit = Weight | FileSize;
export interface ConvertUnitResult {
ย newValue: number;
ย newUnit: GeneralUnit;
}
export interface UnitConversionStrategy {
ย [key: string]: {
ย ย unitOrder: GeneralUnit[];
ย ย threshold: number;
ย };
}
Now we need to modify the code to apply the Strategy pattern. At the heart of every implementation of the Strategy pattern, there has to be an object that defines the strategies. In this case, it’s UNIT_CONVERSION_STRATEGY
:
import { ConvertUnitResult, FileSize, GeneralUnit, UnitConversionStrategy, Weight } from './unit.utils.types';
const WEIGHT_UNITS = Object.values(Weight) as GeneralUnit[];
const FILE_SIZE_UNITS = Object.values(FileSize) as GeneralUnit[];
const UNIT_CONVERSION_STRATEGY: UnitConversionStrategy = {
ย [Weight.Gram]: {
unitOrder: WEIGHT_UNITS,
threshold: 1_000
},
ย [FileSize.Byte]: {
unitOrder: FILE_SIZE_UNITS,
threshold: 1_000
},
};
// This will ensure every unit in a set
// of units is present in the strategy object,
// which is necessary for the code below to work.
Object.values(UNIT_CONVERSION_STRATEGY).forEach((unitStrategy) => {
ย unitStrategy.unitOrder.forEach((unit) => {
ย ย UNIT_CONVERSION_STRATEGY[unit] = unitStrategy;
ย });
});
export const convertUnit = (value: number, unit: GeneralUnit): ConvertUnitResult => {
ย const unitConversionStrategy = UNIT_CONVERSION_STRATEGY[unit];
ย if (!unitConversionStrategy) throw new Error('Unit not supported');
ย const { unitOrder, threshold } = unitConversionStrategy;
ย if (value < threshold || unit === unitOrder.at(-1)) {
ย ย return { newValue: value, newUnit: unit };
ย }
ย const nextUnit = unitOrder[Math.min(unitOrder.indexOf(unit) + 1, unitOrder.length - 1)];
ย return convertUnit(value / threshold, nextUnit);
};
You can try it in the live codesandbox:
By leveraging the Strategy pattern as demonstrated, we circumvent the Shotgun Surgery anti-pattern. This approach keeps the number of conditional statements constant and simplifies the addition of new unit sets by merely extending the UNIT_CONVERSION_STRATEGY
object without altering any existing logic, adhering to the Open/Closed Principle in SOLID.
2️⃣ Example: Design Systems in CSS-in-JS Projects
Another great example where the Strategy pattern can be applied in frontend projects is when you have to implement components in a design system.
Problem Statement
Consider implementing a button with various variants, colors, and sizes in a design system:
export type ButtonVariant = 'contained' | 'outlined' | 'text';
export type ButtonColor = 'primary' | 'secondary' | 'error' | 'success' | 'warning' | 'info';
export type ButtonSize = 'xs' | 'sm' | 'md';
export interface ButtonProps {
ย color?: ButtonColor;
ย variant?: ButtonVariant;
ย size?: ButtonSize;
ย children: React.ReactNode;
}
A traditional implementation might result in a cluttered component:
- Button.tsx (Before Strategy Pattern):
import { memo } from 'react';
import { Button as ThemeUIButton } from 'theme-ui';
import { ButtonProps } from './Button.types';
const ButtonBase = ({ color = 'primary', variant = 'contained', size = 'sm', children }: ButtonProps) => {
ย return (
ย ย <ThemeUIButton
ย ย ย sx={{
outline: 'none',
borderRadius: 4,
transition: '0.1s all',
cursor: 'pointer',
...(variant === 'contained'
? {
backgroundColor: `${color}.main`,
color: 'white',
'&:hover': {
backgroundColor: `${color}.dark`,
},
}
: {}),
...(variant === 'outlined'
? {
backgroundColor: 'transparent',
color: `${color}.main`,
border: '1px solid',
borderColor: `${color}.main`,
'&:hover': {
backgroundColor: `${color}.light`,
},
}
: {}),
...(variant === 'text'
? {
backgroundColor: 'transparent',
color: `${color}.main`,
'&:hover': {
backgroundColor: `${color}.light`,
},
}
: {}),
...(size === 'xs'
? {
fontSize: '0.75rem',
padding: '8px 12px',
}
: {}),
...(size === 'sm'
? {
fontSize: '0.875rem',
padding: '12px 16px',
}
: {}),
...(size === 'md'
? {
fontSize: '1rem',
padding: '16px 24px',
}
: {}),
}}
ย ย >
ย ย ย {children}
ย ย </ThemeUIButton>
ย );
};
export const Button = memo(ButtonBase);
Applying the Strategy Pattern
Now let’s try applying the Strategy pattern and see how it helps us to clear the mess:
import { ButtonColor } from './Button.types';
export const getButtonVariantMapping = (color: ButtonColor = 'primary') => {
return {
contained: {
backgroundColor: `${color}.main`,
color: 'white',
'&:hover': {
backgroundColor: `${color}.dark`,
},
},
outlined: {
backgroundColor: 'transparent',
color: `${color}.main`,
border: '1px solid',
borderColor: `${color}.main`,
'&:hover': {
backgroundColor: `${color}.light`,
},
},
text: {
backgroundColor: 'transparent',
color: `${color}.main`,
'&:hover': {
backgroundColor: `${color}.light`,
},
},
};
};
export const BUTTON_SIZE_STYLE_MAPPING = {
xs: {
fontSize: '0.75rem',
padding: '8px 12px',
},
sm: {
fontSize: '0.875rem',
padding: '12px 16px',
},
md: {
fontSize: '1rem',
padding: '16px 24px',
},
};
- Button.tsx (After Strategy Pattern):
import { memo } from 'react';
import { Button as ThemeUIButton } from 'theme-ui';
import { ButtonProps } from './Button.types';
import { getButtonVariantMapping, BUTTON_SIZE_STYLE_MAPPING } from './Button.utils';
const ButtonBase = ({ color = 'primary', variant = 'contained', size = 'sm', children }: ButtonProps) => {
ย const buttonVariantStyle = getButtonVariantMapping(color)[variant];
ย return (
ย ย <ThemeUIButton
ย ย ย sx={{
outline: 'none',
borderRadius: 4,
transition: '0.1s all',
cursor: 'pointer',
...buttonVariantStyle,
...BUTTON_SIZE_STYLE_MAPPING[size],
}}
ย ย >
ย ย ย {children}
ย ย </ThemeUIButton>
ย );
};
export const Button = memo(ButtonBase);
This refactoring significantly improves code readability and gives us a clear separation of concerns.
Conclusion
This article showcased a simplified application of the Strategy pattern in frontend projects. By adopting this pattern, we can avoid code duplication, improve code readability, and easily extend functionality without modifying existing logic.
Please look forward to the next part of the series where I’ll be sharing my personal experience with applying useful design patterns in frontend projects.
[fluentform id="8"]