Iterator Pattern: provides a way to access the elements of an
aggregate object sequentially without exposing its underlying representation.
Example
Spare.java
package com.iteratorpattern.example; public class Spare { private String name; private String description; private float price; public Spare(String name, String description, float price) { this.name = name; this.description = description; this.price = price; } public String getName() { return name; } public String getDescription() { return description; } public float getPrice() { return price; } }
BikeSpare.java
package com.iteratorpattern.example; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class BikeSpare implements SpareList { ListspareList = new ArrayList<>(); public BikeSpare() { spareList.add(new Spare("Head Light Assembly", "Head Light holder and blub set", 700)); spareList.add(new Spare("Piston", "Piston with bearing", 1500)); spareList.add(new Spare("Tyres", "Front and back tyres", 1200)); } @Override public Iterator createIterator() { return new BikeSpareItearator(spareList); } }
CarSpare.java
package com.iteratorpattern.example; import java.util.Iterator; public class CarSpare implements SpareList { public final int MAXITEMS = 3; Spare[] spareItems = new Spare[MAXITEMS]; public int noofItems = 0; public CarSpare(){ addItem(new Spare("Bumpers", "Front and Rear Bumper Assembly", 4000)); addItem(new Spare("CrankShaft", "CrankShaft with bearings", 2500)); addItem(new Spare("audio player", "audio player with usb and aux connectivity", 1000)); } public void addItem(Spare spare){ if (noofItems >= MAXITEMS) { System.out.println("can't add more spares"); } else { spareItems[noofItems] = spare; noofItems++; } } public IteratorcreateIterator(){ return new CarSpareIterator(spareItems); } }
TruckSpare.java
package com.iteratorpattern.example; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class TruckSpare implements SpareList { MapspareList = new HashMap<>(); public TruckSpare() { String name = "Front Grill"; spareList.put(name,new Spare(name, "Front Grill Assembly", 3000)); name = "Horn Assembly"; spareList.put(name,new Spare(name, "Horn with Holders Assembly", 1500)); name = "Seat Covers"; spareList.put(name,new Spare(name, "set of seat covers", 1200)); } @Override public Iterator createIterator() { return spareList.values().iterator(); } }
SpareList.java
package com.iteratorpattern.example; import java.util.Iterator; public interface SpareList { public IteratorcreateIterator(); }
BikeSpareItearator.java
package com.iteratorpattern.example; import java.util.Iterator; import java.util.List; public class BikeSpareItearator implements Iterator{ List sparesList; int position; public BikeSpareItearator(List sparesList) { this.sparesList = sparesList; } @Override public boolean hasNext() { if(position>=sparesList.size()){ return false; }else{ return true; } } @Override public Spare next() { Spare spareItem = sparesList.get(position); position++; return spareItem; } }
CarSpareItearator.java
package com.iteratorpattern.example; import java.util.Iterator; public class CarSpareIterator implements Iterator{ Spare[] spareItems; int position; public CarSpareIterator(Spare[] spareItems) { this.spareItems = spareItems; } @Override public boolean hasNext() { if(position>=spareItems.length||spareItems[position]==null){ return false; }else{ return true; } } @Override public Spare next() { Spare spareItem = spareItems[position]; position++; return spareItem; } }
Dealer.java
package com.iteratorpattern.example; import java.util.Iterator; import java.util.List; public class Dealer { private ListspareList; public Dealer(List spareList) { this.spareList = spareList; } public void listMenu(){ for (SpareList spare : spareList) { listMenu(spare.createIterator()); } } public void listMenu(Iterator iterator){ while (iterator.hasNext()) { Spare spare = (Spare) iterator.next(); System.out.format("Item Name %s with Description %s is of price %.2f \n", spare.getName(), spare.getDescription(), spare.getPrice()); } } }
GarageTestDrive.java
package com.iteratorpattern.example; import java.util.ArrayList; import java.util.List; public class GarageTestDrive { public static void main(String[] args) { ListHomespareList = new ArrayList<>(); spareList.add(new BikeSpare()); spareList.add(new CarSpare()); spareList.add(new TruckSpare()); Dealer dealer = new Dealer(spareList); dealer.listMenu(); } }
No comments:
Post a Comment