Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[박세연] [템플릿 메서드, 반복자, 컴포지트]패턴 구현 #9 #11

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,9 @@ gradle-app.setting
# Java heap dump
*.hprof

# End of https://www.toptal.com/developers/gitignore/api/java,intellij,gradle
# End of https://www.toptal.com/developers/gitignore/api/java,intellij,gradle

.idea
parksey.imi

*.iml
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions chapter1/parksey/character/Character.java
Original file line number Diff line number Diff line change
@@ -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();
}
14 changes: 14 additions & 0 deletions chapter1/parksey/character/King.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
14 changes: 14 additions & 0 deletions chapter1/parksey/character/Knight.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
14 changes: 14 additions & 0 deletions chapter1/parksey/character/Queen.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
14 changes: 14 additions & 0 deletions chapter1/parksey/character/Troll.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
8 changes: 8 additions & 0 deletions chapter1/parksey/weapon/AxeBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package strategypattern.puzzle1.weapon;

public class AxeBehavior implements WeaponBehavior {
@Override
public void userWeapon() {

}
}
8 changes: 8 additions & 0 deletions chapter1/parksey/weapon/BowAndArrowBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package strategypattern.puzzle1.weapon;

public class BowAndArrowBehavior implements WeaponBehavior{
@Override
public void userWeapon() {

}
}
8 changes: 8 additions & 0 deletions chapter1/parksey/weapon/KnifeBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package strategypattern.puzzle1.weapon;

public class KnifeBehavior implements WeaponBehavior{
@Override
public void userWeapon() {

}
}
8 changes: 8 additions & 0 deletions chapter1/parksey/weapon/SwordBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package strategypattern.puzzle1.weapon;

public class SwordBehavior implements WeaponBehavior{
@Override
public void userWeapon() {

}
}
5 changes: 5 additions & 0 deletions chapter1/parksey/weapon/WeaponBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package strategypattern.puzzle1.weapon;

public interface WeaponBehavior {
void userWeapon();
}
11 changes: 11 additions & 0 deletions chapter2/parksey/StockApplication.java
Original file line number Diff line number Diff line change
@@ -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();

}
}
37 changes: 37 additions & 0 deletions chapter2/parksey/company/domain/Company.java
Original file line number Diff line number Diff line change
@@ -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<User> 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<User> getUsers() {
return users;
}

public void addUser(User user) {
this.users.add(user);
}
}
57 changes: 57 additions & 0 deletions chapter2/parksey/company/dto/CompanyDatas.java
Original file line number Diff line number Diff line change
@@ -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<Observer> 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;
}
}
12 changes: 12 additions & 0 deletions chapter2/parksey/company/dto/CompanySubject.java
Original file line number Diff line number Diff line change
@@ -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();
}
50 changes: 50 additions & 0 deletions chapter2/parksey/transaction/TransactionController.java
Original file line number Diff line number Diff line change
@@ -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<Company> initCompanys;
private static List<User> 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<CompanyDatas> companyDatasList = new ArrayList<>();
initCompanys.stream()
.forEach(company -> companyDatasList.add(CompanyDatas.addSubject(company)));
update(companyDatasList);
}

private void update(List<CompanyDatas> companyDatasList) {
companyDatasList.stream()
.forEach(companyDatas -> companyDatas.updatePrice(1000));
}

}
19 changes: 19 additions & 0 deletions chapter2/parksey/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions chapter2/parksey/user/dto/Observer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package observerpattern.puzzle.user.dto;

public interface Observer {
void notification();
}
31 changes: 31 additions & 0 deletions chapter2/parksey/user/dto/UserObserver.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading