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

[20210129] DNS, 팩토리 메서드 패턴 #25

Open
JuHyun419 opened this issue Jan 29, 2021 · 0 comments
Open

[20210129] DNS, 팩토리 메서드 패턴 #25

JuHyun419 opened this issue Jan 29, 2021 · 0 comments

Comments

@JuHyun419
Copy link
Owner

DNS(Domain Name System)

  • 기본 UDP/53 포트, 특수 TCP/53 포트
  • 기본적으로 UDP를 이용하나, 특수 상황에서는 TCP를 사용하기도 함
  • DNS 질의 => IP 기준으로 통신을 하는데 도메인 밖에 모르니 DNS 서버를 빌려 IP를 캐냄

팩토리 메서드 디자인 패턴

  • 객체를 new 연산자를 통해 인스턴스화 하면 문제점이 존재함.
  • 객체 생성을 변경하면 생성하는 모든 곳을 수정해야함.
  • 한 곳을 변경하면 잠재적으로 다른 많은 곳에서 코드를 변경해야 하므로 코드가 밀접하게 묶임
abstract class Shape {
}

class Circle extends Shape {
}

Circle circle = new Circle();
  • 위 상황을 해결하기 위해 팩토리 메서드 디자인 패턴 을 사용할 수 있음
  • 필요한 특정 클래스를 알 수 없을때, 사용
enum ShapeType {
    CIRCLE, RECTANGLE
}

public abstract class Shape {
    private ShapeType shapeType = null;

    public Shape(ShapeType shapeType) {
        this.shapeType = shapeType;
    }

    // 도형 생성
    protected abstract void generate();
}

class Rectangle extends Shape {

    Rectangle() {
        super(ShapeType.RECTANGLE);
    }

    @Override
    protected void generate() {
        System.out.println("Generate Rectangle");
    }
}


class Circle extends Shape {

    Circle() {
        super(ShapeType.CIRCLE);
    }

    @Override
    protected void generate() {
        System.out.println("Generate Circle");
    }
}

class ShapeFactory {
    public static Shape generateShape(final ShapeType shapeType) {
        Shape shape = null;

        switch (shapeType) {
            case RECTANGLE:
                shape = new Rectangle();
                break;

            case CIRCLE:
                shape = new Circle();
                break;

            default:
                break;
        }
        return shape;
    }
}

class TestFactoryPattern {
    public static void main(String[] args) {
        ShapeFactory.generateShape(ShapeType.RECTANGLE);
        ShapeFactory.generateShape(ShapeType.CIRCLE);
    }
}

https://github.com/jh-dev-study/object-oriented-thinking-process/blob/main/10.%20%EB%94%94%EC%9E%90%EC%9D%B8%20%ED%8C%A8%ED%84%B4.md

@JuHyun419 JuHyun419 changed the title [20210129] - DNS, 팩토리 메서드 패턴 [20210129] DNS, 팩토리 메서드 패턴 Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant