---
title: "Replace Type Code with State/Strategy"
type: "refactoring-technique"
slug: "replace-type-code-with-state-strategy"
url: "http://localhost:3000/en/replace-type-code-with-state-strategy.md"
category: "Organizing Data"
description: "Problem: You have a coded type that affects behavior but you can’t use subclasses to get rid of it. Solution: Replace type code with a state object. If it’s necessary to replace a field value with type code, another state object is “plugged in”."
---
# Replace Type Code with State/Strategy

> Problem: You have a coded type that affects behavior but you can’t use subclasses to get rid of it. Solution: Replace type code with a state object. If it’s necessary to replace a field value with type code, another state object is “plugged in”.

## Problem

You have a coded type that affects behavior but you can’t use subclasses to get rid of it.

## Solution

Replace type code with a state object. If it’s necessary to replace a field value with type code, another state object is “plugged in”.

## Why Refactor

You have type code and it affects the behavior of a class, therefore we can’t use [Replace Type Code with Class](/replace-type-code-with-class).

Type code affects the behavior of a class but we can’t create subclasses for the coded type due to the existing class hierarchy or other reasons. Thus means that we can’t apply [Replace Type Code with Subclasses](/replace-type-code-with-subclasses).

## Benefits

* This refactoring technique is a way out of situations when a field with a coded type changes its value during the object’s lifetime. In this case, replacement of the value is made via replacement of the state object to which the original class refers.
* If you need to add a new value of a coded type, all you need to do is to add a new state subclass without altering the existing code (cf. the _Open/Closed Principle_).

## How to Refactor

1. Use [Self Encapsulate Field](/self-encapsulate-field) to create a getter for the field that contains type code.
2. Create a new class and give it an understandable name that fits the purpose of the type code. This class will be playing the role of _state_ (or _strategy_). In it, create an abstract coded field getter.
3. Create subclasses of the state class for each value of the coded type. In each subclass, redefine the getter of the coded field so that it returns the corresponding value of the coded type.
4. In the abstract state class, create a static factory method that accepts the value of the coded type as a parameter. Depending on this parameter, the factory method will create objects of various states. For this, in its code create a large conditional; it’ll be the only one when refactoring is complete.
5. In the original class, change the type of the coded field to the state class. In the field’s setter, call the factory state method for getting new state objects.
6. Now you can start to move the fields and methods from the superclass to the corresponding state subclasses (using [Push Down Field](/push-down-field) and [Push Down Method](/push-down-method)).
7. When everything moveable has been moved, use [Replace Conditional with Polymorphism](/replace-conditional-with-polymorphism) in order to get rid of conditionals that use type code once and for all.
## Relations

**Similar techniques**

- [Replace Type Code with Class](/en/replace-type-code-with-class.md)
- [Replace Type Code with Subclasses](/en/replace-type-code-with-subclasses.md)

**Eliminates smells**

- [Primitive Obsession](/en/smells/primitive-obsession.md)

