Strategy Pattern, Am i on the right Track?

The question:

Design a small, prototype, application, which creates, stores and amends warehouse records. This may have little more than a StockItem class.

Using each of the patterns Strategy, Decorator, Adaptor and in turn provide three different adaptations to your prototype application which will enable a user to view the warehouse records in at least three different ways, e.g. by location, alphabetically, by value, by quantity in stock, as continuous plain text, as formatted text, in a list pane, as a printout etc

My Code

package warehouse;

publicclass StockItem

{

privateint id;

private String describtion;

privatedouble price;

privateint quantity;

private String location;

ViewByLocationBehavior locationVar;

public StockItem()

{

}

publicvoid setId(int id)

{

this.id=id;

}

publicint getId()

{

return id;

}

publicvoid setDescribtion(String describtion)

{

this.describtion=describtion;

}

public String getDescribtion()

{

return describtion;

}

publicvoid setPrice(double price)

{

this.price=price;

}

publicdouble getPrice()

{

return price;

}

publicvoid setQuantity(int quantity)

{

this.quantity=quantity;

}

publicint getQuantity()

{

return quantity;

}

publicvoid setLocation(String location)

{

this.location=location;

}

public String getLocation()

{

return location;

}

publicvoid setViewByLocationBehavior(ViewByLocationBehavior locationVar )

{

this.locationVar=locationVar;

}

publicvoid displayByLocation()

{

locationVar.displayByLocation();

}

public String toString()

{

return String.format("%d %S %f %d %S",id ,describtion,price,quantity,location);

}

}

The interface

package warehouse;

publicinterface ViewByLocationBehavior

{

void displayByLocation();

}

Concrete classes arepackage warehouse;

publicclass PlainTextextends StockItemimplements ViewByLocationBehavior

{

publicvoid displayByLocation()

{

System.out.println("Display PlainText");

}

}

package warehouse;

publicclass FormattedTextextends StockItemimplements ViewByLocationBehavior

{

publicvoid displayByLocation()

{

System.out.println("Display Formatted Text ");

}

}

package warehouse;

publicclass ListPaneextends StockItemimplements ViewByLocationBehavior

{

publicvoid displayByLocation()

{

System.out.println("Display ListPane");

}

}

package warehouse;

publicclass PrintOutextends StockItemimplements ViewByLocationBehavior

{

publicvoid displayByLocation()

{

System.out.println("Display PrintOut ");

}

}

package warehouse;

publicclass StockTest

{

publicstaticvoid main(String args[])

{

StockItem obj =new StockItem();

obj.setViewByLocationBehavior(new PlainText());

obj.displayByLocation();

System.out.println(obj.toString());

}

}

But my questions is Am i on the right track applying the Strategy pattern?

Because next i will make the other Interfaces: View by alphabetically, by value, by quantity, and their Concrete Classes are

Display as continuous plain text, formatted text, in a list pane, and printout.

Each interface have four concrete classes

Need your comments Please?

[8011 byte] By [AQ_Javaa] at [2007-11-27 11:20:12]
# 1

> But my questions is Am i on the right track applying

> the Strategy pattern?

Your assignment explicitly calls for the strategy pattern:

"> Using each of the patterns Strategy, Decorator,

> Adaptor and in turn provide three different

> adaptations to your prototype application which will

> enable a user to view the warehouse records in at

> least three different ways, e.g. by location,

> alphabetically, by value, by quantity in stock, as

> continuous plain text, as formatted text, in a list

> pane, as a printout etc"

I'm not sure I care for that interface much. I would have expected something more like this (I'm not 100% sure about the parameter that I'd pass, if any):

public interface StockItemFilter

{

List<StockItem> filter(List<StockItem> unfilteredStockItems);

}

Now you'll pass in an unfiltered List of StockItems and return the List of appropriate StockItems filtered alphabetically, by location, etc. If you want to combine the fetching of StockItems with filtering, replace the unfiltered List with some Criterion.

Your ViewByLocationBehavior doesn't seem to accept any input or return anything. What good is it?

%

duffymoa at 2007-7-29 14:41:12 > top of Java-index,Java Essentials,Java Programming...
# 2

> Design a small, prototype, application, which

> creates, stores and amends warehouse records.

Straight forward enough.

> This may have little more than a StockItem class.

Don't let this miss lead you. 1 Responsibility = 1 Class.

> Using each of the patterns

> Strategy,

Use Strategy pattern to control the Ordering

e.g.

OrderByLocation extends OrderStrategy

> Decorator,

Use Decorator to decorate a Stock item with location information

> Adaptor and in turn provide three different

Use Adaptors to control the output, e.g. Plain, decorated etc.

> But my questions is Am i on the right track applying the Strategy pattern?

No, you client code should tell the warehouse code which Strategy to use for ordering.

MartinS.a at 2007-7-29 14:41:12 > top of Java-index,Java Essentials,Java Programming...