I am trying to understand and accomplish the task of trying to create a class that extends a generic class that accepts all types of classes. So far I have that working. I am trying to create a class that extends a generic holder class and have this class accept only specific objects.
Example, a class called "ComputerOrder" that will not accept an Apple or Orange object but only a ComputerPart or Peripheral object, such as Motherboard or Printer objects. Been stuck on this for 2 weeks. I can't for the life of me figure this concept out. Any help would be appreciated.
abstract class Product{
protected float price;
abstract float price();
public String toString() {
return "Price = " + String.valueOf(price) + " ";
}
}
class Apple extends Product{}
class Orange extends Product{}
class ComputerPart extends Product{
public ComputerPart(float p){
price = p;
}
public float price() {
return price;
}
}
class Motherboard extends ComputerPart{
protected String manufacturer;
public Motherboard(String mfg, float p) {
super(p);
manufacturer = mfg;
}
public String getManufacturer() {
return manufacturer;
}
}
class Peripheral extends Product{
public Peripheral(float p) {
price = p;
}
public float price() {
return price;
}
}
class Printer extends Peripheral{
protected String model;
public Printer(String model, float p) {
super(p);
this.model = model;
}
public String getModel() {
return model;
}
}
class Cheese extends Product{
public Cheese(float p) {
price = p;
}
public float price() {
return price;
}
}
class Cheddar extends Cheese{
public Cheddar(float p) {
super(p);
}
}
class GenericOrder<T>{
public ArrayList<T> storage = new ArrayList<T>();
private static int counter = 1;
public final int id;
public T obj;
public GenericOrder(){
id = counter;
counter++;
}
public void add(T item){
storage.add(item);
}
public T get(int in){
return obj;
}
public void getId(){
System.out.println(this.id);
}
public String toString(){
String ret = "";
Iterator<T> it = storage.iterator();
while (it.hasNext()) {
ret += it.next() + "\n";
}
return ret;
}
}
class ComputerOrder extends GenericOrder {
public void add(ComputerPart in){
if(in instanceof ComputerPart){
storage.add(in);
}
}
}
public class Tme2{
public static void main(String[] args){
ComputerOrder com = new ComputerOrder();
com.add(new Motherboard("bla", 3.33f))
}
}
You can do it like this:
class ComputerOrder<T extends ComputerProduct> extends GenericOrder<T> {
//...
}
Here, ComputerProduct is a class that extends Product and all your computer products like ComputerPart or Peripheral extend ComputerProduct. Similarly, you could create a class FoodProduct derived from Product, from which Apple, Orange and Cheese are derived:
class FoodOrder<T extends FoodProduct> extends GenericOrder<T> {
//...
}
The declaration <T extends ComputerProduct> is a type restriction, which ensures that all types of T are derived from ComputerPart, otherwise you will get a compiler error.
The ComputerOrder class is still generic, so you could instance an order for all computer products:
ComputerOrder order = new ComputerOrder<ComputerProduct>();
// Add peripherals, printers, motherboards...
// Apples, ... will throw compiler errors...
But you could also restrict it to peripherals only:
ComputerOrder order = new ComputerOrder<Peripheral>();
// Add peripherals, printers,...
// Apples, motherboards (ComputerProduct, but NOT Peripheral) will fail...
Related
I would appreciate any help in solving the following question.
Design and implement a subclass of GenericOrder called ComputerPartyOrder that takes an arbitrary number of different classes of ComputerPart objects, Peripheral objects, Cheese objects, Fruit objects and Service objects.
here is the code for Product class and GerericOrder class.
abstract class Product {
protected float price;
// return the price of a particular product
abstract float price();
//public getType() {
//
//}
}
//------------------------------------------------------------
class ComputerPart extends Product {
public ComputerPart(float p) {
price = p;
}
public float price() { return price; }
}
class Motherboard extends ComputerPart {
protected String manufacturer;
public Motherboard(String mfg, float p) {
super(p);
manufacturer = mfg;
}
public String getManufacturer() { return manufacturer; }
}
class RAM extends ComputerPart {
protected int size;
protected String manufacturer;
public RAM(String mfg, int size, float p) {
super(p);
this.manufacturer = mfg;
this.size = size;
}
public String getManufacturer() { return manufacturer; }
}
class Drive extends ComputerPart {
protected String type;
protected int speed;
public Drive(String type, int speed, float p) {
super(p);
this.type = type;
this.speed = speed;
}
public String getType() { return type; }
public int getSpeed() { return speed; }
}
class Peripheral extends Product {
public Peripheral(float p) {
price = p;
}
public float price() { return price; }
}
class Printer extends Peripheral {
protected String model;
public Printer(String model, float p) {
super(p);
this.model = model;
}
public String getModel() { return model; }
}
class Monitor extends Peripheral {
protected String model;
public Monitor(String model, float p) {
super(p);
this.model = model;
}
public String getModel() { return model; }
}
class Service extends Product {
public Service(float p) {
price = p;
}
public float price() { return price; }
}
class AssemblyService extends Service {
String provider;
public AssemblyService(String pv, float p) {
super(p);
provider = pv;
}
public String getProvider() { return provider; }
}
class DeliveryService extends Service {
String courier;
public DeliveryService(String c, float p) {
super(p);
courier = c;
}
public String getCourier() { return courier; }
}
//-------------------------------------------------------
class Cheese extends Product {
public Cheese(float p) {
price = p;
}
public float price() { return price; }
}
class Cheddar extends Cheese {
public Cheddar(float p) {
super(p);
}
}
class Mozzarella extends Cheese {
public Mozzarella(float p) {
super(p);
}
}
class Fruit extends Product {
public Fruit(float p) {
price = p;
}
public float price() { return price; }
}
class Apple extends Fruit {
public Apple(float p) {
super(p);
}
}
class Orange extends Fruit {
public Orange(float p) {
super(p);
}
}
GenericOrder:
import java.util.ArrayList;
import java.util.List;
public abstract class GenericOrder<T> extends Product {
private static long counter = 1;
private final long id = counter++;
private List<T> Item;
public GenericOrder() {
Item = new ArrayList<T>();
}
public long getid() {
return id;
}
public void addItem(T newItem) {
Item.add(newItem);
}
public List<T> getItem() {
return Item;
}
public void setItem(List<T> Item) {
this.Item = Item;
}
}
EDIT: Code so far
public abstract class ComputerPartyOrder extends GenericOrder {
GenericOrder GOrder = new GenericOrder() {
#Override
float price() {
return 0;
}
};
public void input(Product newitem) {
GOrder.addItem(newitem);
}
public void output() {
System.out.println(GOrder.getItem());
}
}
You have the right idea, but GenericOrder does not need a type parameter T. Instead, you can set the type of Item to Product (the superclass of all the different types of products).
public abstract class GenericOrder extends Product {
private static long counter = 1;
private final long id = counter++;
private List<Product> Item;
public GenericOrder() {
Item = new ArrayList<Product>();
}
public long getid() {
return id;
}
public void addItem(Product newItem) {
Item.add(newItem);
}
public List<Product> getItem() {
return Item;
}
public void setItem(List<Product> Item) {
this.Item = Item;
}
}
You will still be able to call addItem with any instance of a subclass of Product.
I would also suggest renaming Item to item, uppercase names are usually used for types, not variables.
I am implementing Hierarchical Builder Pattern. I have used generics to make return type of my Parent Builder setter as that for child. Still I am facing problem when I use the child setter method after calling parent setter method. I don't want to define my child specific method (setEngine(String) here) in my Parent Builder. Is there any other way around for this problem?
I have made an example snippet for the mentioned problem, identical to this case.
CarFactory -> It returns the object of specific car that user wants
Car -> Parent for all Car types, Swift, Duster etc
Swift -> Specific car
Parent->Child Hierarchy
Car -> Swift
CarBuilder -> SwiftBuilder
Car.java
package Builders;
public class Car {
int tyre;
int seat;
public int getTyre() {
return tyre;
}
public void setTyre(int tyre) {
this.tyre = tyre;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
}
Swift.java
package Builders;
public class Swift extends Car {
boolean safetyAirbag;
String engine;
public boolean isSafetyAirbag() {
return safetyAirbag;
}
public String getEngine() {
return engine;
}
public void setSafetyAirbag(boolean safetyAirbag) {
this.safetyAirbag = safetyAirbag;
}
public void setEngine(String engine) {
this.engine = engine;
}
}
CarBuilder.java
package Builders;
public abstract class CarBuilder {
int tyre;
int seat;
public abstract <B extends CarBuilder>B self();
public abstract <T extends Car>T typeOfCar();
public <B extends CarBuilder>B setTyre(int tyre) {
this.tyre = tyre;
return self();
}
public <B extends CarBuilder> B setSeat(int seat) {
this.seat = seat;
return self();
}
public <C extends Car>C build()
{ C car=this.typeOfCar();
car.setSeat(seat);
car.setTyre(tyre);
return car;
}
}
SwiftBuilder.java
package Builders;
public class SwiftBuilder extends CarBuilder {
String engine;
#Override
public
SwiftBuilder self() {
return this;
}
#Override
public
Swift typeOfCar() {
return new Swift();
}
public SwiftBuilder setEngine(String string) {
this.engine=string;
return this;
}
public Swift build()
{ Swift s=(Swift)super.build();
return s;
}
}
CarFactory.java
package Builders;
public class CarFactory {
public SwiftBuilder getSwiftDesire()
{
return new SwiftBuilder();
}
}
Drivers.java
package Builders;
public class Drivers {
Swift getMyCar() {
Swift s= this.factory().getSwiftDesire().setSeat(4).setEngine("CC").build();
return s;
}
CarFactory factory() {
return new CarFactory();
}
}
In Drivers.java class I am not able to use setEngine() method after setSeat() method,
this.factory().getSwiftDesire().setSeat(4).setEngine("CC").build();
I don't want to declare setEngine in parent class, is there any way around for same?
Thank you in advance!
You need to use generics on the class level, not on the method level, in your CarBuilder:
package Builders;
public abstract class CarBuilder<B extends CarBuilder<B, C>, C extends Car> {
int tyre;
int seat;
public abstract B self();
public abstract C typeOfCar();
public B setTyre(int tyre) {
this.tyre = tyre;
return self();
}
public B setSeat(int seat) {
this.seat = seat;
return self();
}
public C build() {
C car = this.typeOfCar();
car.setSeat(seat);
car.setTyre(tyre);
return car;
}
}
And then you define your SwiftBuilder:
package Builders;
public class SwiftBuilder extends CarBuilder<SwiftBuilder, Swift> {
String engine;
#Override
public SwiftBuilder self() {
return this;
}
#Override
public Swift typeOfCar() {
return new Swift();
}
public SwiftBuilder setEngine(String string) {
this.engine = string;
return this;
}
public Swift build() {
Swift s = super.build();
s.setEngine(engine);
return s;
}
}
And it works.
I try to code a builder pattern for my better understanding. Mostly I relied on GOF and wikipedia.
So my Object is a house with required attribute area and some optional attributes (like windows, doors, rooms etc.)
I will show you the code. Now, I'm not really sure if its correct and I think I don't have a director? I don't get in which cases you need one and how it works.
This is my class house and the innerclass HouseBuilder
public class House {
//required
private final String area;
//optional
private int windows;
private int doors;
private int rooms;
//constructor with HouseBuilder
private House(HouseBuilder builder) {
this.windows = builder.windows;
this.doors = builder.doors;
this.rooms = builder.rooms;
}
public static class HouseBuilder {
//required
private String area;
//optional
private int windows;
private int doors;
private int rooms;
//constructor with required attributes
HouseBuilder(String area) {
this.area = area;
}
//optional attributes
public HouseBuilder windows(int windows) {
this.windows = windows;
return this;
}
public HouseBuilder doors (int doors) {
this.doors = doors;
return this;
}
//function for building
public Housebuild() {
return new House(this);
}
}
Now, I just got a class demo where I can build a house like that:
House house = new House.HouseBuilder("Downtown")
.doors(3).windows(2).build();
But this is not a director like in the books. Is my idea even correct? And why is that better than just using setters?
Thanks!
Your example illustrates classic builder. Director is something like an abstract builder, and in practise it is rarely used because the client class can handle that perfectly well. Example of a director in your case would be:
public class House
{
public final String area;
public windows;
public int doors;
public int rooms;
}
interface HouseBuilder
{
public HouseBuilder area();
public HouseBuilder windows();
public HouseBuilder doorsors();
public HouseBuilder rooms();
public House build();
}
public static class DowntownHouseBuilder implements HouseBuilder
{
House downtownHouse = new House();
public HouseBuilder area()
{
downtownHouse.area = "Downtown";
}
public HouseBuilder windows()
{
downtownHouse.windows = 3;
return this;
}
public HouseBuilder doors()
{
downtownHouse.doors = 2;
return this;
}
public HouseBuilder rooms()
{
downtownHouse.rooms = 2;
return this;
}
public House build()
{
return downtownHouse;
}
}
public static class VilaBuilder implements HouseBuilder
{
House vila new House();
public HouseBuilder area()
{
vila.area = "Downtown";
}
public HouseBuilder windows()
{
vila.windows = 24;
return this;
}
public HouseBuilder doors()
{
vila.doors = 5;
return this;
}
public HouseBuilder rooms()
{
downtownHouse.rooms = 10;
return this;
}
public House build()
{
return vila;
}
}
class Driector
{
private HouseBuilder houseBuilder;
public Driector(HouseBuilder houseBuilder)
{
this.houseBuilder = houseBuilder;
}
public House buildHouse()
{
return this.houseBuilder.area()
.windows()
.doors()
.rooms()
.buid();
}
}
class HouseConstruction
{
public static void main(String[] args)
{
Director director = new Director(new VilaBuilder());
House house = director.buildHouse();
System.out.println("Builder constructed: "+ house);
}
}
Hope this helps clarify what is a Director in Builder pattern.
i have to implement Pizza(American and Neapolitan) decoration pattern with 4 different toppings(Salami,Soudjouk,Onion,Pepper) which extends "TopingDecorator" class and out of them 3 will be added to pizza by "Add Pizza" command.However, the code does not add it to Pizza's TopingDecorator ArrayList. It should be something like below(I am trying to add Salami and Soudjouk to AmericanPan pizza(which extends PlainPizza class)):
AmericanPan a = new American();
Salami s = new Salami(a);
Soudjouk so = new Soudjouk(s);
Here is my PlainPizza class:
public class PlainPizza implements Pizza{
private int cost;
private String name;
private int orderID;
List<ToppingDecorator> topingsOfPizza;
public PlainPizza(int orderID){
this.orderID = orderID;
topingsOfPizza = new ArrayList<ToppingDecorator>();
}
public void addPizza(PlainPizza p) {
Pizza.allPizzas.add(p);
}
public List<ToppingDecorator> getTopingsOfPizza() {
return topingsOfPizza;
}
#Override
public int cost() {
// TODO Auto-generated method stub
return cost;
}
public int getOrderID() {
return orderID;
}
public String getName() {
return name;
}
#Override
public void addTopping() {
}
And here is my AmericanPan class:
public class AmericanPan extends PlainPizza{
// Class Instances
private final int cost = 5;
private String name;
// Constructor
public AmericanPan(int orderID) {
super(orderID);
this.name = "AmericanPan";
}
// Get Cost
#Override
public int cost() {
return cost;
}
// Get Name
public String getName() {
return name;
}
I am tryin to add Salami on American Pan in Salami class:
public class Salami extends ToppingDecorator{
private String name;
ToppingDecorator t;
public Salami(PlainPizza pizza) {
super(pizza);
this.name = "salami";
this.addToping();
}
#Override
public int cost() {
return super.cost() + 3;
}
#Override
public void addTopping() {
t = new Salami(pizza);
pizza.topingsOfPizza.add(t);
}
And I am trying to add it with code below in my function in main class which operates the whole process:
PlainPizza piz = new AmericanPan(orderID);
// Check The Toppings that Pizza contains
if(pizzatops.contains("soudjouk")){
soudjok = true;
}if(pizzatops.contains("salami")){
salami = true;
}if(pizzatops.contains("pepper")){
pepper = true;
}if(pizzatops.contains("onion")){
onion = true;
}
// Add Pizza according to Toppings
for(int g = 0;g<pizzatops.size();g++){
if(pizzatops.get(g).equals("salami")){
Salami s = new Salami(piz);
}else if(pizzatops.get(g).equals("pepper")){
Pepper p = new Pepper(piz);
}else if(pizzatops.get(g).equals("soudjouk")){
Soudjouk p = new Soudjouk(piz);
}
else if(pizzatops.get(g).equals("onion")){
Onion o = new Onion(piz);
}
}
Pizza.allPizzas.add(piz);
System.out.println("AmericanPan pizza added to order " + orderID);
You're going about this all wrong, with the decorator pattern you use different decorator classes to create different type of instances. In your case this means that you can't add multiple toppings to a pizza because the toppings are actually pizzas themselves, so Salami is a salami pizza and Pepper is a pepper pizza and not two toppings
If you want to add multiple toppings to one pizza then Decorator is not the right pattern.
Here is my simplified decorator implementation
interface Pizza {
int cost();
}
public class PlainPizza implements Pizza {
#Override
public int cost() {
return 10;
}
}
public abstract class ToppingDecorator implements Pizza {
private Pizza pizza;
public ToppingDecorator(PlainPizza aPizza) {
pizza = aPizza;
}
#Override
public int cost() {
return pizza.cost();
}
}
public class SalamiPizza extends ToppingDecorator {
public SalamiPizza(PlainPizza aPizza) {
super(aPizza);
}
#Override
public int cost() {
return super.cost() +3;
}
}
public static void main(String[] args) {
SalamiPizza p = new SalamiPizza(new PlainPizza());
System.out.print(p.cost());
}
I tnink, your implementation is wrong. Decorator pattern using interfaces, abstract classes.
Here
You can see, what is the right implementation with Java.
I have the following example in Java:
public abstract class Vehicle {
private final String name;
private final String make;
public Vehicle(final String name, final String make) {
this.make = make;
this.name = name;
}
}
public final class Car extends Vehicle {
public Car(final String name, final String make) {
super(name, make);
}
}
public final class Truck extends Vehicle {
final Integer grossVehicleWeight;
public Truck(final String name, final String make, final Integer gvw) {
super(name, make);
this.grossVehicleWeight = gvw;
}
Say I want to do some work with a vehicle, and the work is not dependent on the subclass of vehicle. So, I have a method in another class like this:
public void doStuff(public final Vehicle vehicle) {
//do stuff here
//then insert it into my database:
insertVehicle(vehicle);
}
However, I want to do different things in my insertVehicle, so I override that method for each subclass:
public void insertVehicle(Car car) { //do stuff for a car }
public void insertVehicle(Truck truck) { //do stuff for a truck }
In my doStuff method, I could use instanceOf to determine the class of the vehicle (Car or Truck), and then cast the vehicle into that class and call the insertVehicle method like this:
public void doStuff(public final Vehicle vehicle) {
//do stuff here
//then insert it into my database:
if (vehicle instanceof Car) {
insertVehicle((Car) vehicle);
} else {
insertVehicle((truck) vehicle);
}
}
However, I have read that using instanceof is not the best way to do this. 1
How could I best rework this so that I do not have to use instanceof?
You can use the Visitor Pattern:
public interface VehicleVisitor {
public void visit(Car car);
public void visit(Truck truck);
}
public class Car extends Vehicle {
#Override
public void insert(VehicleVisitor vehicleVisitor) {
vehicleVisitor.visit(this);
}
}
public class Truck extends Vehicle {
#Override
public void insert(VehicleVisitor vehicleVisitor) {
vehicleVisitor.visit(this);
}
}
public abstract class Vehicle {
public abstract void insert(VehicleVisitor vehicleVisitor);
}
public class VehicleVisitorImpl implements VehicleVisitor {
#Override
public void visit(Car car) {
System.out.println("insert car");
}
#Override
public void visit(Truck truck) {
System.out.println("insert truck");
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Car();
// finally the agnostic call
vehicle.insert(new VehicleVisitorImpl());
}
}
You can make the abstract function inside the vehicle for
public abstract void doStuff()
call this function from the instance of the object that you want to modify
ford.doStuff(); //ford is car instance
and then you can make modification using this.
doStuff()
{
this.cost += 10;
}
Otherwise, you can add a variable for vehicle which indicated what is the vehicle type and return it. Like:
public void doStuff(public final Vehicle vehicle) {
//do stuff here
//then insert it into my database:
if (vehicle.getType()== 'Car') {
insertVehicle((Car) vehicle);
} else {
insertVehicle((truck) vehicle);
}
}
This variable 'vehicleType' will be in the vehicle class and will be initialized inside the constructor:
public final class Car extends Vehicle {
public Car(final String name, final String make, final String vehicleType) {
super(name, make, type);
}
}
It depends on what kind of problem you trying to solve. If it is persistency make sure you are not reinventing JPA. If it is type-specific processing then you can solve it as #denis suggested. Or if you want to keep entities in POJO-style you can use strategy pattern like:
Map<Class<?>, Consumer<Vehicle>> consumers;
{
consumers.put(Car.class, v -> insertVehicle((Car)v));
consumers.put(Truck.class, v -> insertVehicle((Truck)v));
}
public void doStuff(public final Vehicle vehicle) {
//do stuff here
consumers
.get(vehicle.getClass())
.accept(vehicle);
}
One way is to have the insertVehicle() method abstract in Vehicle. And then implement them in the subclasses Car and Truck.
However, this moves the logic into the POJOs. Maybe it is better to separate db-logic from the POJOs, i.e. just use instanceof in this case.
public abstract class Vehicle {
private final String name;
private final String make;
public Vehicle(final String name, final String make) {
this.make = make;
this.name = name;
}
public abstract void insertVehicle();
}
public final class Car extends Vehicle {
public Car(final String name, final String make) {
super(name, make);
}
public void insertVehicle() {
}
}
public final class Truck extends Vehicle {
final Integer grossVehicleWeight;
public Truck(final String name, final String make, final Integer gvw) {
super(name, make);
this.grossVehicleWeight = gvw;
}
public void insertVehicle() {
}
}
public void doStuff(Vehicle vehicle) {
//do stuff here
//then insert it into my database:
vehicle.insertVehicle();
}
If you don't like putting doStuff() into Car and Truck, you could have a doStuff() method for each of them, and put the common Vehicle logic into another method.
private void doCommonStuff(final Vehicle vehicle) {
//do stuff here
}
public void doStuff(final Car car) {
doCommonStuff(car);
//then insert it into my database:
insertVehicle(car);
}
public void doStuff(final Truck truck) {
doCommonStuff(truck);
//then insert it into my database:
insertVehicle(truck);
}
We can do better, though, with generics.
public abstract class StuffDoer<T extends Vehicle> {
public void doStuff(final T vehicle) {
// do stuff here
// then insert it into my database:
insertVehicle(vehicle);
}
public abstract void insertVehicle(T vehicle);
}
public class CarStuffDoer extends StuffDoer<Car> {
public void insertVehicle(Car car) {
// whatever
}
}
public class TruckStuffDoer extends StuffDoer<Truck> {
public void insertVehicle(Truck truck) {
// whatever else
}
}