A factory is an object that is used to create other objects. We looked at the preceding reservation interface. Essentially, it is an association between an item for sale and a user who bought it, along with some metadata such as dates and so on, and there will be multiple types of reservation. So, how does the client code create a reservation without knowing about implementation classes such as HotelReservationImpl?
In a factory method pattern, a helper method (or function) is defined, to enable object creation without knowing the implementation class details. For example, in the case of reservation, the simple factory can be this:
func NewReservation(vertical, reservationDate string) Reservation {
switch(vertical) {
case "flight":
return FlightReservationImpl{reservationDate,}
case "hotel":
return HotelReservationImpl{reservationDate,}
default:
return nil
}
}
It can be used as follows:
hotelReservation:= NewReservation("hotel","20180101")