
์๋ฐ์ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๊ธฐ๋ฅ๋ง ๋์ํ๊ฒ ํ๋ ๊ฒ์ ๋ถ์กฑํ๋ค!
๋ณด๊ธฐ ์ข์์ผ ํ๊ณ ์ฌ์ฉํ๊ธฐ๋ ํธํด์ผ๋ง ํ๋ค!!
7์ฅ์์๋ ๋ ๊ฐ์ฒด ์งํฅ์ ์ด๊ณ ๊ฐ๋ ฅํ ์ฝ๋๋ฅผ ๋ง๋๋๋ฐ ์ ์ฉํ๊ฒ ์ฐ์ผ ์๋ฐ์ ๋ณดํธ์ ์ธ ๋์์ธ ์๋ฆฌ๋ฅผ ๊ฐ์กฐํ๊ฒ ๋ค.
7.1 ๋ถ ๋งค๊ฐ๋ณ์๋ก ๋ฉ์๋ ๋ถํ
๋ฌธ์ ์ฝ๋
void log(String message, boolean classified) throws IOException {
if (classified) {
writeMessage(message, CAPTAIN_LOG);
} else {
writeMessage(message, CREW_LOG);
}
}
์ ์ฝ๋๋ boolean์ ์ํ์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ์์ ์ ์ํํ๋ค.
์ด๋ ๋์์ ๋ฌธ์ ๊ฐ ์์ง๋ง ์ฝ๊ธฐ ๋ถํธํ๊ณ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ์ฌ๋์ด True์ False๊ฐ ๊ฐ๊ฐ ๋ฌด์จ ์๋ฏธ์ธ์ง ์๊ณ ์์ด์ผ๋ง ํ๋ค.
์ด ์ฝ๋๋ฅผ ์ข ๋ ํฅ์ ์ํค๋ ค๋ฉด boolean ๋งค๊ฐ๋ณ์๊ฐ ์ค์ ๋ก ์ด๋ค ์ญํ ์ ํ๋์ง ์์๋ณด๊ธฐ ์ฝ๊ฒ ๋ง๋ค์ด์ผ ํ๋ค. (ํผ๋์ ์ค์)
๊ฐ์ ์ฝ๋
void writeToCaptainLog(String message) throws IOException {
writeMessage(message, CAPTAIN_LOG);
}
void writeToCrewLog(String message) throws IOException {
writeMessage(message, CREW_LOG);
}
์ ์ฝ๋์ฒ๋ผ boolean์ด ์ฐ์ธ ๋ฉ์๋๋ฅผ ์ฌ๋ฌ ๊ฐ๋ก ๋ถ๋ฆฌํจ์ผ๋ก์จ ์ฝ๋๊ฐ ํฅ์๋ ์ ์๋ค.
*๊ฐ์ ๋ฐฉ๋ฒ*
1. ๋งค๊ฐ๋ณ์๋ก ๊ตฌ๋ถํ๋ ์ ์ด ํ๋ฆ ๊ฒฝ๋ก๋ง๋ค ์ ๋ฉ์๋๋ฅผ ์ถ๊ฐ
2. ๋ฉ์๋์ ์๋ฏธ ์๋ ์ด๋ฆ์ ์ง์ด์ฃผ์ด ์ฝ๋ ๊ฐ๋ ์ฑ ๋์
๋ฉ์๋๊ฐ ์ด๋์ ์ํ๋ฉฐ ๋ฌด์์ ํ๋์ง ๋ฉ์๋๋ช ๋ง ๋ด๋ ํ์คํ ์ ์ ์์ด์ง.
7.2 ์ต์ ๋งค๊ฐ๋ณ์๋ก ๋ฉ์๋ ๋ถํ
์ต์ ๋งค๊ฐ๋ณ์๋ ๋ถ ๋งค๊ฐ๋ณ์ ์ฒ๋ผ ๋ฉ์๋ ๋ถํ ์ ํตํด ์ฝ๋๋ฅผ ๊ฐ์ ํ ์ ์๋ค.
๋ฌธ์ ์ฝ๋
class Logbook {
static final Path CREW_LOG = Paths.get("/var/log/crew.log");
List<String> readEntries(LocalDate date) throws IOException {
final List<String> entries = Files.readAllLines(CREW_LOG,
StandardCharsets.UTF_8);
if (date == null) {
return entries;
}
List<String> result = new LinkedList<>();
for (String entry : entries) {
if (entry.startsWith(date.toString())) {
result.add(entry);
}
}
return result;
}
}
์ธ์๋ก null ์ธ ๊ฒ๊ณผ null์ด ์๋ ๊ฒ์ด ๋์ด์ฌ ์ ์๋๋ฐ ์ด๋ ์๋ก ๋ค๋ฅธ ์์ ์ด ์คํ๋๋ค.
ํธ์ถ์ ์ ์ฅ์์ null๋ก ์ธ์๋ก ๋๊ธธ ๋ ์ด๋ค ๊ฒฐ๊ณผ๊ฐ ๋์ฌ์ง ์์ํ๊ธฐ ์ด๋ ต๋ค..
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ด ๋ถ๋ถ์ ๋ฆฌํฉํ ๋ง ํ ์ ์๋ค.
๊ฐ์ ์ฝ๋
class Logbook {
static final Path CREW_LOG = Paths.get("/var/log/crew.log");
List<String> readEntries(LocalDate date) throws IOException {
Objects.requireNonNull(date);
List<String> result = new LinkedList<>();
for (String entry : readAllEntries()) {
if (entry.startsWith(date.toString())) {
result.add(entry);
}
}
return result;
}
List<String> readAllEntries() throws IOException {
return Files.readAllLines(CREW_LOG, StandardCharsets.UTF_8);
}
}
'๋ถ ๋งค๊ฐ๋ณ์'๋ฅผ ๊ฐ์ ํ ๊ฒ์ฒ๋ผ '์ต์ ๋งค๊ฐ๋ณ์' ๋ํ ๋ฉ์๋๋ฅผ ์ฌ๋ฌ ๊ฐ๋ก ๋ถํ ํด์ฃผ๋ฉด ๋๋ค.
*๊ฐ์ ๋ฐฉ๋ฒ*
1. ๊ฐ๊ฐ์ ์ ์ด ํ๋ฆ์ ํ๋์ ๋ฉ์๋๋ก ํํ.
2. ๋ฉ์๋๋ช ์ ํตํด ๋ฌด์์ ํ ๊ฒ์ธ์ง ๋ช ํํ ์ ๋ฌ.
readAllEntriles()๋ '๋ชจ๋ ์ํธ๋ฆฌ๋ฅผ ์ฝ์ด์จ๋ค'๋ผ๋ ์๋ฏธ๋ฅผ ํ๋์ ์ ์ ์๊ณ ๋ ์ด์ null์ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
7.3 ๊ตฌ์ฒด ํ์ ๋ณด๋ค ์ถ์ ํ์
๋ณ์์ ์ถ์์ ์ธ ํ์ ์ ์ฌ์ฉํ ์๋ก ์ฝ๋๋ ๋ ์ ์ฐํด์ง๋ค!
๋ฌธ์ ์ฝ๋
class Inventory {
LinkedList<Supply> supplies = new LinkedList();
void stockUp(ArrayList<Supply> delivery) {
supplies.addAll(delivery);
}
LinkedList<Supply> getContaminatedSupplies() {
LinkedList<Supply> contaminatedSupplies = new LinkedList<>();
for (Supply supply : supplies) {
if (supply.isContaminated()) {
contaminatedSupplies.add(supply);
}
}
return contaminatedSupplies;
}
}
class Usage {
static void main(String[] args) {
CargoShip cargoShip = null;
Inventory inventory = null;
Stack<Supply> delivery = cargoShip.unload();
ArrayList<Supply> loadableDelivery = new ArrayList<>(delivery);
inventory.stockUp(loadableDelivery);
}
}
์ ์ฝ๋๋ LinkenList<>, ArraryList<> ๋ฑ์ ๊ตฌ์ฒด ํ์ ์ผ๋ก ํ๋ผ๋ฏธํฐ ํ์ ์ด๋, ๋ณ์ ํ์ ์ ์ง์ ํ๊ณ ์๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ฌ๋ฌ ์๋ฃ ๊ตฌ์กฐ ๊ฐ ํ์ ๊ฐ ๋ณํ์ด ์ฆ์์ง๊ณ Inventoryํด๋์ค๋ฅผ ๋ณ๊ฒฝํ ๊ฒฝ์ฐ ๋ค๋ฅธ ๋ถ๋ถ์๋ ์ํฅ์ ๋ฏธ์น๊ฒ ๋๋ค....(๊ฐ์ฒด ์งํฅ์ ์์ข์)
์ถ์ ํ์ ์ ์ฐ๋ฉด ์ด ๋ฌธ์ ๋ฅผ ๊ฐ์ ํ ์ ์๋ค.
๊ฐ์ ์ฝ๋
class Inventory {
List<Supply> supplies = new LinkedList();
void stockUp(Collection<Supply> delivery) {
supplies.addAll(delivery);
}
List<Supply> getContaminatedSupplies() {
List<Supply> contaminatedSupplies = new LinkedList<>();
for (Supply supply : supplies) {
if (supply.isContaminated()) {
contaminatedSupplies.add(supply);
}
}
return contaminatedSupplies;
}
}
์ฝ๋๊ฐ ๊ฐ์ ๋ 3๊ฐ์ง ์ธก๋ฉด์ ์ดํด๋ณด์.
- supplies ํ๋์ List ์ธํฐํ์ด์ค ์ฌ์ฉ -> ์ธ๋ฒคํ ๋ฆฌ์ ์ ํ์ด ์ด๋ป๊ฒ ์ ์ฅ๋๋์ง ์ ์ ์์ด์ง (๋ค์ํ ๊ฐ์ฒด ๋ฃ์ ์ ์์ด์ง)
- stockUp() ๋ฉ์๋๊ฐ ํ๋ผ๋ฏธํฐ๋ก ์ด๋ค Collection์ด๋ ํ์ฉ -> ์ฆ, ์ด๋ค ํ์ ํ์ ์ด๋ ๋ฉ์๋๋ก ์ ๋ฌ ๊ฐ๋ฅํด์ง.
- getConataminatedSupplies() ๊ตฌ์ฒด์ ์ธ ํ์
์ด ์๋ ์ถ์์ ์ธ List<> ํ์
์ ๋ฐํํจ.
-> ์ ํ์ ๋ฐํ์ Listํํ์ด์ง๋ง ๋ด๋ถ์ ์ผ๋ก ์ด๋ป๊ฒ ์ ์ฅํ๋ ์ง๋ ์ธ๋ถ์ ์๋ฆฌ์ง ์์
class Usage {
static void main(String[] args) {
CargoShip cargoShip = null;
Inventory inventory = null;
Stack<Supply> delivery = cargoShip.unload();
inventory.stockUp(delivery);
}
}
stockUp ์ฌ์ฉ ์ ํ์ ๋ณํ ์์ด Stack ํ์ ์ ๋ฐ๋ก Collection์ผ๋ก ๋ฐ์์ ์ฌ์ฉํ ์ ์๋ค.
7.4 ๊ฐ๋ณ ์ํ๋ณด๋ค ๋ถ๋ณ ์ํ ์ฌ์ฉํ๊ธฐ
๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ฒด์ ์ํ๋ ๋ถ๋ณ์ด๋ค. ๊ฐ๋ฅํ๋ฉด ๊ฐ์ฒด๋ฅผ ๋ถ๋ณ์ผ๋ก ๋ง๋ค์ด์ผ ์๋ชป ์ฌ์ฉํ ๊ฒฝ์ฐ๊ฐ ์ ๋ค.
๋ฌธ์ ์ฝ๋
class Distance {
DistanceUnit unit;
double value;
Distance(DistanceUnit unit, double value) {
this.unit = unit;
this.value = value;
}
static Distance km(double value) {
return new Distance(DistanceUnit.KILOMETERS, value);
}
void add(Distance distance) {
distance.convertTo(unit);
value += distance.value;
}
void convertTo(DistanceUnit otherUnit) {
double conversionRate = unit.getConversionRate(otherUnit);
unit = otherUnit;
value = conversionRate * value;
}
}
class Main {
static void usage() {
Distance toMars = new Distance(DistanceUnit.KILOMETERS, 56_000_000);
Distance marsToVenus = new Distance(DistanceUnit.LIGHTYEARS, 0.000012656528);
Distance toVenusViaMars = toMars;
toVenusViaMars.add(marsToVenus);
}
}
toMars์ toVenusViaMars๋ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํจ๋ค. ๋ฐ๋ผ์, toVenusViaMars.add(marsToVenus)๋ฅผ ์คํํ๋ฉด toMars๊ฐ์ฒด๋ ๋ณ๊ฒฝ์ด ์ผ์ด๋๊ฒ ๋๋ค. (value ๊ฐ์ ๋ณ๋)
๊ฐ์ ์ฝ๋
final class Distance {
final DistanceUnit unit;
final double value;
Distance(DistanceUnit unit, double value) {
this.unit = unit;
this.value = value;
}
Distance add(Distance distance) {
return new Distance(unit, value + distance.convertTo(unit).value);
}
Distance convertTo(DistanceUnit otherUnit) {
double conversionRate = unit.getConversionRate(otherUnit);
return new Distance(otherUnit, conversionRate * value);
}
}
class Main {
static void usage() {
Distance toMars = new Distance(DistanceUnit.KILOMETERS, 56_000_000);
Distance marsToVenus = new Distance(DistanceUnit.LIGHTYEARS, 0.000012656528);
Distance toVenusViaMars = toMars.add(marsToVenus)
.convertTo(DistanceUnit.MILES);
}
}
๊ฐ์ฒด๋ ์ ํจํ์ง ์์ ๋ณ๊ฒฝ์ด ์ผ์ด๋์ง ์๋๋ก ๊ฐ๋ณ์ฑ์ ์ ํํด ์ค์ค๋ก๋ฅผ ๋ณดํธํ ์ ์๋ค.
์์ฑ์์ value์ unitํ๋์ final ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ดํ๋ก๋ ๋ณ๊ฒฝํ ์ ์๋ค.
๋ฐ๋ผ์, ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ ค๋ฉด ๋งค๋ฒ ์๋ก์ด ์ธ์คํด์ค๊ฐ ํ์ํ๋ค.
add() ์ convertTo() ํจ์๋ ์ํ ํ ์๋ก์ด ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด ๋ฐํํ๋ค.
์ด๋ ๊ฒ ํ๋ฉด Distance์ ๋ถ๋ณ ์ํ๋ฅผ ์ ์งํ ์ ์๋ค.
ํด๋์ค ์ ์ ์์ final ํค์๋๋ฅผ ๋ถ์ด๋ ๊ฒฝ์ฐ๋ ํด๋์ค๋ฅผ ๋ ์ด์ ํ์ฅํ ์ ์๊ฒ ํ๋ ค๋ ์๋์ด๋ค.
7.5 ์ํ์ ๋์ ๊ฒฐํฉํ๊ธฐ
๋ฌธ์ ์ฝ๋
class Hull {
int holes;
}
class HullRepairUnit {
void repairHole(Hull hull) {
if (isIntact(hull)) {
return;
}
hull.holes--;
}
boolean isIntact(Hull hull) {
return hull.holes == 0;
}
}
์ํ์ ๋์์ ๊ฒฐํฉ์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๊ธฐ๋ณธ ํ ์ค ํ๋์ด๋ค.
์ ์ฝ๋๋ ์ํ์ ๋์์ด ๋ณ๊ฐ์ ํด๋์ค๋ก ๋๋์ด ์๋ค.
์์ ๊ฐ์ด ์ฌ์ฉํ๋ฉด ๋ฌธ์ ๋ '์ ๋ณด ์๋'์ด ๋ถ๊ฐ๋ฅํด์ง๊ณ ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ๋ ์ด๋ ค์์ง๋ค๋ ์ ์ด๋ค.
Hull ํด๋์ค๋ HullRepairUnit ๋ฐ๊ฐ์ ์๋ ํด๋์ค์ด๋ฏ๋ก ์์ ์ ์ํ์ ๋ํด ์ฝ๊ธฐ์ ์ฐ๊ธฐ ์ ๊ทผ์ ์ ๊ณตํด์ผ๋ง ํ๋ค.
์ํ์ ๋์์ ์ฌ๋ฐ๋ฅด๊ฒ ๊ฒฐํฉํด์ ์ฝ๋๋ฅผ ๊ฐ์ ํด๋ณด์.
๊ฐ์ ์ฝ๋
class Hull {
int holes;
void repairHole() {
if (isIntact()) {
return;
}
holes--;
}
boolean isIntact() {
return holes == 0;
}
}
HullRepairUnit์ ์์ ํ ์ง์ ๋ค. ์ด์ Hull์ ์ค์ค๋ก ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
Hull ๋ฉ์๋๋ ์์ ์ ๋ด๋ถ ์ํ๋ฅผ ์ง์ ์ฒ๋ฆฌํ ์ ์๊ณ ๋ฉ์๋ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
๋ํ, ๊ฒํฐ, ์ธํฐ๋ฅผ ํตํด ์ธ๋ถ๋ก ์ํ๋ฅผ ๋ ธ์ถํ์ง ์์๋ ๋๋ค.
์์ฝํ๋ฉด ๋ฉ์๋ ๋ด์์ ์ ๋ ฅ ๋งค๊ฐ๋ณ์๋ง ๋ค๋ฃจ๊ณ ์์ ์ด ์ํ ํด๋์ค ์ธ์คํด์ค ๋ณ์๋ ๋ค๋ฃจ์ง ์๋ ๊ฒฝ์ฐ๋ ์ ์ฌํ ๋ด์ผํ๋ค.
์ด๊ฒ์ ์ํ์ ๋์์ด ๋ถ๋ฆฌ๋์๋ค๋ ์๋ฏธ์ด๊ณ ์ด๋ฌํ ๋ฉ์๋๋ก๋ '์ ๋ณด ์๋'์ด ๋ถ๊ฐ๋ฅํ๋ค.
7.6 ์ฐธ์กฐ ๋์ ํผํ๊ธฐ
๋ฌธ์ ์ฝ๋
class Inventory {
private final List<Supply> supplies;
Inventory(List<Supply> supplies) {
this.supplies = supplies;
}
List<Supply> getSupplies() {
return supplies;
}
}
class Usage {
static void main(String[] args) {
List<Supply> externalSupplies = new ArrayList<>();
Inventory inventory = new Inventory(externalSupplies);
inventory.getSupplies().size(); // == 0
externalSupplies.add(new Supply("Apple"));
inventory.getSupplies().size(); // == 1
inventory.getSupplies().add(new Supply("Banana"));
inventory.getSupplies().size(); // == 2
}
}
์ ์ฝ๋๋ ์ด๋ ๋ถ๋ถ์ด ๋ฌธ์ ๊ฐ ๋ ๊น?
๋จผ์ inventory๋ ๋ด๋ถ์ ์ ํ ๋ฆฌ์คํธ๋ฅผ ์ ํ ๋ณดํธํ์ง ์๋๋ค!
์ ์ฝ๋์์ ์ฌ๊ณ ์ถ๊ฐ๋ ํด๋์ค ๋ด๋ถ๊ฐ ์๋๋๋ผ๋ ์ฌ๋ฌ ๊ณณ์์ ๊ฐ๋ฅํ๋ค....
1. ์ธ๋ถ์์ ์์ฑํ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด ์ฌ๊ณ ์ถ๊ฐ -> externalSupplies.add(new Supply("Apple"));
2. ํด๋์ค์ ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๋ฆฌ์คํธ ์ฐธ์กฐํด ์ธ๋ถ์์ ์ฌ๊ณ ์ถ๊ฐ -> inventory.getSupplies().add(new Supply("Banana"));
๊ทธ๋ ๋ค๋ฉด ํด๋์ค ๋ด๋ถ๋ฅผ ์ด๊ธฐํํ ํ ์กฐ์๋์ง ์๋๋ก ๋ณดํธํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น??
class Inventory {
private final List<Supply> supplies;
Inventory(List<Supply> supplies) {
this.supplies = new ArrayList<>(supplies);
}
List<Supply> getSupplies() {
return Collections.unmodifiableList(supplies);
}
}
class Usage {
static void main(String[] args) {
List<Supply> externalSupplies = new ArrayList<>();
Inventory inventory = new Inventory(externalSupplies);
inventory.getSupplies().size(); // == 0
externalSupplies.add(new Supply("Apple"));
inventory.getSupplies().size(); // == 0
// UnsupportedOperationException
inventory.getSupplies().add(new Supply("Banana"));
}
}
๋จผ์ ์ ์ฝ๋๋ ์ ๋ฌํ ๋ฆฌ์คํธ์ ์ฐธ์กฐ๊ฐ ์๋๋ผ ๋ฆฌ์คํธ ๋ด Supply๊ฐ์ฒด๋ก ๋ด๋ถ ArrayList๋ฅผ ์ฑ์ด๋ค.
-> this.supplies = new ArrayList<>(supplies); (๋ฐฉ์ด ๋ณต์ฌ)
๊ทธ๋ฆฌ๊ณ getSupplies() ํธ์ถ ์ -> return Collections.unmodifiableList(supplies); ๋ก ๋ํํ ํ ๋ฐํํด์ ์ฝ๊ธฐ ์ ๊ทผ๋ง ๊ฐ๋ฅํ๊ฒ ๋ง๋ ๋ค.
์ด๋ ๊ฒ ํ๋ฉด ์ธ๋ถ์์๋ ๋ด๋ถ์ ๋ฆฌ์คํธ๋ฅผ ์กฐ์ํ ์ ์์ผ๋ ์ธ๋ถ์์ ๋ด๋ถ๋ก ์ํฅ์ ์ฃผ์ง ๋ชปํ๋ค.
7.7 ๋ ๋ฐํํ์ง ์๊ธฐ
๋ฉ์๋ ํธ์ถ ์ ๋ฐํํ ๊ฐ์ด ์์ผ๋ฉด ๊ทธ๋ฅ 'null'์ ๋ฐํํ๋ ํ๋ก๊ทธ๋๋จธ๊ฐ ์๋ค. ์ด๊ฒ์ ํ๋ก๊ทธ๋จ์ ์์ ์ฑ์ ํด์น๋ ํ๋์ด๋ค.
๋ฌธ์ ์ฝ๋
class SpaceNations {
static List<SpaceNation> nations = Arrays.asList(
new SpaceNation("US", "United States"),
new SpaceNation("RU", "Russia")
);
static SpaceNation getByCode(String code) {
for (SpaceNation nation : nations) {
if (nation.getCode().equals(code)) {
return nation;
}
}
return null;
}
}
์ ์ฝ๋๋ getByCode() ๋ฉ์๋ ์ฌ์ฉ ์ code๊ฐ ๊ฐ์ ๊ฒ์ด ์๋ค๋ฉด 'null'์ ๋ฐํํ๋ค.
์ด๋ ๊ฐ์ ๊ตญ๊ฐ ์ฝ๋๊ฐ ์๋ค๋ฉด ๋๋ฌด ์ฝ๊ฒ 'NullPointerException'์ ๋ฐ์์ํจ๋ค.
๋ฌผ๋ก ํธ์ถํ๋ ์ชฝ์์ null์ ๊ฒ์ฆํ ์ ์์ง๋ง ๋๊ตฐ๊ฐ๋ ์์ด๋ฒ๋ฆฌ๊ณ ์์ธ๋ฅผ ๋ฐ์ํ๊ฒ ๋ ์ ์๋ค..
๋ฐ๋ผ์, null๋ง๊ณ ๋ค๋ฅธ ๋ฌด์ธ๊ฐ๋ฅผ ๋ฐํํ๋ ์ชฝ์ผ๋ก ๊ฐ์ ํด๋ณด์.
๊ฐ์ ์ฝ๋
class SpaceNations {
/** Null object. */
static final SpaceNation UNKNOWN_NATION = new SpaceNation("", "");
static List<SpaceNation> nations = Arrays.asList(
new SpaceNation("US", "United States"),
new SpaceNation("RU", "Russia")
);
static SpaceNation getByCode(String code) {
for (SpaceNation nation : nations) {
if (nation.getCode().equals(code)) {
return nation;
}
}
return UNKNOWN_NATION;
}
}
์ ์ฝ๋๋ "๋ ๊ฐ์ฒด ํจํด"์ ์ฌ์ฉํ๋ค. null์ ๋ฐํํ๋ ๋์ ๋ ๊ฐ์ฒด(null object), ์ฆ ๊ฐ์ฒด์ ์ค์ง์ ์ธ ๊ฐ์ด ์์์ ๋ช ์์ ์ผ๋ก ํํํ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๋ฐฉ์์ด๋ค.
์์ ์์๋ UNKOWN_NATION -> SpaceNation("", "")
์ด๋ ๊ฒ ํ๋ฉด ํธ์ถ์์ชฝ์์ 'NullPointerException'์ด ๋ฐ์ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ ๊ฐ์ฒด๋ฅผ ๋ฌด์ํ ์ง ์์ธ๋ฅผ ๋์ง ์ง ์ ํํ ์ ์๋ค.
7.8 7์ฅ์์ ๋ฐฐ์ด ๋ด์ฉ
์ฐ๋ฆฌ๋ ์ฝ๋ ์์ฑ์๊ฐ ์ด๋ค ๊ทผ๊ฑฐ๋ก ๋์์ธ ๊ฒฐ์ ์ ๋ด๋ ธ๋์ง ์์๋ด๋๋ฐ ๋ง์ ๋ ธ๋ ฅ์ ๊ธฐ์ธ์ฌ์ผ ํ๋ค.
์ฝ๋ ๋์์ธ์ ํญ์ ์ ํ์ ์ฐ์์ด๋ฉฐ ๊ทธ ๋์ค์ ๋ํ ์ฑ ์์ ๊ฐ๋ฐ์์ ๋ชซ์ด๋ค.
<๊ฐ๋ ์ฑ>, <ํ ์คํธ ๊ฐ๋ฅ์ฑ>, <์ ์ง ๋ณด์์ฑ>, <์ฑ๋ฅ> ๋ฑ์ ํธ๋ ์ด๋ ์คํ๋ฅผ ๊ณ ๋ คํด ๊ฐ๋ฐ์๋ ์ฌ๋ฐ๋ฅธ ๊ฒฐ์ ์ ๋ด๋ ค์ผ ํ๋ค.
'๐์ฝ์ ์ฑ ์ ๋ฆฌ > ์๋ฐ ์ฝ๋ฉ์ ๊ธฐ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐ ์ฝ๋ฉ์ ๊ธฐ์ ] 8์ฅ : ๋ฐ์ดํฐ ํ๋ฆ (0) | 2021.04.16 |
---|---|
[์๋ฐ ์ฝ๋ฉ์ ๊ธฐ์ ] 6์ฅ : ์ฌ๋ฐ๋ฅด๊ฒ ๋๋ฌ๋ด๊ธฐ (0) | 2021.04.15 |
[์๋ฐ ์ฝ๋ฉ์ ๊ธฐ์ ] 5์ฅ : ๋ฌธ์ ๋ฐ์์ ๋๋นํ๊ธฐ (0) | 2021.04.14 |
[์๋ฐ ์ฝ๋ฉ์ ๊ธฐ์ ] 4์ฅ : ์ฌ๋ฐ๋ฅด๊ฒ ๋ช ๋ช ํ๊ธฐ (0) | 2021.04.13 |
[์๋ฐ ์ฝ๋ฉ์ ๊ธฐ์ ] 3์ฅ : ์ฌ๊ธฐ๋กญ๊ฒ ์ฃผ์ ์ฌ์ฉํ๊ธฐ (0) | 2021.04.09 |