Public Vehicle getVehicle(String vehicleType) Bus.java package com.javainterviewpoint īus is a sub class of Vehicle and Overrides the method travel() and returns “Bus is travelling” Car.java package com.javainterviewpoint Ĭar is also a sub class of Vehicle and it provides implementation for the method travel() and returns “Car is travelling” VehicleFactory.java package com.javainterviewpoint Whatever class which is extending the Vehicle class should provide implementation for it ( Bus and Car in our case). Vehicle is a simple abstract class, having an unimplemented method travel(). Factory Pattern in Java Example Vehicle.java (abstract class) package com.javainterviewpoint Let’s take the below Factory Pattern Example, VehicleFactory class which will be returning the Vehicle instance based on the input passed. Factory Pattern lets the sub class to decide which object needs to be created and encapsulates the object creation from the client.The Factory Design Pattern enables the class to have loose coupling and high cohesion, it essentially decouples the calling class from the target class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |