im creating little program which produces cars, tractors and mopeds. car has 4 wheels and 4 cilinder engine, tractor has same, and moped has 2 wheels and 1 cilinder engine.
this is what i have at the moment.
next thing i need to do that with 2 factories.. home factory produces vehicles with these quantity of wheels and engine cilinders and foreignfactory produces tractors with 3 cilinder engine and cars with 2 cilinder engines.
here is my code so far

package Vehicles;

public class Car extends Vehicle {

	private int wheels = 4;
	private int engine = 4;
	/**
	 * Method over-ridden from Vehicle
	 * @return wheels
	 */
	public int getWheels() {
		return wheels;
	}
	
	/**
	 * Method over-ridden from Vehicle, returns 
	 * @return Parts
	 */
	public int getEngine() {
		return engine;
	}
	/**
	 * @return returns full specification of the car
	 */
	public String getSpecification() {
		return getClass()+ "\n"+wheels+" wheels\n"
		+engine+" cilinder engine\n"+getTax();
	}
} // End of class

tractor is same as car class

package Vehicles;

public class Moped extends Vehicle {
	
	private int wheels = 2;
	private int engine = 1;
	/**
	 * Method overridden from Vehicle, returns 
	 * @return Parts
	 */
	public int getWheels() {
		return wheels;
	}
	
	/**
	 * Method overridden from Vehicle, returns 
	 * @return Parts
	 */
	public int getEngine() {
		return engine;
	}
	/**
	 * @return returns full specification of the car
	 */
	public String getSpecification() {
		return getClass()+ "\n"+wheels+" wheels\n"
		+engine+" cilinder engine\n"+getTax();
	}
} // End of class
package Vehicles;

public abstract class Vehicle {
	
	public abstract int getWheels();
	public abstract int getEngine();
	public abstract String getSpecification();
	
	public final int getTax() {
		int sum=0;
		if (getClass()!=Moped.class) sum+=getEngine()*15;
		if (getWheels()>2) sum+=getWheels()*7;
		if (getClass()==Tractor.class) sum+=getWheels()*7;
		return sum;
		
	}
} // End of class
package Vehicles;

public class VehicleType {
	private Vehicle vehic;

	public Vehicle getVehicle(String vehicleType) {
	 	if (vehicleType.equals("Car")) vehic = new Car();
	 	else if(vehicleType.equals("Tractor")) vehic = new Tractor();
	 	else if(vehicleType.equals("Moped")) vehic = new Moped();
	 	else vehic = new NullVehicle();
		return vehic;
	}
} // End of class

i've googled alot for abstract factory pattern but there are examples with different products for factories. that would be copy-paste programming if i used same (creating classes HomeCar, ForeignCar and so on).
what would be the better way to create abstract factory?

package Factories;

public abstract class Factories {
public Factories() {
	// TODO Auto-generated constructor stub
}

} // End of class

class ChineseFactory extends Factories {
	public ChineseFactory() {
		// TODO Auto-generated constructor stub
	}
}

class HomeFactory extends Factories {
	public HomeFactory() {
		// TODO Auto-generated constructor stub
	}
}

Recommended Answers

All 3 Replies

import java.util.Map;
import java.util.HashMap;

public abstract class Factory {
     public static Factory defaultInstance = new ChineeseFactory();
     private Map<Class<? extends Factory>, Factory> factories = new HashMap<Class<? extends Factory>, Factory>();     

     public static Factory getDefault() {
          return defaultInstance;
     }

     public static Factory getFactory( Class<? extends Factory> clazz ) throws Throwable {
            Factory fInstance = this.fac
     }

     public abstract <T extends Vehicle> T createVehicle();
}

public abstract class ChineeseFactory extends Factory {
        public <T extends Vehicle> T createVehicle() {
             return (T) new Car();
        }
}

public final class Main {
    
     public static void main( String[] args ) {
          Vehicle v = Factory.getDefault().createVehicle();
     }
}

Something like this?

According to you description every vehicle is described as having a number of wheels and cylinders;

Before talking “Design Pattern” there is too observations about your model:
First:
The abstract class Vehicle that have tree abstract methods:

public abstract int getWheels();


public abstract int getEngine();


public abstract String getSpecification();

Those methods have the same core in the tree subclass car, tractor and moped, so it is more appropriate to implement them in the Vehicle class, the consequence is the Vehicle class is no longer abstract;

The second is that the data members must be declared in the Vehicle class.

Adding some constructors the Vehicle class will be as following:

public class Vehicle {


protected int wheels;
protected int engine;


public Vehicle(int wheels, int engine) {
this.engine = engine;
this.wheels = wheels;
}


public int getWheels() {
return wheels;
}


public int getEngine() {
return engine;
}


public String getSpecification() {
return getClass() + "\n" + wheels + " wheels\n" + engine + " cilinder engine\n" + getTax();
}


public final int getTax() {
int sum = 0;
if (getClass() != Moped.class) {
sum += getEngine() * 15;
}
if (getWheels() > 2) {
sum += getWheels() * 7;
}
if (getClass() == Tractor.class) {
sum += getWheels() * 7;
}
return sum;
}
} // End of class

The imminent consequence is that the car, the moped and the tractor; even home or foreign; become instances of the Vehicle class!!!

Vehicle car=new Vehicle(4,4);
Vehicle tractor=new Vehicle(4,4)
Vehicle moped=new Vehicle(2,1);

….
Now lets talk “Design Pattern”
Before talking about the abstract factory we must talk about the factory pattern because the Abstract Factory pattern is one level of abstraction higher than the factory pattern.

According to James W. Cooper:

Factory Pattern
You should consider using a Factory pattern when
•A class can’t anticipate which kind of class of objects it must create.
•A class uses its subclasses to specify which objects it creates.
•You want to localize the knowledge of which class gets created.
There are several similar variations on the factory pattern to recognize.
1. The base class is abstract and the pattern must return a complete working class.
2. The base class contains default methods and is only subclassed for cases where the default methods are insufficient.
3. Parameters are passed to the factory telling it which of several class types
to return. In this case the classes may share the same method names but may do something quite different.
Abstract Factory Pattern
The Abstract Factory pattern is one level of abstraction higher than the factory pattern. You can use this pattern when you want to return one of several related classes of objects, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories.

There is of corse some extension of Abstract factories maded by John Vlissides that he call PLUGGABLE FACTORY.
Hope this help.

thanks alot, got the code shorter with that by changing what moutanna typed. now i'm stuck on next place.

public class Car extends Vehicle {

	public Car(int wheels, int engine) {
		super(wheels, engine);
	}
} // End of class
public class VehicleType {
	public Vehicle vehic;

	public Vehicle getVehicle(String vehicleType) {
	 	if (vehicleType.equals("Car")) vehic = new Car(4,4);
	 	else if(vehicleType.equals("Tractor")) vehic = new Tractor(4,4);
	 	else if(vehicleType.equals("Moped")) vehic = new Moped(2,1);
	 	else vehic = new NullVehicle(0, 0);
		return vehic;
	}
} // End of class
public abstract class FactoryType {
	public Vehicle v;

	public FactoryType() {
		if (getClass()==ChineseFactory.class) {
			// how to get objects from chinese factory class?
		}
		else if (getClass()==HomeFactory.class) {
			
		}
		else {
			// null-factory class
		}
	}
} // End of class

class ChineseFactory extends FactoryType {
	public Vehicle getVehicle(String vehicleType) {
	 	if (vehicleType.equals("Car")) v = new Car(4,2);
	 	else if(vehicleType.equals("Tractor")) v = new Tractor(3,4);
	 	else if(vehicleType.equals("Moped")) v = new Moped(2,1);
	 	else v = new NullVehicle(0, 0);
		return v;
	}
}

class HomeFactory extends FactoryType {
	public Vehicle getVehicle(String vehicleType) {
	 	if (vehicleType.equals("Car")) v = new Car(4,4);
	 	else if(vehicleType.equals("Tractor")) v = new Tractor(4,4);
	 	else if(vehicleType.equals("Moped")) v = new Moped(2,1);
	 	else v = new NullVehicle(0, 0);
		return v;
	}
}

as i get here, i don't need no more VehicleType class. it's now inside my Factories classes. how to get them out to FactoryType class?
in the main method i need to create like 100 different vehicles and calculate their tax alltogether. if im right this could be done with arraylists and then for each loop for calculating tax.

ArrayList<Vehicle> Vehicle = new ArrayList<Vehicle>();
int sum;
for(Vehicle vehicle : Vehicle){
			System.out.println(vehicle.getSpecification());
			sum += vehicle.getTax();
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.