0

This is an OOD based interview question:

There is a Furniture class and has derived classes like WoodChair, WoodTable, SteelChair, SteelTable. Interviewer wanted to add more number of classes like ironchair,irontable etc; How should we do that. The design is not yet published and we are free to modify the entire sutff given.

I thought that since we're basically building types of furniture we should use a builder pattern here with Furniture class with properties like type (chair/table) and make(iron/wood) etc. Then we'd have an interface Builder with functions like: buildLegs(..), buildSurface(..) and sub-classes like ChairBuilder, TableBuilder and a Director class to instantiate all of them. We could add as many new types of Furniture of any make and construct a builder class for them without affecting existing design.

After reading Builder Vs Decorator pattern I was sure that I'm not supposed to use Decorator pattern here. But is Builder also ok? Is it an overkill?

Also, I'm not sure how to deal with the make of the furniture. Would adding a feature of type enum for the make be enough? [steel, iron, wood] The make doesn't really add any new behavior to the furniture items.

Community
  • 1
  • 1
user1071840
  • 3,522
  • 9
  • 48
  • 74

2 Answers2

1

It looks like something needs to be refactored in the existing classes, which may also help avoiding creating a new class for every one of the need that arise in the future. This depends entirely on the context though: an inventory application needs a radically different model of a chair than a software that needs to display a chair in 3d. How do you know? Ask the interviewer, then you will know where they want you to go.

Boring case: a Chair has some common behavior/data that can be refactored out in a different class, same thing for Table. Now how do you choose to represent the material? Again, it depends on the domain, ask the interviewer. It is also a matter of the language you are using: does your language of choice support multiple inheritance? Do you need (or want) to use multiple inheritance at all? It may make sense to favor composition over inheritance. Why would you go one way or the other? Do you even need a class to represent this piece of information?

How should we do that.

Ask the interviewer, they will guide you to the solution. There is no single correct answer to a problem so broadly formulated, they want you to ask questions and see how you think. That said, as broad as the question is, the way it is formulated may be a hint that you should refactor something in order to avoid having to create a class for every new combination of furniture and material.

Possible solutions:

  • No need for Table/Chair/Bed to inherit from Furniture: a class Furniture with a property for the piece of furniture and a property for the material.
  • Classes for Table, Chair, Bed, whatever with a property for the material. The complexity of how the material is represented depends on how this information have to be modeled: it could be a string, or a Class (Wood, Iron, Steel) implementing an IMaterial interface.
Filippo Pensalfini
  • 1,714
  • 12
  • 16
0

Probably, i was use Abstract Factory: WoodFurntiture, SteelFurniture, IronFurniture. Each Factory know How to make chair, table. Inside you can use (if you need) other DP, but for a now, i do not see any needs for it

Code:

    namespace Furniture
    {
        class Program
        {
            static void Main(string[] args)
            {
                IFurnitureFactory factory = new WoodFurnitureFactory();
                IFurniture chair = factory.GetChair();
            }
        }

        public interface IFurniture { }
        public class WoodChair : IFurniture { }
        public class WoodTable : IFurniture { }
        public class SteelChair : IFurniture { }
        public class SteelTable : IFurniture { }
        public class IronChair : IFurniture { }
        public class IronTable : IFurniture { }

        public interface IFurnitureFactory
        {
            IFurniture GetChair();
            IFurniture GetTable();
        }

        public class WoodFurnitureFactory : IFurnitureFactory
        {
            public IFurniture GetChair()
            {
                return new WoodChair();
            }

            public IFurniture GetTable()
            {
                return new WoodTable();
            }
        }

        public class IronFurnitureFactory : IFurnitureFactory
        {
            public IFurniture GetChair()
            {
                return new IronChair();
            }

            public IFurniture GetTable()
            {
                return new IronTable();
            }
        }

        public class SteeFurniturelFactory : IFurnitureFactory
        {
            public IFurniture GetChair()
            {
                return new SteelChair();
            }

            public IFurniture GetTable()
            {
                return new SteelTable();
            }
        }
    }
zzfima
  • 1,528
  • 1
  • 14
  • 21
  • What all changes would you have to make to the code to add a new furniture type? What if you want to make a new furniture item, say bed..you'd have to go and make changes to the IFurnitureFactory and then update each type (steel/iron) for bed object instantiation. That's the problem we're trying to solve. Isn't it? – user1071840 Feb 27 '13 at 07:48