-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package gof.dp.factorymethod; | ||
|
||
public class Avante extends Car{ | ||
|
||
public Avante(){ | ||
carType = "avante"; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gof.dp.factorymethod; | ||
|
||
public abstract class Car { | ||
protected String carType; | ||
|
||
public String toString(){ | ||
return carType; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package gof.dp.factorymethod; | ||
|
||
public abstract class CarFactory { | ||
public abstract Car createCar(String name); | ||
public abstract Car fixedCar(String owner); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package gof.dp.factorymethod; | ||
|
||
public class CarTest { | ||
public static void main(String[] args){ | ||
CarFactory carFactory = new HyunKiFactory(); | ||
Car newCar = carFactory.createCar("sorento"); | ||
|
||
System.out.println(newCar); | ||
|
||
Car myCar = carFactory.fixedCar("zangzangs"); | ||
System.out.println(myCar); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package gof.dp.factorymethod; | ||
|
||
import java.util.HashMap; | ||
|
||
public class HyunKiFactory extends CarFactory{ | ||
|
||
HashMap<String, Car> carMap = new HashMap<>();//owner, car | ||
|
||
@Override | ||
public Car createCar(String name) { | ||
|
||
Car car = null; | ||
|
||
if("avante".equals(name)){ | ||
car = new Avante(); | ||
}else if("sorento".equals(name)){ | ||
car = new Sorento(); | ||
} | ||
|
||
return car; | ||
} | ||
|
||
@Override | ||
public Car fixedCar(String owner) { | ||
|
||
Car myCar = carMap.get(owner); | ||
|
||
if(null == myCar){ | ||
if (owner.equals("zangzangs")){ | ||
myCar = new Avante(); | ||
} | ||
} | ||
return myCar; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package gof.dp.factorymethod; | ||
|
||
public class Sorento extends Car{ | ||
|
||
public Sorento(){ | ||
carType = "sorento"; | ||
} | ||
|
||
|
||
} |