diff --git a/.gitignore b/.gitignore index 73281d9..946b88e 100644 --- a/.gitignore +++ b/.gitignore @@ -167,4 +167,9 @@ gradle-app.setting # Java heap dump *.hprof -# End of https://www.toptal.com/developers/gitignore/api/java,intellij,gradle \ No newline at end of file +# End of https://www.toptal.com/developers/gitignore/api/java,intellij,gradle + +.idea +parksey.imi + +*.iml \ No newline at end of file diff --git a/README.md b/README.md index dca7afb..844293b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,35 @@ -# DesignPatternStudy -디자인 패턴 스터디를 위한 저장소 입니다. +# 디자인 패턴 스터디 + +## 1. 디자인 패턴 학습의 필요성 + +- 보다 효율 적인 의사 소통을 위해 + - 디자인 패턴은 공통된 언어를 정의 하며 이를 바탕으로 효율적인 의사소통이 가능합니다. 예로 들어 ‘전략 패턴으로 리팩터링 하자’라고 말하면 디자인 패턴을 아는사람은 상대방의 목적을 바로 이해 할 수 있습니다. +- 디자인 패턴의 근본적인 이해를 위해 + - “망치만 있으면 모든 것이 못처럼 보인다”라는 말이 있습니다. 아무리 좋은 망치를 들고 있다고 해도 적절하게 사용하지 않으면 망치가 없는 것 보다 못한일이 발생합니다. 디자인 패턴도 그렇습니다. 근본적이 학습 없이 모든 곳에 디자인 패턴을 적용하면 복잡성만 증가 합니다. + - 모든 프로젝트의 문제와 상황(Context)이 다릅니다. 따라서 디자인패턴을 각각 프로젝에 적용할 수 있는 능력이 중요합니다. 이를 위해서는 디자인 패턴의 근본적인 이해와 경험이 필요합니다. + +## 2. 스터디 목표 + +- 모든 디자인 패턴의 암기가 아니라 근본적인 이해를 목표로 합니다. +- 책의 예제를 조금이라도 변형해서 Github에 업로드 +- 모르는 부분은 서로 공유하고 토론을 통해 해소하기 +- 즐겁게 하기 😊 + +## 3. 스터디 방식 + +- 각 주마다 서로의 일정을 공유하여 스터디 일정 조율(주 3회 목표) +- 개인마다 핵심 정리 후 모였을 때 모르는 내용이 있다면 토론 +- 각 챕터 마다 예제를 변형한 문제를 코드로 작성하고 Github에 업로드 +- 순서 : 병곤님 - 승원님 - 종운님 - 원우님 - 세연님 + +## 4. 참여 인원 + +@유원우 @문종운 @세연 박 @한승원 @강병곤 + +## 5. Github 주소 + +[GitHub - Study-Family/DesignPatternStudy: 디자인 패턴 스터디를 위한 저장소 입니다.](https://github.com/Study-Family/DesignPatternStudy) + +## 6. 스터디 일정 + +[디자인 패턴 스터디 일정](https://www.notion.so/398723e98cb24894a9746a493c8bf13d?pvs=21) diff --git a/chapter1/parksey/character/Character.java b/chapter1/parksey/character/Character.java new file mode 100644 index 0000000..5320a44 --- /dev/null +++ b/chapter1/parksey/character/Character.java @@ -0,0 +1,13 @@ +package strategypattern.puzzle1.character; + +import strategypattern.puzzle1.weapon.WeaponBehavior; + +public abstract class Character { + WeaponBehavior weaponBehavior; + + public void setWeapon(WeaponBehavior weapon) { + this.weaponBehavior = weapon; + } + + abstract void fight(); +} diff --git a/chapter1/parksey/character/King.java b/chapter1/parksey/character/King.java new file mode 100644 index 0000000..a287ce8 --- /dev/null +++ b/chapter1/parksey/character/King.java @@ -0,0 +1,14 @@ +package strategypattern.puzzle1.character; + +import strategypattern.puzzle1.weapon.KnifeBehavior; + +public class King extends Character { + public King() { + this.weaponBehavior = new KnifeBehavior(); + } + + @Override + public void fight() { + System.out.println("king"); + } +} diff --git a/chapter1/parksey/character/Knight.java b/chapter1/parksey/character/Knight.java new file mode 100644 index 0000000..9b9d258 --- /dev/null +++ b/chapter1/parksey/character/Knight.java @@ -0,0 +1,14 @@ +package strategypattern.puzzle1.character; + +import strategypattern.puzzle1.weapon.SwordBehavior; + +public class Knight extends Character{ + public Knight() { + this.weaponBehavior = new SwordBehavior(); + } + + @Override + public void fight() { + System.out.println("knight"); + } +} diff --git a/chapter1/parksey/character/Queen.java b/chapter1/parksey/character/Queen.java new file mode 100644 index 0000000..074d197 --- /dev/null +++ b/chapter1/parksey/character/Queen.java @@ -0,0 +1,14 @@ +package strategypattern.puzzle1.character; + +import strategypattern.puzzle1.weapon.BowAndArrowBehavior; + +public class Queen extends Character{ + public Queen() { + this.weaponBehavior = new BowAndArrowBehavior(); + } + + @Override + void fight() { + System.out.println("queen"); + } +} diff --git a/chapter1/parksey/character/Troll.java b/chapter1/parksey/character/Troll.java new file mode 100644 index 0000000..d206a30 --- /dev/null +++ b/chapter1/parksey/character/Troll.java @@ -0,0 +1,14 @@ +package strategypattern.puzzle1.character; + +import strategypattern.puzzle1.weapon.AxeBehavior; + +public class Troll extends Character{ + public Troll() { + this.weaponBehavior = new AxeBehavior(); + } + + @Override + public void fight() { + System.out.println("troll"); + } +} diff --git a/chapter1/parksey/weapon/AxeBehavior.java b/chapter1/parksey/weapon/AxeBehavior.java new file mode 100644 index 0000000..02e2be5 --- /dev/null +++ b/chapter1/parksey/weapon/AxeBehavior.java @@ -0,0 +1,8 @@ +package strategypattern.puzzle1.weapon; + +public class AxeBehavior implements WeaponBehavior { + @Override + public void userWeapon() { + + } +} diff --git a/chapter1/parksey/weapon/BowAndArrowBehavior.java b/chapter1/parksey/weapon/BowAndArrowBehavior.java new file mode 100644 index 0000000..31e1227 --- /dev/null +++ b/chapter1/parksey/weapon/BowAndArrowBehavior.java @@ -0,0 +1,8 @@ +package strategypattern.puzzle1.weapon; + +public class BowAndArrowBehavior implements WeaponBehavior{ + @Override + public void userWeapon() { + + } +} diff --git a/chapter1/parksey/weapon/KnifeBehavior.java b/chapter1/parksey/weapon/KnifeBehavior.java new file mode 100644 index 0000000..5d6a805 --- /dev/null +++ b/chapter1/parksey/weapon/KnifeBehavior.java @@ -0,0 +1,8 @@ +package strategypattern.puzzle1.weapon; + +public class KnifeBehavior implements WeaponBehavior{ + @Override + public void userWeapon() { + + } +} diff --git a/chapter1/parksey/weapon/SwordBehavior.java b/chapter1/parksey/weapon/SwordBehavior.java new file mode 100644 index 0000000..4a66314 --- /dev/null +++ b/chapter1/parksey/weapon/SwordBehavior.java @@ -0,0 +1,8 @@ +package strategypattern.puzzle1.weapon; + +public class SwordBehavior implements WeaponBehavior{ + @Override + public void userWeapon() { + + } +} diff --git a/chapter1/parksey/weapon/WeaponBehavior.java b/chapter1/parksey/weapon/WeaponBehavior.java new file mode 100644 index 0000000..2df99e2 --- /dev/null +++ b/chapter1/parksey/weapon/WeaponBehavior.java @@ -0,0 +1,5 @@ +package strategypattern.puzzle1.weapon; + +public interface WeaponBehavior { + void userWeapon(); +} diff --git a/chapter2/parksey/StockApplication.java b/chapter2/parksey/StockApplication.java new file mode 100644 index 0000000..51c7241 --- /dev/null +++ b/chapter2/parksey/StockApplication.java @@ -0,0 +1,11 @@ +package observerpattern.puzzle; + +import observerpattern.puzzle.transaction.TransactionController; + +public class StockApplication { + public static void main(String[] args) { + TransactionController stockController = new TransactionController(); + stockController.trading(); + + } +} diff --git a/chapter2/parksey/company/domain/Company.java b/chapter2/parksey/company/domain/Company.java new file mode 100644 index 0000000..707d47c --- /dev/null +++ b/chapter2/parksey/company/domain/Company.java @@ -0,0 +1,37 @@ +package observerpattern.puzzle.company.domain; + +import observerpattern.puzzle.user.domain.User; + +import java.util.ArrayList; +import java.util.List; + +// entity +public class Company { + private String name; + private int price; + + private List users; + + public Company(String name, int price) { + this.name = name; + this.price = price; + + this.users = new ArrayList<>(); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public List getUsers() { + return users; + } + + public void addUser(User user) { + this.users.add(user); + } +} diff --git a/chapter2/parksey/company/dto/CompanyDatas.java b/chapter2/parksey/company/dto/CompanyDatas.java new file mode 100644 index 0000000..f9c9c38 --- /dev/null +++ b/chapter2/parksey/company/dto/CompanyDatas.java @@ -0,0 +1,57 @@ +package observerpattern.puzzle.company.dto; + +import observerpattern.puzzle.company.domain.Company; +import observerpattern.puzzle.user.dto.Observer; +import observerpattern.puzzle.user.dto.UserObserver; + +import java.util.ArrayList; +import java.util.List; + +public class CompanyDatas implements CompanySubject{ + private String name; + private int price; + private List observers; + + public CompanyDatas(String name, int price) { + this.name = name; + this.price = price; + this.observers = new ArrayList<>(); + } + + public static CompanyDatas addSubject(Company company) { + CompanyDatas companyDatas = new CompanyDatas(company.getName(), company.getPrice()); + company.getUsers().stream() + .forEach(user -> UserObserver.toDto(companyDatas, user)); + + return companyDatas; + } + + @Override + public void addObserver(Observer observer) { + this.observers.add(observer); + } + + @Override + public void removeObserver(Observer observer) { + this.observers.remove(observer); + } + + @Override + public void notifyObserver() { + this.observers.stream() + .forEach(observer -> observer.notification()); + } + + public void updatePrice(int addPirce) { + this.price += addPirce; + notifyObserver(); + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } +} diff --git a/chapter2/parksey/company/dto/CompanySubject.java b/chapter2/parksey/company/dto/CompanySubject.java new file mode 100644 index 0000000..53770f4 --- /dev/null +++ b/chapter2/parksey/company/dto/CompanySubject.java @@ -0,0 +1,12 @@ +package observerpattern.puzzle.company.dto; + +import observerpattern.puzzle.user.dto.Observer; + +public interface CompanySubject { + void addObserver(Observer observer); + void removeObserver(Observer observer); + void notifyObserver(); + + String getName(); + int getPrice(); +} diff --git a/chapter2/parksey/transaction/TransactionController.java b/chapter2/parksey/transaction/TransactionController.java new file mode 100644 index 0000000..377f47d --- /dev/null +++ b/chapter2/parksey/transaction/TransactionController.java @@ -0,0 +1,50 @@ +package observerpattern.puzzle.transaction; + +import observerpattern.puzzle.company.domain.Company; +import observerpattern.puzzle.company.dto.CompanyDatas; +import observerpattern.puzzle.user.domain.User; + +import java.util.ArrayList; + +import java.util.List; +import java.util.stream.Collectors; + +public class TransactionController { + private static List initCompanys; + private static List initUsers; + + static { + User user1 = new User("PSY", "010-XXXX-XXXX"); + User user2 = new User("PSY2", "010-XXXX-XXXX"); + + initUsers = new ArrayList<>(); + initUsers.add(user1); + initUsers.add(user2); + + Company sam = new Company("삼성", 70_000); + sam.addUser(user1); + sam.addUser(user2); + + Company auto = new Company("현대오토에버", 100_000); + auto.addUser(user1); + + initCompanys = new ArrayList<>(); + initCompanys.add(sam); + initCompanys.add(auto); + } + + public TransactionController() {} + + public void trading() { + List companyDatasList = new ArrayList<>(); + initCompanys.stream() + .forEach(company -> companyDatasList.add(CompanyDatas.addSubject(company))); + update(companyDatasList); + } + + private void update(List companyDatasList) { + companyDatasList.stream() + .forEach(companyDatas -> companyDatas.updatePrice(1000)); + } + +} diff --git a/chapter2/parksey/user/domain/User.java b/chapter2/parksey/user/domain/User.java new file mode 100644 index 0000000..c85810a --- /dev/null +++ b/chapter2/parksey/user/domain/User.java @@ -0,0 +1,19 @@ +package observerpattern.puzzle.user.domain; + +public class User { + private String name; + private String personalData; + + public User(String name, String personalData) { + this.name = name; + this.personalData = personalData; + } + + public String getName() { + return name; + } + + public String getPersonalData() { + return personalData; + } +} diff --git a/chapter2/parksey/user/dto/Observer.java b/chapter2/parksey/user/dto/Observer.java new file mode 100644 index 0000000..79698fe --- /dev/null +++ b/chapter2/parksey/user/dto/Observer.java @@ -0,0 +1,5 @@ +package observerpattern.puzzle.user.dto; + +public interface Observer { + void notification(); +} diff --git a/chapter2/parksey/user/dto/UserObserver.java b/chapter2/parksey/user/dto/UserObserver.java new file mode 100644 index 0000000..0535ce7 --- /dev/null +++ b/chapter2/parksey/user/dto/UserObserver.java @@ -0,0 +1,31 @@ +package observerpattern.puzzle.user.dto; + +import observerpattern.puzzle.company.dto.CompanyDatas; +import observerpattern.puzzle.company.dto.CompanySubject; +import observerpattern.puzzle.user.domain.User; + +public class UserObserver implements Observer { + private CompanySubject companySubject; + private String name; + private int price; + private User user; + + public UserObserver(CompanySubject companySubject, User user) { + this.companySubject = companySubject; + this.user = user; + this.price = companySubject.getPrice(); + this.name = companySubject.getName(); + + this.companySubject.addObserver(this); + } + + public static UserObserver toDto(CompanySubject companySubject, User user) { + return new UserObserver(companySubject, user); + } + + @Override + public void notification() { + this.name = companySubject.getName(); + this.price = companySubject.getPrice(); + } +} diff --git a/chapter3/parksey/DecorateApplication.java b/chapter3/parksey/DecorateApplication.java new file mode 100644 index 0000000..4c4b355 --- /dev/null +++ b/chapter3/parksey/DecorateApplication.java @@ -0,0 +1,21 @@ +package decoratepattern.puzzle; + +import decoratepattern.puzzle.controller.DecorateController; +import decoratepattern.puzzle.ui.InputView; +import decoratepattern.puzzle.ui.OutputView; + +import java.util.Scanner; + +public class DecorateApplication { + public static void main(String[] args) { + DecorateController decorateController = new DecorateController( + new InputView(scannerInstance()) + , new OutputView() + ); + + } + + public static Scanner scannerInstance() { + return new Scanner(System.in); + } +} diff --git a/chapter3/parksey/controller/DecorateController.java b/chapter3/parksey/controller/DecorateController.java new file mode 100644 index 0000000..3a78e05 --- /dev/null +++ b/chapter3/parksey/controller/DecorateController.java @@ -0,0 +1,67 @@ +package decoratepattern.puzzle.controller; + +import decoratepattern.puzzle.domain.*; +import decoratepattern.puzzle.exception.InputException; +import decoratepattern.puzzle.ui.InputView; +import decoratepattern.puzzle.ui.OutputView; + +import java.util.function.Supplier; + +public class DecorateController { + private final InputView inputView; + private final OutputView outputView; + + public DecorateController(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + + public void userInput() { + + + User user = new User(getUserName(), getPhoneNumber(), getAddress()); + + UserInfo userInfo = new UserName(); + userInfo = new UserNumber(userInfo); + userInfo = new UserAddress(userInfo); + + + userInfo.save(user); + + userInfo.read(); + + } + + private String getUserName() { + return getInput(()-> { + this.outputView.printName(); + return inputView.getUserInput(); + }); + } + + private String getPhoneNumber() { + return getInput(()-> { + this.outputView.printName(); + return inputView.getUserInput(); + }); + } + + private String getAddress() { + return getInput(()-> { + this.outputView.printName(); + return inputView.getUserInput(); + }); + } + + + private T getInput(Supplier inputSupplier) { + while(true) { + try { + return inputSupplier.get(); + } catch (InputException error) { + this.outputView.printMsg(error.getMessage()); + } + } + } +} diff --git a/chapter3/parksey/domain/Cryptoable.java b/chapter3/parksey/domain/Cryptoable.java new file mode 100644 index 0000000..710ea0b --- /dev/null +++ b/chapter3/parksey/domain/Cryptoable.java @@ -0,0 +1,10 @@ +package decoratepattern.puzzle.domain; + +import java.security.NoSuchAlgorithmException; + +public interface Cryptoable { + String parseCrtpto(String noraml) throws NoSuchAlgorithmException; + String deparseCrytpto(String crypto); +} + +s \ No newline at end of file diff --git a/chapter3/parksey/domain/User.java b/chapter3/parksey/domain/User.java new file mode 100644 index 0000000..e08a322 --- /dev/null +++ b/chapter3/parksey/domain/User.java @@ -0,0 +1,25 @@ +package decoratepattern.puzzle.domain; + +public class User { + private String name; + private String phoneNumber; + private String address; + + public User(String name, String phoneNumber, String address) { + this.name = name; + this.phoneNumber = phoneNumber; + this.address = address; + } + + public String getName() { + return name; + } + + public String getAddress() { + return address; + } + + public String getPhoneNumber() { + return phoneNumber; + } +} diff --git a/chapter3/parksey/domain/UserAddress.java b/chapter3/parksey/domain/UserAddress.java new file mode 100644 index 0000000..8577c41 --- /dev/null +++ b/chapter3/parksey/domain/UserAddress.java @@ -0,0 +1,29 @@ +package decoratepattern.puzzle.domain; + +import decoratepattern.puzzle.exception.InputException; + +public class UserAddress implements UserInfo{ + private UserInfo userInfo; + + public UserAddress(UserInfo userInfo) { + this.userInfo = userInfo; + } + + @Override + public boolean save(User user) { + validation(user.getAddress()); + return this.userInfo.save(user); + } + + + @Override + public void validation(String input) { + InputException.checkAddress(); + } + + @Override + public User read() { + return this.userInfo.read(); + } + +} diff --git a/chapter3/parksey/domain/UserInfo.java b/chapter3/parksey/domain/UserInfo.java new file mode 100644 index 0000000..e84921d --- /dev/null +++ b/chapter3/parksey/domain/UserInfo.java @@ -0,0 +1,14 @@ +package decoratepattern.puzzle.domain; + +import decoratepattern.puzzle.exception.InputException; + +import java.security.NoSuchAlgorithmException; +import java.util.function.Supplier; + +public interface UserInfo { + boolean save(User user) throws NoSuchAlgorithmException; + void validation(String input); + + User read(); + +} diff --git a/chapter3/parksey/domain/UserName.java b/chapter3/parksey/domain/UserName.java new file mode 100644 index 0000000..214b39b --- /dev/null +++ b/chapter3/parksey/domain/UserName.java @@ -0,0 +1,29 @@ +package decoratepattern.puzzle.domain; + +import decoratepattern.puzzle.exception.InputException; + +public class UserName implements UserInfo{ + private User user; + + public UserName() { + + } + + @Override + public boolean save(User user) { + validation(user.getName()); + this.user = user; + return true; + } + + + @Override + public void validation(String guserName) { + InputException.checkUserName(); + } + + @Override + public User read() { + return user; + } +} diff --git a/chapter3/parksey/domain/UserNumber.java b/chapter3/parksey/domain/UserNumber.java new file mode 100644 index 0000000..ed54685 --- /dev/null +++ b/chapter3/parksey/domain/UserNumber.java @@ -0,0 +1,42 @@ +package decoratepattern.puzzle.domain; + +import decoratepattern.puzzle.exception.InputException; + +import java.security.NoSuchAlgorithmException; + + +public class UserNumber implements UserInfo, Cryptoable{ + private UserInfo userInfo; + private String secretKey = "123456"; + + public UserNumber(UserInfo userInfo) { + this.userInfo = userInfo; + } + + @Override + public boolean save(User user) throws NoSuchAlgorithmException { + validation(user.getPhoneNumber()); + return this.userInfo.save(new User(user.getName(), parseCrtpto(user.getPhoneNumber()), user.getAddress())); + } + + @Override + public void validation(String input) { + InputException.checkPhoneNumber(); + } + + @Override + public User read() { + User user = this.userInfo.read(); + return new User(user.getName(), deparseCrytpto(user.getPhoneNumber()), user.getAddress()); + } + + @Override + public String parseCrtpto(String noraml) throws NoSuchAlgorithmException { + return noraml+secretKey; + } + + @Override + public String deparseCrytpto(String crypto) { + return crypto.substring(0,crypto.length()-secretKey.length()); + } +} diff --git a/chapter3/parksey/exception/InputException.java b/chapter3/parksey/exception/InputException.java new file mode 100644 index 0000000..20b7996 --- /dev/null +++ b/chapter3/parksey/exception/InputException.java @@ -0,0 +1,19 @@ +package decoratepattern.puzzle.exception; + +public class InputException extends RuntimeException{ + public InputException(String msg) { + super(msg); + } + + public static void checkUserName() { + + } + + public static void checkPhoneNumber() { + + } + + public static void checkAddress() { + + } +} diff --git a/chapter3/parksey/ui/InputView.java b/chapter3/parksey/ui/InputView.java new file mode 100644 index 0000000..a8d480f --- /dev/null +++ b/chapter3/parksey/ui/InputView.java @@ -0,0 +1,14 @@ +package decoratepattern.puzzle.ui; + +import java.util.Scanner; + +public class InputView { + public Scanner scanner; + public InputView(Scanner scanner) { + this.scanner = scanner; + } + + public String getUserInput() { + return this.scanner.nextLine(); + } +} diff --git a/chapter3/parksey/ui/OutputView.java b/chapter3/parksey/ui/OutputView.java new file mode 100644 index 0000000..02590ce --- /dev/null +++ b/chapter3/parksey/ui/OutputView.java @@ -0,0 +1,14 @@ +package decoratepattern.puzzle.ui; + +public class OutputView { + private static final String 이름 = "이름 입력 : "; + private static final String 전화번호 = "전화번호 입력 : "; + private static final String 주소 = "주소 입력 : "; + public void printMsg(String msg) { + System.out.println(msg); + } + + public void printName() { + System.out.print(이름); + } +} diff --git a/chapter4/parksey/.gitignore b/chapter4/parksey/.gitignore new file mode 100644 index 0000000..b9448e9 --- /dev/null +++ b/chapter4/parksey/.gitignore @@ -0,0 +1,32 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +/.idea +parksey.iml \ No newline at end of file diff --git a/chapter4/parksey/src/ApplicationTest.java b/chapter4/parksey/src/ApplicationTest.java new file mode 100644 index 0000000..ae5f9af --- /dev/null +++ b/chapter4/parksey/src/ApplicationTest.java @@ -0,0 +1,11 @@ +import main.Application; +import main.gui.GUIFactory; + +public class ApplicationTest { + public static void main(String[] args) { + + String osConfig = "Win"; + + Application app = new Application(GUIFactory.getInstance(osConfig)); + } +} \ No newline at end of file diff --git a/chapter4/parksey/src/main/Application.java b/chapter4/parksey/src/main/Application.java new file mode 100644 index 0000000..5ad2912 --- /dev/null +++ b/chapter4/parksey/src/main/Application.java @@ -0,0 +1,21 @@ +package main; + +import main.gui.GUIFactory; + +/** + * 사용자에의 해서 어플리케이션이 실행 + * -> + * 하나의 어플리케이션에는 하나의 GUIFactory만 있어야 하고 또 다시 생성할 필요가 없다고 생각했습니다. + */ +public class Application { + private final GUIFactory guiFactory; + + public Application(GUIFactory guiFactory) { + this.guiFactory = guiFactory; + } + public void execute() { + this.guiFactory. + } + + +} diff --git a/chapter4/parksey/src/main/button/Button.java b/chapter4/parksey/src/main/button/Button.java new file mode 100644 index 0000000..f578bac --- /dev/null +++ b/chapter4/parksey/src/main/button/Button.java @@ -0,0 +1,5 @@ +package main.button; + +public interface Button { + void onClick(); +} diff --git a/chapter4/parksey/src/main/button/MacButton.java b/chapter4/parksey/src/main/button/MacButton.java new file mode 100644 index 0000000..353b6c5 --- /dev/null +++ b/chapter4/parksey/src/main/button/MacButton.java @@ -0,0 +1,9 @@ +package main.button; + +public class MacButton implements Button{ + + @Override + public void onClick() { + System.out.println("Mac 버튼"); + } +} diff --git a/chapter4/parksey/src/main/button/WinButton.java b/chapter4/parksey/src/main/button/WinButton.java new file mode 100644 index 0000000..4960db7 --- /dev/null +++ b/chapter4/parksey/src/main/button/WinButton.java @@ -0,0 +1,8 @@ +package main.button; + +public class WinButton implements Button{ + @Override + public void onClick() { + System.out.println("윈도우 버튼"); + } +} diff --git a/chapter4/parksey/src/main/checkbox/Checkbox.java b/chapter4/parksey/src/main/checkbox/Checkbox.java new file mode 100644 index 0000000..66a20ca --- /dev/null +++ b/chapter4/parksey/src/main/checkbox/Checkbox.java @@ -0,0 +1,5 @@ +package main.checkbox; + +public interface Checkbox { + void onClick(); +} diff --git a/chapter4/parksey/src/main/checkbox/MacCheckbox.java b/chapter4/parksey/src/main/checkbox/MacCheckbox.java new file mode 100644 index 0000000..f6651f0 --- /dev/null +++ b/chapter4/parksey/src/main/checkbox/MacCheckbox.java @@ -0,0 +1,7 @@ +package main.checkbox; +public class MacCheckbox implements Checkbox{ + @Override + public void onClick() { + System.out.println("Mac 체크박스 클릭"); + } +} diff --git a/chapter4/parksey/src/main/checkbox/WinCheckbox.java b/chapter4/parksey/src/main/checkbox/WinCheckbox.java new file mode 100644 index 0000000..3444d3c --- /dev/null +++ b/chapter4/parksey/src/main/checkbox/WinCheckbox.java @@ -0,0 +1,8 @@ +package main.checkbox; + +public class WinCheckbox implements Checkbox{ + @Override + public void onClick() { + System.out.println("Win 체크박스 클릭"); + } +} diff --git a/chapter4/parksey/src/main/gui/GUIFactory.java b/chapter4/parksey/src/main/gui/GUIFactory.java new file mode 100644 index 0000000..95e4d41 --- /dev/null +++ b/chapter4/parksey/src/main/gui/GUIFactory.java @@ -0,0 +1,22 @@ +package main.gui; + +import main.button.Button; +import main.checkbox.Checkbox; + +public abstract class GUIFactory { + private static GUIFactory guiFactory; + + public static GUIFactory getInstance(String os) { + if ("Win".equals(os)) { + return WinFactory.getInstance(); + } + + if ("Mac".equals(os)) { + return MacFactory.getInstance(); + } + throw new RuntimeException("해당 운영체제 없다."); + } + + public abstract Button createButton(); + public abstract Checkbox createCheckbox(); +} diff --git a/chapter4/parksey/src/main/gui/MacFactory.java b/chapter4/parksey/src/main/gui/MacFactory.java new file mode 100644 index 0000000..9bd964f --- /dev/null +++ b/chapter4/parksey/src/main/gui/MacFactory.java @@ -0,0 +1,28 @@ +package main.gui; + +import main.button.Button; +import main.button.MacButton; +import main.checkbox.Checkbox; +import main.checkbox.MacCheckbox; + +public class MacFactory extends GUIFactory{ + private static MacFactory macFactory; + private MacFactory() {} + public static MacFactory getInstance() { + if (MacFactory.macFactory == null) { + MacFactory.macFactory = new MacFactory(); + } + + return MacFactory.macFactory; + } + + @Override + public Button createButton() { + return new MacButton(); + } + + @Override + public Checkbox createCheckbox() { + return new MacCheckbox(); + } +} diff --git a/chapter4/parksey/src/main/gui/WinFactory.java b/chapter4/parksey/src/main/gui/WinFactory.java new file mode 100644 index 0000000..fd165c5 --- /dev/null +++ b/chapter4/parksey/src/main/gui/WinFactory.java @@ -0,0 +1,31 @@ +package main.gui; + + +import main.button.Button; +import main.button.WinButton; +import main.checkbox.Checkbox; +import main.checkbox.WinCheckbox; + +public class WinFactory extends GUIFactory { + private static WinFactory winFactory; + + private WinFactory() {} + public static WinFactory getInstance() { + if (WinFactory.winFactory == null) { + WinFactory.winFactory = new WinFactory(); + } + + return WinFactory.winFactory; + } + + + @Override + public Button createButton() { + return new WinButton(); + } + + @Override + public Checkbox createCheckbox() { + return new WinCheckbox(); + } +} diff --git a/chapter6/parksey/.gitignore b/chapter6/parksey/.gitignore new file mode 100644 index 0000000..30859b7 --- /dev/null +++ b/chapter6/parksey/.gitignore @@ -0,0 +1,172 @@ +# Created by https://www.toptal.com/developers/gitignore/api/java,intellij,gradle +# Edit at https://www.toptal.com/developers/gitignore?templates=java,intellij,gradle + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +# Azure Toolkit for IntelliJ plugin +# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij +.idea/**/azureSettings.xml + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* + +### Gradle ### +.gradle +**/build/ +!src/**/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Avoid ignore Gradle wrappper properties +!gradle-wrapper.properties + +# Cache of project +.gradletasknamecache + +# Eclipse Gradle plugin generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +### Gradle Patch ### +# Java heap dump +*.hprof + +# End of https://www.toptal.com/developers/gitignore/api/java,intellij,gradle + + diff --git a/chapter6/parksey/parksey.iml b/chapter6/parksey/parksey.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/chapter6/parksey/parksey.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/chapter6/parksey/src/commandpattern/Application.java b/chapter6/parksey/src/commandpattern/Application.java new file mode 100644 index 0000000..438ddab --- /dev/null +++ b/chapter6/parksey/src/commandpattern/Application.java @@ -0,0 +1,78 @@ +package commandpattern; + +import commandpattern.command.*; +import commandpattern.invoker.Buttons; +import commandpattern.invoker.Shortcuts; +import commandpattern.util.CommandName; + +import java.util.ArrayList; +import java.util.List; + +public class Application { + String clipboard; + List editors; + Editor activateEditor; + CommandHistory history; + + Buttons buttons; + Shortcuts shortcuts; + + + public Application() { + buttons = new Buttons(); + this.editors = new ArrayList<>(); + this.history = new CommandHistory(); + } + + public void createUi() { + Command copy = new CopyCommand(this, activateEditor); + Command undo = new UndoCommand(this, activateEditor); + Command cut = new CutCommand(this, activateEditor); + Command paste = new PasteCommand(this, activateEditor); + + buttons.createUi(CommandName.COPY, copy); + buttons.createUi(CommandName.UNDO, undo); + buttons.createUi(CommandName.CUT, cut); + buttons.createUi(CommandName.PASTE, paste); + shortcuts.createUi(CommandName.COPY, copy); + shortcuts.createUi(CommandName.UNDO, undo); + shortcuts.createUi(CommandName.CUT, cut); + shortcuts.createUi(CommandName.PASTE, paste); + } + + public Command executeCommand(String command) { + CommandName userCommand = CommandName.getButtonCommand(command); + if (userCommand != null) { + return buttons.execute(userCommand); + } + + userCommand = CommandName.getShortcuts(command); + if (userCommand != null) { + return shortcuts.execute(userCommand); + } + return null; + } + + public void execute(String command) { + Command commandOperator = executeCommand(command); + if (commandOperator != null) { + history.push(commandOperator); + } + } + + + public void undo() { + Command command = history.pop(); + if (command != null) { + command.undo(); + } + } + + public void setClipboard(String newText) { + this.clipboard = newText; + } + + public String getClipboard() { + return clipboard; + } +} diff --git a/chapter6/parksey/src/commandpattern/CommandHistory.java b/chapter6/parksey/src/commandpattern/CommandHistory.java new file mode 100644 index 0000000..96cd672 --- /dev/null +++ b/chapter6/parksey/src/commandpattern/CommandHistory.java @@ -0,0 +1,18 @@ +package commandpattern; + +import commandpattern.command.Command; + +import java.util.List; + +public class CommandHistory { + List history; + + public void push(Command command) { + history.add(command); + } + + public Command pop() { + return history.remove(history.size()-1); + } + +} diff --git a/chapter6/parksey/src/commandpattern/Editor.java b/chapter6/parksey/src/commandpattern/Editor.java new file mode 100644 index 0000000..b2ae4ff --- /dev/null +++ b/chapter6/parksey/src/commandpattern/Editor.java @@ -0,0 +1,25 @@ +package commandpattern; + +public class Editor { + private String text; + + public Editor(String text) { + this.text = text; + } + + public String getSelection() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public void deleteSelction() { + this.text = ""; + } + + public void replaceSelection(String text) { + this.text = text; + } +} diff --git a/chapter6/parksey/src/commandpattern/command/Command.java b/chapter6/parksey/src/commandpattern/command/Command.java new file mode 100644 index 0000000..bdcb55e --- /dev/null +++ b/chapter6/parksey/src/commandpattern/command/Command.java @@ -0,0 +1,27 @@ +package commandpattern.command; + + +import commandpattern.Application; +import commandpattern.Editor; + +public abstract class Command { + protected Application application; + protected Editor editor; + + private String backup; + + public Command(Application application, Editor editor) { + this.application = application; + this.editor = editor; + } + + public void saveBackup() { + this.backup = this.editor.getSelection(); + } + + public void undo() { + this.editor.setText(this.backup); + } + + abstract public void execute(); +} diff --git a/chapter6/parksey/src/commandpattern/command/CopyCommand.java b/chapter6/parksey/src/commandpattern/command/CopyCommand.java new file mode 100644 index 0000000..09524e7 --- /dev/null +++ b/chapter6/parksey/src/commandpattern/command/CopyCommand.java @@ -0,0 +1,14 @@ +package commandpattern.command; + +import commandpattern.Application; +import commandpattern.Editor; + +public class CopyCommand extends Command { + public CopyCommand(Application application, Editor editor) { + super(application, editor); + } + @Override + public void execute() { + this.application.setClipboard(editor.getSelection()); + } +} diff --git a/chapter6/parksey/src/commandpattern/command/CutCommand.java b/chapter6/parksey/src/commandpattern/command/CutCommand.java new file mode 100644 index 0000000..e5c2fee --- /dev/null +++ b/chapter6/parksey/src/commandpattern/command/CutCommand.java @@ -0,0 +1,19 @@ +package commandpattern.command; + + +import commandpattern.Application; +import commandpattern.Editor; + +public class CutCommand extends Command{ + public CutCommand(Application application, Editor editor) { + super(application, editor); + } + + @Override + public void execute() { + saveBackup(); + + this.application.setClipboard(this.editor.getSelection()); + this.editor.deleteSelction(); + } +} diff --git a/chapter6/parksey/src/commandpattern/command/PasteCommand.java b/chapter6/parksey/src/commandpattern/command/PasteCommand.java new file mode 100644 index 0000000..41ace2a --- /dev/null +++ b/chapter6/parksey/src/commandpattern/command/PasteCommand.java @@ -0,0 +1,16 @@ +package commandpattern.command; + +import commandpattern.Application; +import commandpattern.Editor; + +public class PasteCommand extends Command{ + public PasteCommand(Application application, Editor editor) { + super(application, editor); + } + + @Override + public void execute() { + saveBackup(); + this.editor.replaceSelection(application.getClipboard()); + } +} diff --git a/chapter6/parksey/src/commandpattern/command/UndoCommand.java b/chapter6/parksey/src/commandpattern/command/UndoCommand.java new file mode 100644 index 0000000..4db4bac --- /dev/null +++ b/chapter6/parksey/src/commandpattern/command/UndoCommand.java @@ -0,0 +1,16 @@ +package commandpattern.command; + + +import commandpattern.Application; +import commandpattern.Editor; + +public class UndoCommand extends Command{ + public UndoCommand(Application application, Editor editor) { + super(application, editor); + } + + @Override + public void execute() { + this.application.undo(); + } +} diff --git a/chapter6/parksey/src/commandpattern/invoker/Button.java b/chapter6/parksey/src/commandpattern/invoker/Button.java new file mode 100644 index 0000000..7a6b47a --- /dev/null +++ b/chapter6/parksey/src/commandpattern/invoker/Button.java @@ -0,0 +1,25 @@ +package commandpattern.invoker; + + +import commandpattern.command.Command; + +public class Button { + + private Command command; + + public Button(Command command) { + this.command = command; + } + + public void setCommand(Command command) { + this.command = command; + } + + public Command getCommand() { + return command; + } + + public void execute() { + command.execute(); + } +} diff --git a/chapter6/parksey/src/commandpattern/invoker/Buttons.java b/chapter6/parksey/src/commandpattern/invoker/Buttons.java new file mode 100644 index 0000000..f5921d6 --- /dev/null +++ b/chapter6/parksey/src/commandpattern/invoker/Buttons.java @@ -0,0 +1,25 @@ +package commandpattern.invoker; + +import commandpattern.command.Command; +import commandpattern.util.CommandName; + +import java.util.HashMap; +import java.util.Map; + +public class Buttons { + private Map buttons; + + public Buttons() { + buttons = new HashMap<>(); + } + + public void createUi(CommandName name, Command command) { + buttons.put(name, new Button(command)); + } + + public Command execute(CommandName name) { + Button button = buttons.get(name); + button.execute(); + return button.getCommand(); + } +} diff --git a/chapter6/parksey/src/commandpattern/invoker/Shortcut.java b/chapter6/parksey/src/commandpattern/invoker/Shortcut.java new file mode 100644 index 0000000..3908d56 --- /dev/null +++ b/chapter6/parksey/src/commandpattern/invoker/Shortcut.java @@ -0,0 +1,23 @@ +package commandpattern.invoker; + +import commandpattern.command.Command; + +public class Shortcut { + private Command command; + + public Shortcut(Command command) { + this.command = command; + } + + public void setCommand(Command command) { + this.command = command; + } + + public Command getCommand() { + return command; + } + + public void execute() { + command.execute(); + } +} diff --git a/chapter6/parksey/src/commandpattern/invoker/Shortcuts.java b/chapter6/parksey/src/commandpattern/invoker/Shortcuts.java new file mode 100644 index 0000000..ae691fc --- /dev/null +++ b/chapter6/parksey/src/commandpattern/invoker/Shortcuts.java @@ -0,0 +1,25 @@ +package commandpattern.invoker; + +import commandpattern.command.Command; +import commandpattern.util.CommandName; + +import java.util.HashMap; +import java.util.Map; + +public class Shortcuts { + private Map shortcuts; + + public Shortcuts() { + shortcuts = new HashMap<>(); + } + + public void createUi(CommandName name, Command command) { + shortcuts.put(name, new Shortcut(command)); + } + + public Command execute(CommandName name) { + Shortcut shortcut = shortcuts.get(name); + shortcut.execute(); + return shortcut.getCommand(); + } +} diff --git a/chapter6/parksey/src/commandpattern/util/CommandName.java b/chapter6/parksey/src/commandpattern/util/CommandName.java new file mode 100644 index 0000000..74f936f --- /dev/null +++ b/chapter6/parksey/src/commandpattern/util/CommandName.java @@ -0,0 +1,37 @@ +package commandpattern.util; + +import java.util.HashMap; +import java.util.Map; + +public enum CommandName { + COPY("copy", "ctrl+c"), + CUT("cut", "ctrl+x"), + PASTE("copy", "ctrl+v"), + UNDO("undo", "ctrl+z"); + + private final String buttonCommand; + private final String shortcutCommand; + + private static final Map buttons = new HashMap<>(); + private static final Map shortcuts = new HashMap<>(); + + static { + for (CommandName commandName : CommandName.values()) { + buttons.put(commandName.buttonCommand, commandName); + shortcuts.put(commandName.shortcutCommand, commandName); + } + } + + CommandName(String buttonCommand, String shortcutCommand) { + this.buttonCommand = buttonCommand; + this.shortcutCommand = shortcutCommand; + } + + public static CommandName getButtonCommand(String command) { + return buttons.getOrDefault(command, null); + } + + public static CommandName getShortcuts(String command) { + return shortcuts.getOrDefault(command, null); + } +} diff --git a/chapter7/parksey/.gitignore b/chapter7/parksey/.gitignore new file mode 100644 index 0000000..73281d9 --- /dev/null +++ b/chapter7/parksey/.gitignore @@ -0,0 +1,170 @@ +# Created by https://www.toptal.com/developers/gitignore/api/java,intellij,gradle +# Edit at https://www.toptal.com/developers/gitignore?templates=java,intellij,gradle + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +# Azure Toolkit for IntelliJ plugin +# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij +.idea/**/azureSettings.xml + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* + +### Gradle ### +.gradle +**/build/ +!src/**/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Avoid ignore Gradle wrappper properties +!gradle-wrapper.properties + +# Cache of project +.gradletasknamecache + +# Eclipse Gradle plugin generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +### Gradle Patch ### +# Java heap dump +*.hprof + +# End of https://www.toptal.com/developers/gitignore/api/java,intellij,gradle \ No newline at end of file diff --git a/chapter7/parksey/parksey.iml b/chapter7/parksey/parksey.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/chapter7/parksey/parksey.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/chapter7/parksey/src/Main.java b/chapter7/parksey/src/Main.java new file mode 100644 index 0000000..3e59c38 --- /dev/null +++ b/chapter7/parksey/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/chapter7/parksey/src/adapterpattern/AdapterTest.java b/chapter7/parksey/src/adapterpattern/AdapterTest.java new file mode 100644 index 0000000..eabf2af --- /dev/null +++ b/chapter7/parksey/src/adapterpattern/AdapterTest.java @@ -0,0 +1,15 @@ +package adapterpattern; + +public class AdapterTest { + public static void main(String[] args) { + RoundHole hole = new RoundHole(10); + + System.out.println(hole.fits(new RoundPeg(9))); + System.out.println(hole.fits(new RoundPeg(10))); + System.out.println(hole.fits(new RoundPeg(11))); + + System.out.println(hole.fits(new SquarePegAdapter(6))); + System.out.println(hole.fits(new SquarePegAdapter(7))); + System.out.println(hole.fits(new SquarePegAdapter(8))); + } +} diff --git a/chapter7/parksey/src/adapterpattern/RoundHole.java b/chapter7/parksey/src/adapterpattern/RoundHole.java new file mode 100644 index 0000000..593ce59 --- /dev/null +++ b/chapter7/parksey/src/adapterpattern/RoundHole.java @@ -0,0 +1,19 @@ +package adapterpattern; + +public class RoundHole { + private int radius; + + public RoundHole(int radius) { + this.radius = radius; + } + + private int getRadius() { + return radius*radius; + } + + public boolean fits(RoundPeg peg) { + return getRadius() >= peg.getRadius(); + } + + +} diff --git a/chapter7/parksey/src/adapterpattern/RoundPeg.java b/chapter7/parksey/src/adapterpattern/RoundPeg.java new file mode 100644 index 0000000..9f7c73b --- /dev/null +++ b/chapter7/parksey/src/adapterpattern/RoundPeg.java @@ -0,0 +1,13 @@ +package adapterpattern; + +public class RoundPeg { + protected int radius; + + public RoundPeg(int redius) { + this.radius = redius; + } + + public int getRadius() { + return radius * radius; + } +} diff --git a/chapter7/parksey/src/adapterpattern/SquarePegAdapter.java b/chapter7/parksey/src/adapterpattern/SquarePegAdapter.java new file mode 100644 index 0000000..144b678 --- /dev/null +++ b/chapter7/parksey/src/adapterpattern/SquarePegAdapter.java @@ -0,0 +1,12 @@ +package adapterpattern; + +public class SquarePegAdapter extends RoundPeg{ + public SquarePegAdapter(int redius) { + super(redius); + } + + + public int getRadius() { + return this.radius * this.radius * 2; + } +} diff --git a/chapter7/parksey/src/adapterpattern/SquareReg.java b/chapter7/parksey/src/adapterpattern/SquareReg.java new file mode 100644 index 0000000..92d13a7 --- /dev/null +++ b/chapter7/parksey/src/adapterpattern/SquareReg.java @@ -0,0 +1,13 @@ +package adapterpattern; + +public class SquareReg { + private int width; + + public SquareReg(int width) { + this.width = width; + } + + public int getWidth() { + return width; + } +} diff --git a/chapter7/parksey/src/facadepattern/Application.java b/chapter7/parksey/src/facadepattern/Application.java new file mode 100644 index 0000000..3f72762 --- /dev/null +++ b/chapter7/parksey/src/facadepattern/Application.java @@ -0,0 +1,12 @@ +package facadepattern; + +import facadepattern.converter.File; +import facadepattern.converter.VideoConverter; + +public class Application { + public static void main(String[] args) { + VideoConverter videoConverter = new VideoConverter(); + File mp4 = videoConverter.convertVideo("funny-cats-video.ogg", "mp4"); + mp4.save(); + } +} diff --git a/chapter7/parksey/src/facadepattern/codec/BaseCodec.java b/chapter7/parksey/src/facadepattern/codec/BaseCodec.java new file mode 100644 index 0000000..efdcd6e --- /dev/null +++ b/chapter7/parksey/src/facadepattern/codec/BaseCodec.java @@ -0,0 +1,6 @@ +package facadepattern.codec; + +import facadepattern.codec.Codec; + +public class BaseCodec implements Codec { +} diff --git a/chapter7/parksey/src/facadepattern/codec/Codec.java b/chapter7/parksey/src/facadepattern/codec/Codec.java new file mode 100644 index 0000000..9c8ea4c --- /dev/null +++ b/chapter7/parksey/src/facadepattern/codec/Codec.java @@ -0,0 +1,4 @@ +package facadepattern.codec; + +public interface Codec { +} diff --git a/chapter7/parksey/src/facadepattern/codec/CodecFactory.java b/chapter7/parksey/src/facadepattern/codec/CodecFactory.java new file mode 100644 index 0000000..c0da2cd --- /dev/null +++ b/chapter7/parksey/src/facadepattern/codec/CodecFactory.java @@ -0,0 +1,10 @@ +package facadepattern.codec; + +import facadepattern.converter.File; + +public class CodecFactory { + + public Codec extract(File file) { + return new BaseCodec(); + } +} diff --git a/chapter7/parksey/src/facadepattern/codec/MPEG4CompressionCodec.java b/chapter7/parksey/src/facadepattern/codec/MPEG4CompressionCodec.java new file mode 100644 index 0000000..413e3fe --- /dev/null +++ b/chapter7/parksey/src/facadepattern/codec/MPEG4CompressionCodec.java @@ -0,0 +1,6 @@ +package facadepattern.codec; + +import facadepattern.codec.Codec; + +public class MPEG4CompressionCodec implements Codec { +} diff --git a/chapter7/parksey/src/facadepattern/codec/OggCompressionCodec.java b/chapter7/parksey/src/facadepattern/codec/OggCompressionCodec.java new file mode 100644 index 0000000..7cd0193 --- /dev/null +++ b/chapter7/parksey/src/facadepattern/codec/OggCompressionCodec.java @@ -0,0 +1,6 @@ +package facadepattern.codec; + +import facadepattern.codec.Codec; + +public class OggCompressionCodec implements Codec { +} diff --git a/chapter7/parksey/src/facadepattern/converter/AudioMixer.java b/chapter7/parksey/src/facadepattern/converter/AudioMixer.java new file mode 100644 index 0000000..d6f1a24 --- /dev/null +++ b/chapter7/parksey/src/facadepattern/converter/AudioMixer.java @@ -0,0 +1,7 @@ +package facadepattern.converter; + +public class AudioMixer { + public byte[] fix(byte[] buffer) { + return buffer; + } +} diff --git a/chapter7/parksey/src/facadepattern/converter/BitrateReader.java b/chapter7/parksey/src/facadepattern/converter/BitrateReader.java new file mode 100644 index 0000000..a4472fa --- /dev/null +++ b/chapter7/parksey/src/facadepattern/converter/BitrateReader.java @@ -0,0 +1,14 @@ +package facadepattern.converter; + +import facadepattern.codec.Codec; + +public class BitrateReader { + + public byte[] read(String fileName, Codec sourceCodec) { + return null; + } + + public byte[] convert(byte[] buffer, Codec destiationCodec) { + return null; + } +} diff --git a/chapter7/parksey/src/facadepattern/converter/File.java b/chapter7/parksey/src/facadepattern/converter/File.java new file mode 100644 index 0000000..3ca34d8 --- /dev/null +++ b/chapter7/parksey/src/facadepattern/converter/File.java @@ -0,0 +1,17 @@ +package facadepattern.converter; + +public class File { + protected String fileName; + + public File(String fileName) { + this.fileName = fileName; + } + + public File(byte[] buffer) { + + } + + public void save(){ + + } +} diff --git a/chapter7/parksey/src/facadepattern/converter/VideoConverter.java b/chapter7/parksey/src/facadepattern/converter/VideoConverter.java new file mode 100644 index 0000000..d242a29 --- /dev/null +++ b/chapter7/parksey/src/facadepattern/converter/VideoConverter.java @@ -0,0 +1,29 @@ +package facadepattern.converter; + +import facadepattern.codec.Codec; +import facadepattern.codec.CodecFactory; +import facadepattern.codec.MPEG4CompressionCodec; +import facadepattern.codec.OggCompressionCodec; + +public class VideoConverter { + + public File convertVideo(String filename, String format) { + File file = new VideoFile(filename); + Codec sourceCodec = new CodecFactory().extract(file); + Codec destinationCodec = findCodecByForamt(format); + + BitrateReader bitrateReader =new BitrateReader(); + byte[] buffer = bitrateReader.read(filename, sourceCodec); + byte[] result = bitrateReader.convert(buffer, destinationCodec); + + return new File(new AudioMixer().fix(result)); + } + + public Codec findCodecByForamt(String format) { + if (format.equals("mp4")) { + return new MPEG4CompressionCodec(); + } + + return new OggCompressionCodec(); + } +} diff --git a/chapter7/parksey/src/facadepattern/converter/VideoFile.java b/chapter7/parksey/src/facadepattern/converter/VideoFile.java new file mode 100644 index 0000000..732acb5 --- /dev/null +++ b/chapter7/parksey/src/facadepattern/converter/VideoFile.java @@ -0,0 +1,8 @@ +package facadepattern.converter; + +public class VideoFile extends File{ + private String fileName; + public VideoFile(String fileName) { + super(fileName); + } +} diff --git a/chapter8/parksey/.gitignore b/chapter8/parksey/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/chapter8/parksey/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/chapter8/parksey/src/Main.java b/chapter8/parksey/src/Main.java new file mode 100644 index 0000000..3e59c38 --- /dev/null +++ b/chapter8/parksey/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/chapter8/parksey/src/ai/GameAI.java b/chapter8/parksey/src/ai/GameAI.java new file mode 100644 index 0000000..d647104 --- /dev/null +++ b/chapter8/parksey/src/ai/GameAI.java @@ -0,0 +1,88 @@ +package ai; + +import ai.resource.ResourceAI; +import global.Jobs; +import global.Position; +import global.resources.Resources; + +/** + * GameAI가 자동으로 동작할 수 있도록 하는 메서드 + * - Monster : 집 짓기 불가, 선제공격 불가 + * - Human : 집 짓기 가능, 방어적으로 추가 기능 가능, 선제공격 선택 + * - Orcs: : 집 짓기 가능, 공격적으로 가능, 무조건 선제공격 + */ +public abstract class GameAI { + private static final double BASIC_BOUNDARY = 10.0; + private static final int LEVEL_UP = 10; + + protected int hp = 100; + private int damage = 10; + protected Position position; + protected Jobs jobs = Jobs.FARMER; + + /** + * 각 턴 마다 동작 + */ + public void takeTurn(GameAI gameAI, Resources resources) { + attack(gameAI); + collectResources(resources); + + if (haveToBuild()) { + buildStructures(); + } + buildUnits(); + grow(); + + if (canMove()) { + + } + } + + public void attack(GameAI gameAI) { + if (nearByEnemies(gameAI.position)) { + return; + } + + if (!firstAttack()) { + return; + } + + damageProcess(gameAI); + } + + public void grow() { + hp+=LEVEL_UP; + } + + + public boolean haveToBuild() { + return false; + } + + public void buildUnits() { + System.out.println("동족 생성"); + } + + + public boolean canMove() { + return false; + } + + protected boolean nearByEnemies(Position enemyPosition) { + double distance = position.calcDistanceTo(enemyPosition); + return distance < BASIC_BOUNDARY; + } + + protected boolean firstAttack() { + return false; + } + + protected void damageProcess(GameAI gameAI) { + gameAI.hp = damage; + } + + abstract public void collectResources(Resources resources); + abstract public void buildStructures(); + abstract public void movePosition(Position position); +} + diff --git a/chapter8/parksey/src/ai/active/ActiveAI.java b/chapter8/parksey/src/ai/active/ActiveAI.java new file mode 100644 index 0000000..504cda9 --- /dev/null +++ b/chapter8/parksey/src/ai/active/ActiveAI.java @@ -0,0 +1,27 @@ +package ai.active; + +import ai.GameAI; +import global.Position; +import global.resources.Resources; + +/** + * movePosition은 현재 동일한 기능이나, 후에 동료 및 혼자 다닐때, 주위 환경에 따라 동작의 상세가 변경될 수 있으므로 추가 + */ +public abstract class ActiveAI extends GameAI { + + @Override + public void collectResources(Resources resources) { + hp += resources.takeHp(); + } + + @Override + public boolean haveToBuild() { + return true; + } + + @Override + public boolean canMove() { + return true; + } + +} diff --git a/chapter8/parksey/src/ai/active/HumanAI.java b/chapter8/parksey/src/ai/active/HumanAI.java new file mode 100644 index 0000000..06267da --- /dev/null +++ b/chapter8/parksey/src/ai/active/HumanAI.java @@ -0,0 +1,28 @@ +package ai.active; + +import ai.GameAI; +import global.Jobs; +import global.Position; + +public class HumanAI extends ActiveAI { + private static final double SPEED = 1; + + @Override + protected boolean firstAttack() { + if (jobs.equals(Jobs.FARMER)) { + return false; + } + return true; + } + + + @Override + public void buildStructures() { + System.out.println("마을 생성"); + } + + @Override + public void movePosition(Position position) { + this.position.moveTo(position, SPEED); + } +} diff --git a/chapter8/parksey/src/ai/active/MonstersAI.java b/chapter8/parksey/src/ai/active/MonstersAI.java new file mode 100644 index 0000000..75dc5de --- /dev/null +++ b/chapter8/parksey/src/ai/active/MonstersAI.java @@ -0,0 +1,18 @@ +package ai.active; + +import ai.GameAI; +import global.Position; + +public class MonstersAI extends ActiveAI { + private static final double SPEED = 2; + + @Override + public void buildStructures() { + System.out.println("둥지 생성"); + } + + @Override + public void movePosition(Position position) { + this.position.moveTo(position, SPEED); + } +} diff --git a/chapter8/parksey/src/ai/active/OrcsAI.java b/chapter8/parksey/src/ai/active/OrcsAI.java new file mode 100644 index 0000000..741b930 --- /dev/null +++ b/chapter8/parksey/src/ai/active/OrcsAI.java @@ -0,0 +1,23 @@ +package ai.active; + +import ai.GameAI; +import global.Position; + +public class OrcsAI extends ActiveAI { + private static final double SPEED = 0.5; + + @Override + protected boolean firstAttack() { + return true; + } + + @Override + public void buildStructures() { + System.out.println("동굴 생성"); + } + + @Override + public void movePosition(Position position) { + this.position.moveTo(position, SPEED); + } +} diff --git a/chapter8/parksey/src/ai/resource/ResourceAI.java b/chapter8/parksey/src/ai/resource/ResourceAI.java new file mode 100644 index 0000000..4b12549 --- /dev/null +++ b/chapter8/parksey/src/ai/resource/ResourceAI.java @@ -0,0 +1,37 @@ +package ai.resource; + +import ai.GameAI; +import global.Position; +import global.resources.Resources; +import global.resources.TakeResources; + +public class ResourceAI extends GameAI { + @Override + public void attack(GameAI gameAI) { + + } + + @Override + public void collectResources(Resources resources) { + if (!(resources instanceof TakeResources)) { + return; + } + + addResource(resources); + } + + @Override + public void buildStructures() { + throw new RuntimeException("건물을 생성할 수 없습니다."); + } + + @Override + public void movePosition(Position position) { + throw new RuntimeException("움직일 수 없습니다."); + } + + + protected void addResource(Resources resources) { + hp += resources.takeHp(); + } +} diff --git a/chapter8/parksey/src/ai/resource/TreeAI.java b/chapter8/parksey/src/ai/resource/TreeAI.java new file mode 100644 index 0000000..c2fcd0f --- /dev/null +++ b/chapter8/parksey/src/ai/resource/TreeAI.java @@ -0,0 +1,8 @@ +package ai.resource; + +import global.resources.Resources; +import global.resources.TakeResources; + +public class TreeAI extends ResourceAI{ + +} diff --git a/chapter8/parksey/src/ai/resource/WaterAI.java b/chapter8/parksey/src/ai/resource/WaterAI.java new file mode 100644 index 0000000..1359d40 --- /dev/null +++ b/chapter8/parksey/src/ai/resource/WaterAI.java @@ -0,0 +1,6 @@ +package ai.resource; + +import global.resources.Resources; + +public class WaterAI extends ResourceAI{ +} diff --git a/chapter8/parksey/src/global/Jobs.java b/chapter8/parksey/src/global/Jobs.java new file mode 100644 index 0000000..51dd36e --- /dev/null +++ b/chapter8/parksey/src/global/Jobs.java @@ -0,0 +1,8 @@ +package global; + +public enum Jobs { + FARMER, + SOLDIE, + NIGHT, + HUNTER; +} diff --git a/chapter8/parksey/src/global/Position.java b/chapter8/parksey/src/global/Position.java new file mode 100644 index 0000000..5f25cee --- /dev/null +++ b/chapter8/parksey/src/global/Position.java @@ -0,0 +1,20 @@ +package global; + +public class Position { + private int x; + private int y; + private int z; + + public double calcDistanceTo(Position position) { + int distanceX = x - position.x; + int distanceY = y - position.y; + int distanceZ = z - position.z; + return Math.sqrt(distanceX*distanceX + distanceY*distanceY + distanceZ + distanceZ); + } + + public void moveTo(Position position, double speed) { + x += position.x * speed; + y += position.y * speed; + z += position.z * speed; + } +} diff --git a/chapter8/parksey/src/global/resources/ActiveResources.java b/chapter8/parksey/src/global/resources/ActiveResources.java new file mode 100644 index 0000000..a425ecb --- /dev/null +++ b/chapter8/parksey/src/global/resources/ActiveResources.java @@ -0,0 +1,33 @@ +package global.resources; + +import java.util.Arrays; +import java.util.EnumMap; +import java.util.stream.Collectors; + +public enum ActiveResources implements Resources{ + TREE(-3), + ROCK(-10), + ICON(-20); + + + private final int takeHp; + + private static final EnumMap resources = new EnumMap<>(ActiveResources.class); + + static { + Arrays.stream(ActiveResources.values()) + .collect(Collectors + .toMap(activeResources -> activeResources, activeResources -> activeResources.takeHp + )); + } + + + ActiveResources(int takeHp) { + this.takeHp = takeHp; + } + + @Override + public int takeHp() { + return takeHp; + } +} diff --git a/chapter8/parksey/src/global/resources/Resources.java b/chapter8/parksey/src/global/resources/Resources.java new file mode 100644 index 0000000..34157fe --- /dev/null +++ b/chapter8/parksey/src/global/resources/Resources.java @@ -0,0 +1,5 @@ +package global.resources; + +public interface Resources { + int takeHp(); +} diff --git a/chapter8/parksey/src/global/resources/TakeResources.java b/chapter8/parksey/src/global/resources/TakeResources.java new file mode 100644 index 0000000..1629999 --- /dev/null +++ b/chapter8/parksey/src/global/resources/TakeResources.java @@ -0,0 +1,33 @@ +package global.resources; + +import java.util.Arrays; +import java.util.EnumMap; +import java.util.stream.Collectors; + +public enum TakeResources implements Resources{ + WATER(1), + RABIT(5), + PIG(10), + COW(20); + + + private final int takeHp; + + private static final EnumMap resources = new EnumMap<>(TakeResources.class); + + static { + Arrays.stream(TakeResources.values()) + .collect(Collectors + .toMap(takeResources -> takeResources, takeResources -> takeResources.takeHp + )); + } + + TakeResources(int takeHp) { + this.takeHp = takeHp; + } + + @Override + public int takeHp() { + return takeHp; + } +} diff --git a/chapter9/parksey/.gitignore b/chapter9/parksey/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/chapter9/parksey/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/chapter9/parksey/src/Main.java b/chapter9/parksey/src/Main.java new file mode 100644 index 0000000..3e59c38 --- /dev/null +++ b/chapter9/parksey/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/chapter9/parksey/src/compositePattern/Circle.java b/chapter9/parksey/src/compositePattern/Circle.java new file mode 100644 index 0000000..e4feed7 --- /dev/null +++ b/chapter9/parksey/src/compositePattern/Circle.java @@ -0,0 +1,13 @@ +package compositePattern; + +public class Circle extends Dot{ + + public Circle(int x, int y) { + super(x, y); + } + + @Override + public void draw() { + System.out.println(String.format("Circle: {} {}", x,y)); + } +} diff --git a/chapter9/parksey/src/compositePattern/CompoundGraphic.java b/chapter9/parksey/src/compositePattern/CompoundGraphic.java new file mode 100644 index 0000000..ef7d947 --- /dev/null +++ b/chapter9/parksey/src/compositePattern/CompoundGraphic.java @@ -0,0 +1,28 @@ +package compositePattern; + +import java.util.ArrayList; +import java.util.List; + +public class CompoundGraphic implements Graphic{ + List graphics = new ArrayList<>(); + + public void add(Graphic graphic) { + graphics.add(graphic); + } + + public void remove(Graphic graphic) { + graphics.remove(graphic); + } + + @Override + public void move(int x, int y) { + graphics.stream() + .forEach(graphic -> graphic.move(x,y)); + } + + @Override + public void draw() { + graphics.stream() + .forEach(graphic -> graphic.draw()); + } +} diff --git a/chapter9/parksey/src/compositePattern/Dot.java b/chapter9/parksey/src/compositePattern/Dot.java new file mode 100644 index 0000000..015e6e9 --- /dev/null +++ b/chapter9/parksey/src/compositePattern/Dot.java @@ -0,0 +1,21 @@ +package compositePattern; + +public class Dot implements Graphic{ + protected int x; + protected int y; + public Dot(int x,int y) { + this.x = x; + this.y = y; + } + + @Override + public void move(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public void draw() { + System.out.println(String.format("Draw: {} {}",x,y)); + } +} diff --git a/chapter9/parksey/src/compositePattern/Graphic.java b/chapter9/parksey/src/compositePattern/Graphic.java new file mode 100644 index 0000000..18ddbce --- /dev/null +++ b/chapter9/parksey/src/compositePattern/Graphic.java @@ -0,0 +1,6 @@ +package compositePattern; + +public interface Graphic { + void move(int x, int y); + void draw(); +} diff --git a/chapter9/parksey/src/compositepatternCustom/Box.java b/chapter9/parksey/src/compositepatternCustom/Box.java new file mode 100644 index 0000000..66ef6ca --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/Box.java @@ -0,0 +1,36 @@ +package compositepatternCustom; + +import compositepatternCustom.electric.Monitor; +import compositepatternCustom.phonepackage.PhonePackage; + +import java.util.ArrayList; +import java.util.List; + +public class Box implements Component { + private List components = new ArrayList<>(); + private Recipe recipe; + + public Box() { + components.add(new PhonePackage()); + components.add(new Monitor()); + recipe = new Recipe(); + } + + + @Override + public void printInfo() { + components.stream().forEach(Component::printInfo); + } + + @Override + public int getPrice() { + initrecipe(); + return recipe.getPrice(); + } + + public void initrecipe() { + this.recipe.initrecipe(components.stream() + .mapToInt(Component::getPrice) + .sum()); + } +} diff --git a/chapter9/parksey/src/compositepatternCustom/Component.java b/chapter9/parksey/src/compositepatternCustom/Component.java new file mode 100644 index 0000000..e9e6854 --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/Component.java @@ -0,0 +1,6 @@ +package compositepatternCustom; + +public interface Component { + void printInfo(); + int getPrice(); +} diff --git a/chapter9/parksey/src/compositepatternCustom/CompositeTest.java b/chapter9/parksey/src/compositepatternCustom/CompositeTest.java new file mode 100644 index 0000000..c884508 --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/CompositeTest.java @@ -0,0 +1,10 @@ +package compositepatternCustom; + +public class CompositeTest { + public static void main(String[] args) { + Box box = new Box(); + + box.printInfo(); + System.out.println(box.getPrice()); + } +} diff --git a/chapter9/parksey/src/compositepatternCustom/Recipe.java b/chapter9/parksey/src/compositepatternCustom/Recipe.java new file mode 100644 index 0000000..7717d29 --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/Recipe.java @@ -0,0 +1,17 @@ +package compositepatternCustom; + +public class Recipe{ + private int price; + + public Recipe() { + price = 0; + } + + public void initrecipe(int price) { + this.price = price; + } + + public int getPrice() { + return price; + } +} diff --git a/chapter9/parksey/src/compositepatternCustom/electric/Monitor.java b/chapter9/parksey/src/compositepatternCustom/electric/Monitor.java new file mode 100644 index 0000000..90f7d05 --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/electric/Monitor.java @@ -0,0 +1,16 @@ +package compositepatternCustom.electric; + +import compositepatternCustom.Component; + +public class Monitor implements Component { + + @Override + public void printInfo() { + System.out.println("모니터"); + } + + @Override + public int getPrice() { + return 200000; + } +} diff --git a/chapter9/parksey/src/compositepatternCustom/phonepackage/PhonePackage.java b/chapter9/parksey/src/compositepatternCustom/phonepackage/PhonePackage.java new file mode 100644 index 0000000..7347f5c --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/phonepackage/PhonePackage.java @@ -0,0 +1,30 @@ +package compositepatternCustom.phonepackage; + +import compositepatternCustom.Component; +import compositepatternCustom.phonepackage.buzz.Buzz; +import compositepatternCustom.phonepackage.charger.SamsungCharger; +import compositepatternCustom.phonepackage.phone.SamsungPhone; + +import java.util.ArrayList; +import java.util.List; + +public class PhonePackage implements Component { + private List components = new ArrayList<>(); + + public PhonePackage() { + components.add(new Buzz()); + components.add(new SamsungCharger()); + components.add(new SamsungPhone()); + } + @Override + public void printInfo() { + components.stream().forEach(Component::printInfo); + } + + @Override + public int getPrice() { + return components.stream() + .mapToInt(Component::getPrice) + .sum(); + } +} diff --git a/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Buzz.java b/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Buzz.java new file mode 100644 index 0000000..6c051ac --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Buzz.java @@ -0,0 +1,28 @@ +package compositepatternCustom.phonepackage.buzz; + +import compositepatternCustom.Component; + +import java.util.ArrayList; +import java.util.List; + +public class Buzz implements Component{ + private List components = new ArrayList<>(); + + public Buzz() { + components.add(new Cradle()); + components.add(new Earbud()); + } + + + @Override + public void printInfo() { + components.stream().forEach(Component::printInfo); + } + + @Override + public int getPrice() { + return components.stream() + .mapToInt(Component::getPrice) + .sum(); + } +} diff --git a/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Cradle.java b/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Cradle.java new file mode 100644 index 0000000..8353f16 --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Cradle.java @@ -0,0 +1,11 @@ +package compositepatternCustom.phonepackage.buzz; + +import compositepatternCustom.Component; + +public class Cradle implements Component { + @Override + public void printInfo() {System.out.println("버즈 크래들");} + + @Override + public int getPrice() {return 20000;} +} diff --git a/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Earbud.java b/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Earbud.java new file mode 100644 index 0000000..a02ff45 --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/phonepackage/buzz/Earbud.java @@ -0,0 +1,13 @@ +package compositepatternCustom.phonepackage.buzz; + +import compositepatternCustom.Component; + +public class Earbud implements Component { + @Override + public void printInfo() { + System.out.println("버즈 이어버드"); + } + + @Override + public int getPrice() {return 10000;} +} diff --git a/chapter9/parksey/src/compositepatternCustom/phonepackage/charger/SamsungCharger.java b/chapter9/parksey/src/compositepatternCustom/phonepackage/charger/SamsungCharger.java new file mode 100644 index 0000000..395630b --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/phonepackage/charger/SamsungCharger.java @@ -0,0 +1,12 @@ +package compositepatternCustom.phonepackage.charger; + +import compositepatternCustom.Component; + +public class SamsungCharger implements Component { + + @Override + public void printInfo() {System.out.println("샘송 충전기");} + + @Override + public int getPrice() {return 5000;} +} diff --git a/chapter9/parksey/src/compositepatternCustom/phonepackage/phone/SamsungPhone.java b/chapter9/parksey/src/compositepatternCustom/phonepackage/phone/SamsungPhone.java new file mode 100644 index 0000000..4a91efc --- /dev/null +++ b/chapter9/parksey/src/compositepatternCustom/phonepackage/phone/SamsungPhone.java @@ -0,0 +1,9 @@ +package compositepatternCustom.phonepackage.phone; + +import compositepatternCustom.Component; + +public class SamsungPhone implements Component { + public void printInfo() {System.out.println("샘송 갤럭쉬 21");} + + public int getPrice() {return 100000;} +} diff --git a/chapter9/parksey/src/iteratorPattern/Application.java b/chapter9/parksey/src/iteratorPattern/Application.java new file mode 100644 index 0000000..6f14519 --- /dev/null +++ b/chapter9/parksey/src/iteratorPattern/Application.java @@ -0,0 +1,19 @@ +package iteratorPattern; + +import iteratorPattern.iterator.ProfileIterator; +import iteratorPattern.network.SocialNetwork; + +public class Application { + private SocialSpammer spammer; + private SocialNetwork network; + + public void sendSpamToFriend(Profile profile) { + ProfileIterator profileIterator = network.createFriendsIterator(profile.getId()); + spammer.send(profileIterator, "Hello, bye"); + } + + public void sendSpamToCoworkers(Profile profile) { + ProfileIterator profileIterator = network.createCoworkersIterator(profile.getId()); + spammer.send(profileIterator, "you're fired"); + } +} diff --git a/chapter9/parksey/src/iteratorPattern/Profile.java b/chapter9/parksey/src/iteratorPattern/Profile.java new file mode 100644 index 0000000..c364c61 --- /dev/null +++ b/chapter9/parksey/src/iteratorPattern/Profile.java @@ -0,0 +1,19 @@ +package iteratorPattern; + +public class Profile { + private String id; + private String email; + + public Profile(String id, String email) { + this.id = id; + this.email = email; + } + + public String getEmail() { + return email; + } + + public String getId() { + return id; + } +} diff --git a/chapter9/parksey/src/iteratorPattern/SocialSpammer.java b/chapter9/parksey/src/iteratorPattern/SocialSpammer.java new file mode 100644 index 0000000..deb8915 --- /dev/null +++ b/chapter9/parksey/src/iteratorPattern/SocialSpammer.java @@ -0,0 +1,19 @@ +package iteratorPattern; + +import iteratorPattern.iterator.ProfileIterator; + +import java.util.ArrayList; +import java.util.List; + +public class SocialSpammer { + public List send(ProfileIterator profileIterator, String message) { + List emails = new ArrayList<>(); + while(profileIterator.hasMore()) { + Profile profile = profileIterator.getNext(); + emails.add(profile.getEmail()); + } + + return emails; + } + +} diff --git a/chapter9/parksey/src/iteratorPattern/iterator/FacebookIterator.java b/chapter9/parksey/src/iteratorPattern/iterator/FacebookIterator.java new file mode 100644 index 0000000..7150dad --- /dev/null +++ b/chapter9/parksey/src/iteratorPattern/iterator/FacebookIterator.java @@ -0,0 +1,37 @@ +package iteratorPattern.iterator; + +import iteratorPattern.Profile; +import iteratorPattern.network.Facebook; + +public class FacebookIterator implements ProfileIterator{ + private static final int INIT_SIZE = 6; + + private Facebook facebook; + private String profileId; + private int currentPosition; + private Profile[] cache; + + public FacebookIterator(Facebook facebook, String profileId) { + currentPosition = 0; + cache = new Profile[INIT_SIZE]; + } + + public FacebookIterator(Facebook facebook, String profileId, int size) { + currentPosition = 0; + cache = new Profile[size]; + } + + + @Override + public Profile getNext() { + if (hasMore()) { + return cache[++currentPosition]; + } + return null; + } + + @Override + public boolean hasMore() { + return currentPosition < cache.length; + } +} diff --git a/chapter9/parksey/src/iteratorPattern/iterator/ProfileIterator.java b/chapter9/parksey/src/iteratorPattern/iterator/ProfileIterator.java new file mode 100644 index 0000000..dcc8f95 --- /dev/null +++ b/chapter9/parksey/src/iteratorPattern/iterator/ProfileIterator.java @@ -0,0 +1,8 @@ +package iteratorPattern.iterator; + +import iteratorPattern.Profile; + +public interface ProfileIterator { + Profile getNext(); + boolean hasMore(); +} diff --git a/chapter9/parksey/src/iteratorPattern/network/Facebook.java b/chapter9/parksey/src/iteratorPattern/network/Facebook.java new file mode 100644 index 0000000..3303702 --- /dev/null +++ b/chapter9/parksey/src/iteratorPattern/network/Facebook.java @@ -0,0 +1,17 @@ +package iteratorPattern.network; + +import iteratorPattern.iterator.FacebookIterator; +import iteratorPattern.iterator.ProfileIterator; + +public class Facebook implements SocialNetwork { + + @Override + public ProfileIterator createFriendsIterator(String profileId) { + return new FacebookIterator(this, profileId); + } + + @Override + public ProfileIterator createCoworkersIterator(String profileId) { + return new FacebookIterator(this, profileId); + } +} diff --git a/chapter9/parksey/src/iteratorPattern/network/SocialNetwork.java b/chapter9/parksey/src/iteratorPattern/network/SocialNetwork.java new file mode 100644 index 0000000..934e92b --- /dev/null +++ b/chapter9/parksey/src/iteratorPattern/network/SocialNetwork.java @@ -0,0 +1,9 @@ +package iteratorPattern.network; + +import iteratorPattern.Profile; +import iteratorPattern.iterator.ProfileIterator; + +public interface SocialNetwork { + ProfileIterator createFriendsIterator(String profileId); + ProfileIterator createCoworkersIterator(String profileId); +}