-
Notifications
You must be signed in to change notification settings - Fork 1
[유원우] 추상팩토리패턴 & 싱글톤을 이용한 GUI OS 과제 구현 #6
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
Open
wonu606
wants to merge
12
commits into
Study-Family:main
Choose a base branch
from
wonu606:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0880ab4
feat: 인터페이스 구현
wonu606 aa5b93f
feat: GUI팩토리 구현체 구현
wonu606 4ea769f
feat: 맥 팩토리 제품 구현
wonu606 2d76ac7
chore: Checkbox 디렉토리명 앞대문자 -> 소문자로 변경
wonu606 2339e2f
feat: 윈도우 팩토리 제품 구현
wonu606 ae8b2c6
fix: 맥 체크박스명이 잘못되어 있어 수정
wonu606 c1e8be9
chore: 맥 팩토리명이 잘못되어 있어 수정
wonu606 ca0e05d
feat: 로직 구현
wonu606 3a0a2b4
커맨드 패턴을 이용한 텍스트 에디터 구현
wonu606 2a9ce3c
어댑터 패턴을 이용한 미니 프로젝트 구현
wonu606 9b293fe
feat: 템플릿 메서드, 반복자, 컴포지트 패턴 구현
wonu606 7f21d9b
chore: 템플릿 메서드, 반복자, 컴포지트 패턴를 Merge해버려서 폴더 이동하여 PR
wonu606 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import button.Button; | ||
| import checkbox.Checkbox; | ||
| import factory.GUIFactory; | ||
|
|
||
| public class App { | ||
|
|
||
| private Button button; | ||
| private Checkbox checkbox; | ||
|
|
||
| public App(GUIFactory factory) { | ||
| this.button = factory.createButton(); | ||
| this.checkbox = factory.createCheckbox(); | ||
| } | ||
|
|
||
| public void paint() { | ||
| button.paint(); | ||
| checkbox.paint(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import factory.GUIFactory; | ||
| import factory.MacOSFactory; | ||
| import factory.WindowsFactory; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| private static GUIFactory macOSFactory = new MacOSFactory(); | ||
| private static GUIFactory windowsFactory = new WindowsFactory(); | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| String osKorean = scanner.nextLine(); | ||
| GUIFactory guiFactory = getOS(osKorean); | ||
| if (!validOS(guiFactory)) { | ||
| return; | ||
| } | ||
|
|
||
| App app = new App(guiFactory); | ||
| app.paint(); | ||
|
|
||
| scanner.close(); | ||
| } | ||
|
|
||
| private static boolean validOS(GUIFactory guiFactory) { | ||
| if (guiFactory == null) { | ||
| System.out.println("그런 OS는 존재하지 않습니다."); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static GUIFactory getOS(String osKorean) { | ||
| switch (osKorean) { | ||
| case "맥": | ||
| return macOSFactory; | ||
| case "윈도우": | ||
| return windowsFactory; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package button; | ||
|
|
||
| public interface Button { | ||
| void paint(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package button; | ||
|
|
||
| public class MacOSButton implements Button { | ||
|
|
||
| @Override | ||
| public void paint() { | ||
| System.out.println("[맥 마우스] 생성됨"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package button; | ||
|
|
||
| public class WindowButton implements Button { | ||
|
|
||
| @Override | ||
| public void paint() { | ||
| System.out.println("[윈도우 버튼] 생성됨"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package checkbox; | ||
|
|
||
| public interface Checkbox { | ||
|
|
||
| void paint(); | ||
| } |
9 changes: 9 additions & 0 deletions
9
AbstractFactory-And-Singleton/src/checkbox/MacOSCheckbox.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package checkbox; | ||
|
|
||
| public class MacOSCheckbox implements Checkbox { | ||
|
|
||
| @Override | ||
| public void paint() { | ||
| System.out.println("[맥 체크박스] 생성됨"); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
AbstractFactory-And-Singleton/src/checkbox/WindowCheckbox.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package checkbox; | ||
|
|
||
| public class WindowCheckbox implements Checkbox { | ||
|
|
||
| @Override | ||
| public void paint() { | ||
| System.out.println("[윈도우 체크박스] 생성됨"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package factory; | ||
|
|
||
| import checkbox.Checkbox; | ||
| import button.Button; | ||
|
|
||
| public interface GUIFactory { | ||
|
|
||
| Button createButton(); | ||
|
|
||
| Checkbox createCheckbox(); | ||
| } |
19 changes: 19 additions & 0 deletions
19
AbstractFactory-And-Singleton/src/factory/MacOSFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package factory; | ||
|
|
||
| import button.MacOSButton; | ||
| import checkbox.Checkbox; | ||
| import button.Button; | ||
| import checkbox.MacOSCheckbox; | ||
|
|
||
| public class MacOSFactory implements GUIFactory { | ||
|
|
||
| @Override | ||
| public Button createButton() { | ||
| return new MacOSButton(); | ||
| } | ||
|
|
||
| @Override | ||
| public Checkbox createCheckbox() { | ||
| return new MacOSCheckbox(); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
AbstractFactory-And-Singleton/src/factory/WindowsFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package factory; | ||
|
|
||
| import button.WindowButton; | ||
| import checkbox.Checkbox; | ||
| import button.Button; | ||
| import checkbox.WindowCheckbox; | ||
|
|
||
| public class WindowsFactory implements GUIFactory { | ||
|
|
||
| @Override | ||
| public Button createButton() { | ||
| return new WindowButton(); | ||
| } | ||
|
|
||
| @Override | ||
| public Checkbox createCheckbox() { | ||
| return new WindowCheckbox(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| RoundHole roundHole = new RoundHole(5); | ||
| RoundPeg roundPeg = new RoundPeg(5); | ||
| if (roundHole.fits(roundPeg)) { | ||
| System.out.println("roundPeg가 들어갈 수 있습니다."); | ||
| } | ||
|
|
||
| SquarePeg smallSqPeg = new SquarePeg(2); | ||
| SquarePeg largeSqPeg = new SquarePeg(20); | ||
|
|
||
| SquarePegAdapter smallPeg = new SquarePegAdapter(smallSqPeg); | ||
| SquarePegAdapter largePeg = new SquarePegAdapter(largeSqPeg); | ||
| // roundHole.fits(smallSqPeg); | ||
| // roundHole.fits(largeSqPeg); | ||
| if (roundHole.fits(smallPeg)) { | ||
| System.out.println("smallPeg가 들어갈 수 있습니다."); | ||
| } | ||
| if (roundHole.fits(largePeg)) { | ||
| System.out.println("largePeg가 들어갈 수 있습니다."); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class RoundHole { | ||
| private double radius; | ||
|
|
||
| public RoundHole(double radius) { | ||
| this.radius = radius; | ||
| } | ||
|
|
||
| public double getRadius() { | ||
| return radius; | ||
| } | ||
|
|
||
| public boolean fits(RoundPeg peg) { | ||
| return radius >= peg.getRadius(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class RoundPeg { | ||
|
|
||
| private double radius; | ||
|
|
||
| public RoundPeg() { | ||
| } | ||
|
|
||
| public RoundPeg(double radius) { | ||
| this.radius = radius; | ||
| } | ||
|
|
||
| public double getRadius() { | ||
| return radius; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class SquarePeg { | ||
| private double width; | ||
|
|
||
| public SquarePeg(double width) { | ||
| this.width = width; | ||
| } | ||
|
|
||
| public double getWidth() { | ||
| return width; | ||
| } | ||
|
|
||
| public double getSquare() { | ||
| return Math.pow(width, 2); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class SquarePegAdapter extends RoundPeg { | ||
|
|
||
| private SquarePeg peg; | ||
|
|
||
| public SquarePegAdapter(SquarePeg peg) { | ||
| this.peg = peg; | ||
| } | ||
|
|
||
| @Override | ||
| public double getRadius() { | ||
| return Math.sqrt( | ||
| Math.pow(peg.getWidth() / 2, 2) * 2); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import editor.ImageEditor; | ||
| import shapes.Circle; | ||
| import shapes.CompoundShape; | ||
| import shapes.Dot; | ||
| import shapes.Rectangle; | ||
|
|
||
| public class Demo { | ||
| public static void main(String[] args) { | ||
| ImageEditor editor = new ImageEditor(); | ||
|
|
||
| editor.loadShapes( | ||
| new Circle(10, 10, 10), | ||
|
|
||
| new CompoundShape( | ||
| new Circle(110, 110, 50), | ||
| new Dot(160, 160) | ||
| ), | ||
|
|
||
| new CompoundShape( | ||
| new Rectangle(250, 250, 100, 100), | ||
| new Dot(240, 240), | ||
| new Dot(240, 360), | ||
| new Dot(360, 360), | ||
| new Dot(360, 240) | ||
| ) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package editor; | ||
|
|
||
| import shapes.BaseShape; | ||
| import shapes.CompoundShape; | ||
|
|
||
| public class ImageEditor { | ||
| private CompoundShape allShapes = new CompoundShape(); | ||
|
|
||
| public void loadShapes(BaseShape... shapes) { | ||
| allShapes.clear(); | ||
| allShapes.add(shapes); | ||
| printCanvas(); | ||
| } | ||
|
|
||
| private void printCanvas() { | ||
| allShapes.print(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package shapes; | ||
|
|
||
| import java.awt.Graphics; | ||
|
|
||
| public abstract class BaseShape implements Shape { | ||
|
|
||
| private int x; | ||
| private int y; | ||
|
|
||
| BaseShape(int x, int y) { | ||
| this.x = x; | ||
| this.y = y; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public int getX() { | ||
| return x; | ||
| } | ||
|
|
||
| @Override | ||
| public int getY() { | ||
| return y; | ||
| } | ||
|
|
||
| public void print() { | ||
| System.out.println(this.getClass().getSimpleName() + ": Position -> (" + getX() + "," + getY() + "), Dimensions -> (" + getWidth() + "," + getHeight() + ")"); | ||
| } | ||
| protected void print(int depth) { | ||
| printTab(depth); | ||
| print(); | ||
| } | ||
|
|
||
| protected void printTab(int depth) { | ||
| for(int i = 0; i < depth; i++) { | ||
| System.out.print("\t"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package shapes; | ||
|
|
||
| public class Circle extends BaseShape{ | ||
|
|
||
| private final int radius; | ||
|
|
||
| public Circle(int x, int y, int radius) { | ||
| super(x, y); | ||
| this.radius = radius; | ||
| } | ||
|
|
||
| @Override | ||
| public int getWidth() { | ||
| return radius * 2; | ||
| } | ||
|
|
||
| @Override | ||
| public int getHeight() { | ||
| return radius * 2; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사용하지 않을 경우에도 메모리를 점유하는 문제가 있는 것 같아요!
사용하고자 할 때에만 생성하는 방식은 어떤가요~?🤔