1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
header image
kennel software . fitness news provides you with up to date news concerning Health and Fitness
Visitor Pattern - Customers Report - Java Sourcecode

Visitor Pattern - Customers Report - Java Sourcecode


Java Source Code Example for the Visitor Design Pattern - implementation version without reflection

Visitor Customers Example - UML Class Diagram

Main.java - The Main class is the client object. It has a reference tot the Object structure. In our case the objects Structure is the Customers objects

package com.oodesign.designpatterns.behavioral.visitor.impl;
package com.oodesign.designpatterns.behavioral.visitor.customers;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Customer c = new Customer("customer1");
c.addOrder(new Order(".order1", "..item1"));
c.addOrder(new Order(".order2", "..item1"));
c.addOrder(new Order(".order3", "..item1"));

Customer c2 = new Customer("customer2");
Order o = new Order(".order_a");
o.addItem(new Item("..item_a1"));
o.addItem(new Item("..item_a2"));
o.addItem(new Item("..item_a3"));
c2.addOrder(o);
c2.addOrder(new Order(".order_b", "..item_b1"));


CustomerGroup customers = new CustomerGroup();
customers.addCustomer(c);
customers.addCustomer(c2);

GeneralReport visitor = new GeneralReport();

customers.accept(visitor);

visitor.displayResults();
}
}

IVisitor and IVisitable interfaces

package com.oodesign.designpatterns.behavioral.visitor.customers;

public interface IVisitable {
public void accept(IVisitor visitor);
}
package com.oodesign.designpatterns.behavioral.visitor.customers;

public interface IVisitor {
public void visit(Customer customer);
public void visit(Order order);
public void visit(Item item);
}

GeneralReport is a visitor class and implements the IVisitor interface. Other Visitors (other reports or other types of visitors) can be added by implementing the IVisitor interface

package com.oodesign.designpatterns.behavioral.visitor.customers;

public class GeneralReport implements IVisitor{

private int customersNo;
private int ordersNo;
private int itemsNo;

public void visit(Customer customer)
{
System.out.println(customer.getName());
customersNo ++;
}
public void visit(Order order)
{
System.out.println(order.getName());
ordersNo++;
}
public void visit(Item item)
{
System.out.println(item.getName());
itemsNo++;
}

public void displayResults()
{
System.out.println("Nr of customers:" + customersNo);
System.out.println("Nr of orders: " + ordersNo);
System.out.println("Nr of itemss: " + itemsNo);
}
}

CustomerGroup, Customer, Order and Item are all visitable classes. A CustomerGroup represents a group of customers, each Customer can have one or more orders and each order can have one ore more Items.

package com.oodesign.designpatterns.behavioral.visitor.customers;

import java.util.ArrayList;
import java.util.Iterator;

public class CustomerGroup {

private ArrayList customers = new ArrayList();

public void accept(IVisitor visitor)
{
for (Iterator it=customers.iterator(); it.hasNext();)
{
((Customer)it.next()).accept(visitor);
}
}

public void addCustomer(Customer customer)
{
customers.add(customer);
}

}
package com.oodesign.designpatterns.behavioral.visitor.customers;

import java.util.ArrayList;
import java.util.Iterator;

public class Customer implements IVisitable{

private String name;

private ArrayList orders = new ArrayList();

public void accept(IVisitor visitor)
{
visitor.visit(this);

for (Iterator it=orders.iterator(); it.hasNext();)
{
((IVisitable)it.next()).accept(visitor);
}
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}

public void addOrder(Order order)
{
orders.add(order);
}


public Customer(String name)
{
this.name = name;
}
}
package com.oodesign.designpatterns.behavioral.visitor.customers;

import java.util.ArrayList;
import java.util.Iterator;

public class Order implements IVisitable {

private String name;

private ArrayList items = new ArrayList();

public Order(String name)
{
this.name = name;
}

public Order(String name, String itemName)
{
this.name = name;
this.addItem(new Item(itemName));
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void accept(IVisitor visitor)
{
visitor.visit(this);

for (Iterator it=items.iterator(); it.hasNext();)
{
((Item)it.next()).accept(visitor);
}
}


public void addItem(Item item)
{
items.add(item);
}
}
package com.oodesign.designpatterns.behavioral.visitor.customers;

public class Item implements IVisitable{

private String name;

public void accept(IVisitor visitor)
{
visitor.visit(this);
}


public Item(String name)
{
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
 
< Prev   Next >