i'm writing java code university exam , project based on "e-commerce". 1 of requirements insert products in shopping cart , i've use visitor
pattern.
this example professor gave me, , i've "use" represent shopping cart. problem i've product
class contain istance of category
class , products list or categories list stored in database (i access them via query @ each step).
now, first of ... book
category or product? compared smartphone or htc 1 m8, example? also, i've class contains code "used" client. there, don't have shoppingcart object simple arraylist of products
related user.
how can implement pattern in project? way?
thank in advance.
it simple actually, example in main class if user added 2 elements:
int totalprice = 0; shoppingcartvisitor cart = new shoppingcartvisitorimpl(); itemelement element1 = new fruit(2,3,"apple"); itemelement element2 = new book(30,"some isbn number"); totalprice += element1.visit(cart); totalprice += element2.visit(cart);
in both fruit , book add:
public void accept(shoppingcartvisitor v) { v.visit(this); }
and in shoppingcartvisitorimpl :
public int visit(book b) { return b.getprice(); } public int visit(fruit f) { return f.getpriceperkg()*f.getweight(); }
Comments
Post a Comment