February 18, 2019

Method chaining example with thread example

/**
 *  you can invoke multiple methods in an object to create a object using method chaining
 * 
 */
package com.konzerntech.pattern;

/**
 * @author konzerntech
 *
 */
public class Hotel {

private int hotelId;
private String hotelName;
private String hotelDescription;

public Hotel setHotelId(int hotelId) {
this.hotelId = hotelId;
return this;
}

public Hotel setHotelName(String hotelName) {
this.hotelName = hotelName;
return this;
}

public Hotel setHotelDescription(String hotelDescription) {
this.hotelDescription = hotelDescription;
return this;
}


@Override
public String toString() {
return hotelId +" @ "+hotelName +" @ "+hotelDescription;
}
}



/**
 * 
 */
package com.konzerntech.pattern;

/**
 * @author Apple
 *
 */
public class HotelThread implements Runnable {
private final Hotel hotel = new Hotel();
private int id;
private String name;
private String desc;
public Hotel getHotel() {
return hotel;
}
public HotelThread(int hotelId, String name, String desc) {
this.id = hotelId;
this.name = name;
this.desc = desc;
}

@Override
public void run() {
hotel.setHotelId(id).setHotelName(name).setHotelDescription(desc);
}

}
/**
 * 
 */
package com.konzerntech.pattern;

/**
 * @author Apple
 *
 */
public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {

HotelThread hotel1 = new HotelThread(1, "hyatt", "diear");
HotelThread hotel2 = new HotelThread(2, "burj al arab", "marina");
HotelThread hotel3 = new HotelThread(3, "jw mariot", "dubai");
Thread t1 = new Thread(hotel1);
Thread t2 = new Thread(hotel2);
Thread t3 = new Thread(hotel3);
t1.start();
t2.start();
t3.start();
System.out.println(hotel1.getHotel());
System.out.println(hotel2.getHotel());
System.out.println(hotel3.getHotel());
}

}

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments