While this approach indeed eliminates the need for subclasses, it creates another problem. You can create a giant constructor right in the base House class with all possible parameters that control the house object. There’s another approach that doesn’t involve breeding subclasses. Any new parameter, such as the porch style, will require growing this hierarchy even more. But eventually you’ll end up with a considerable number of subclasses. The simplest solution is to extend the base House class and create a set of subclasses to cover all combinations of the parameters. But what if you want a bigger, brighter house, with a backyard and other goodies (like a heating system, plumbing, and electrical wiring)? To build a simple house, you need to construct four walls and a floor, install a door, fit a pair of windows, and build a roof. You might make the program too complex by creating a subclass for every possible configuration of an object.įor example, let’s think about how to create a House object. Or even worse: scattered all over the client code. Such initialization code is usually buried inside a monstrous constructor with lots of parameters. User Input not working with keyboard.Imagine a complex object that requires laborious, step-by-step initialization of many fields and nested objects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |