mercredi 26 avril 2017

C# Bridge design pattern

I found this question in glassdoor and careercup, this is an OOP design question.

many people claim to have solved it using different design patterns.
I think It can be solved either with bridge pattern or decorator pattern.
I tried to implement it using bridge pattern but found myself stuck.
in the original question the base class is derived by 4 classes: 1. woodChair 2. woodTable 3.metalChair 4.metalTable

I think I understand how to create the bridge here but I'm not sure if it doesn't break the conditions of the question.

Can you please say:
A. which design pattern will you use? or how (don't have to use a design pattern)
B. Is my implementation correct? does it break the conditions set by the interviewer?

Original Question:
You are on a team that is creating a program to model stress on furniture. Your task is to model the behavior of furniture under abuse such as excessive weight or application of fire.
Someone has already created a prototype with a base class Furniture, and four derived classes WoodChair,SteelChair, WoodTable, and SteelTable.
We will need to start adding other furniture like couches, beds, bookcases, and desks, and also new materials such as plastic, cloth, rubber, etc. Try to improve the class design - you are free to modify it however you wish since it is only a prototype.
applyWeight(double x, double y, double weight, double seconds);
the furniture becomes unusable if enough weight is applied; the algorithm depends on shape of furniture, and the location where the weight is applied applyFire(double x, double y, double fireStrength, double seconds);

the furniture becomes unusable if it is made of wood, and fire is applied long enough; wood will change from brown to black if it is burnt (whether or not the furniture becomes unusable)
Color getColor(); // possible values: Gray, Brown, Black, etc
FurnitureState getState(); // possible values: OK, UNUSABLE

[TestClass]

public class UnitTest1
{
    [TestMethod]
    public void SuperFurnitureTest()
    {
        SuperFurniture furniture = new SuperFurniture();
        //test for wood Chair
        furniture.CurrentFurniture = new WoodFurniture(20, 10);
        furniture.CurrentFurniture.applyFire(0, 1, 2, 21);
        Assert.AreEqual(Color.Black, furniture.CurrentFurniture.getColor());
        furniture.CurrentFurniture.applyWeight(0, 1, 2, 21);
        Assert.AreEqual(FurnitureState.OK, furniture.CurrentFurniture.getState());
    }
}


public class Furniture
{
    protected FurnitureState _state;
    protected Color _color;
    public double _size;
    public double _weightThreshold;
    public Furniture(double size)
    {
        _state = FurnitureState.OK;
    }
    public virtual void applyFire(double x, double y, double fireStrength, double seconds)
    {

    }
    public virtual void applyWeight(double x, double y, double weight, double seconds)
    {
        if (x * y > _size)
        {
            if (weight > _weightThreshold)
            {
                _state = FurnitureState.UNUSABLE;
            }
        }
    }
    public Color getColor()
    {
        return _color;
    }
    public FurnitureState getState()
    {
        return _state;
    }
}


public class WoodFurniture :Furniture
{
    public double _fireThresh;
    public WoodFurniture(double fireThresh, double size) :base(size)
    {
        _fireThresh = fireThresh;
    }
    public override void applyFire(double x, double y, double fireStrength, double seconds)
    {
        if (seconds > _fireThresh)
        {
            _color = Color.Black;
        }
    }      
}

public class SuperFurniture
{
    Furniture currentFurniture = null;
    public Furniture CurrentFurniture
    {
        get { return currentFurniture;}
        set { currentFurniture = value; }
    }

    public void applyWeight(double x, double y, double weight, double seconds)
    {
        currentFurniture.applyWeight(x, y, weight, seconds);
    }
    void applyFire(double x, double y, double fireStrength, double seconds)
    {
        currentFurniture.applyFire(x, y, fireStrength, seconds);
    }
}

Aucun commentaire:

Enregistrer un commentaire