Strategy Pattern?
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?
Message was edited by:
AQ_Java

