-
-
Save anonymous/1571520dd1aa934fce28 to your computer and use it in GitHub Desktop.
AbstractFactory Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Abstract Class | |
public abstract class AbstractVehicel { | |
abstract public void name(); | |
abstract public void manufacturer(); | |
abstract public void horsepower(); | |
} | |
// The ConrectClasses | |
public class Vehicel1 extends AbstractVehicel { | |
@Override | |
public void name() { | |
System.out.println("Ford Mustang"); | |
} | |
@Override | |
public void manufacturer() { | |
System.out.println("Ford"); | |
} | |
@Override | |
public void horsepower() { | |
System.out.println("120"); | |
} | |
} | |
public class Vehicel2 extends AbstractVehicel { | |
@Override | |
public void name() { | |
System.out.println("VW Up"); | |
} | |
@Override | |
public void manufacturer() { | |
System.out.println("Volkswagen"); | |
} | |
@Override | |
public void horsepower() { | |
System.out.println("65"); | |
} | |
} | |
// {...} | |
// The Client | |
public class ListCars { | |
private static List<AbstractVehicel> vehicelList = new ArrayList<>(); | |
public static void main(String[] args) { | |
addVehicelToList(); | |
new ListCars(); | |
} | |
private static void addVehicelToList() { | |
vehicelList.add(new Vehicel1()); | |
vehicelList.add(new Vehicel2()); | |
vehicelList.add(new Vehicel3()); | |
vehicelList.add(new Vehicel4()); | |
} | |
public ListCars() { | |
for(int position = 0; position < vehicelList.size(); ++position) { | |
System.out.println("-----------------------------"); | |
AbstractVehicel vehicel = vehicelList.get(position); | |
vehicel.name(); | |
vehicel.manufacturer(); | |
vehicel.horsepower(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment