Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions AbstractFactory-And-Singleton/src/App.java
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();
}
}
44 changes: 44 additions & 0 deletions AbstractFactory-And-Singleton/src/Main.java
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용하지 않을 경우에도 메모리를 점유하는 문제가 있는 것 같아요!
사용하고자 할 때에만 생성하는 방식은 어떤가요~?🤔

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;
}
}
}
5 changes: 5 additions & 0 deletions AbstractFactory-And-Singleton/src/button/Button.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package button;

public interface Button {
void paint();
}
9 changes: 9 additions & 0 deletions AbstractFactory-And-Singleton/src/button/MacOSButton.java
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("[맥 마우스] 생성됨");
}
}
9 changes: 9 additions & 0 deletions AbstractFactory-And-Singleton/src/button/WindowButton.java
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("[윈도우 버튼] 생성됨");
}
}
6 changes: 6 additions & 0 deletions AbstractFactory-And-Singleton/src/checkbox/Checkbox.java
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 AbstractFactory-And-Singleton/src/checkbox/MacOSCheckbox.java
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("[맥 체크박스] 생성됨");
}
}
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("[윈도우 체크박스] 생성됨");
}
}
11 changes: 11 additions & 0 deletions AbstractFactory-And-Singleton/src/factory/GUIFactory.java
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 AbstractFactory-And-Singleton/src/factory/MacOSFactory.java
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 AbstractFactory-And-Singleton/src/factory/WindowsFactory.java
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();
}
}
24 changes: 24 additions & 0 deletions Adapterpattern/src/Main.java
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가 들어갈 수 있습니다.");
}
}
}
15 changes: 15 additions & 0 deletions Adapterpattern/src/RoundHole.java
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();
}
}
15 changes: 15 additions & 0 deletions Adapterpattern/src/RoundPeg.java
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;
}
}
15 changes: 15 additions & 0 deletions Adapterpattern/src/SquarePeg.java
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);
}
}
14 changes: 14 additions & 0 deletions Adapterpattern/src/SquarePegAdapter.java
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);
}
}
28 changes: 28 additions & 0 deletions chapter8,9/compositepattern/src/Demo.java
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)
)
);
}
}
18 changes: 18 additions & 0 deletions chapter8,9/compositepattern/src/editor/ImageEditor.java
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();
}
}
39 changes: 39 additions & 0 deletions chapter8,9/compositepattern/src/shapes/BaseShape.java
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");
}
}
}
21 changes: 21 additions & 0 deletions chapter8,9/compositepattern/src/shapes/Circle.java
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;
}
}
Loading