Contents

Large Class

Large Classes

Usually we start coding small classes. While the codebase evolve, classes get bigger. And bigger. And bigger… They get bloated.

We can do the following to refactor large classes.

Extract Class

We can separate a large class into multiple smaller classes.

1
2
3
4
5
6
7
8
9
    class Person {
        private name
        private areaCode
        private officeNumber

        function getTelephoneNumber() {
            // Get the phone number
        }
    }

We can extract data from Person into another class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    class Person {
        private name
        private telephoneNumber

        function getTelephoneNumber() {
            telephoneNumber.getTelephoneNumber()
        }
    }

    class TelephoneNumber {
        private areaCode
        private officeNumber

        function getTelephoneNumber() {
            // Get the phone number
        }
    }

Extract Subclasses