Amazon Interview Question
SDE-2sCountry: India
Interview Type: In-Person
The only use-case is decorate furniture with metal or wood or something else and applied different tests based on type.
but not like decorate furniture with wood and then with metal etc..
So do we really need decorator?
enum FurnitureType {
WOOD, METAL;
}
abstract class Furniture {
FurnitureType furnitureType;
FurnitureTest furnitureTest;
public Furniture(FurnitureType furnitureType, FurnitureTest furnitureTest) {
this.furnitureType = furnitureType;
this.furnitureTest = furnitureTest;
}
public boolean testFurniture() {
return furnitureTest.testFurniture();
}
}
interface FurnitureTest {
public boolean testFurniture();
}
class WoodFurnitureTest implements FurnitureTest {
public boolean testFurniture() {
/*
* Test code goes here
*/
return true;
}
}
class MetalFurnitureTest implements FurnitureTest {
public boolean testFurniture() {
/*
* Test code goes here
*/
return true;
}
}
class Chair extends Furniture {
public Chair(FurnitureType furnitureType, FurnitureTest furnitureTest) {
super(furnitureType, furnitureTest);
}
}
class Table extends Furniture {
public Table(FurnitureType furnitureType, FurnitureTest furnitureTest) {
super(furnitureType, furnitureTest);
}
}
class TestClient {
public static void main(String[] args) {
WoodFurnitureTest woodFurnitureTest = new WoodFurnitureTest();
MetalFurnitureTest metalFurnitureTest = new MetalFurnitureTest();
// create wood chair
Chair woodchaChair = new Chair(FurnitureType.WOOD, woodFurnitureTest);
// create metal chair
Chair metalChair = new Chair(FurnitureType.METAL, metalFurnitureTest);
}
}
The solution should be open for extension. How would you test a furniture made of both wood & steel? If you add a new furniture, without code change how it will support test?
public interface Material{
public void setTestFixture(TestFicture fixture);
}
public interface TestFixture {
public void test();
}
public interface Furniture {
public void setMaterial(Material m);
}
-----------------------------------
public class ChokingTestFixture implements TestFixture {
public void test() {...}
}
public class FireTestFixture implements TestFixture {
public void test() {...}
}
-----------------------------------
/* Similarly Wood can be formed. If we make a abstract class as Material, then even this setTestFixture() code need not to be duplicated */
public class Metal implements Material {
TestFixture fixture;
public void setTestFixture(TestFixture fixture) {
this.fixture = fixture;
}
}
------------------------------------
/* Similarly Chair,Table,Sofa can be constructed as follows*/
public class Chair implements Furniture {
public void setMaterial(Material m) {
}
}
interface IMaterialValidatable
{
void validateQuality();
}
public abstract class Material implements IMaterialValidatable
{
}
class Wood extends Material
{
public void validateQuality()
{
// validate quality of wood material
}
}
class Metal extends Material
{
public void validateQuality()
{
//Validate metal quality;
}
}
abstract class Furniture
{
Material[] materials;
Furniture(Material[] materials)
{
this.materials = materials;
}
void evaluateQuality()
{
for(Material material : materials)
{
material.validateQuality();
}
}
}
class Chair extends Furniture
{
}
how about something along the lines of..
pardon my syntax... mix of C++ and C#
interface Furniture
{
// returns bool as test fail or pass
virtual bool TestTheFurniture( TestData* tdata) = 0;
}
interface TestData
{
// ???
}
class WoodenTestData : TestData {}
class MetalTestData : TestData {}
class WoodenFurniture : Furniture
{
bool TestTheFurniture(WoodTestData* wd) {}
}
class MetalFurniture : Furniture
{
bool TestTheFurniture(MetalTestData* wd) {}
}
{{
interface Furniture {
void test();
}
abstract class AbstractFurniture {
Material material;
AbstractFurniture(Material m) {
this.material = m;
}
void test() {
material.testStrength();
}
}
class Chair extends AbstractFurniture{}
class Table extends AbstractFurniture{}
interface Material {
void testStrength();
}
class wood implements Material {
void testStrength() {
testfire();
}
}
}}
We can decorate(Decorator_pattern) each furniture with the type(wood, metal etc.)
- Brandy May 28, 2014