Overview

다리, 즉 연결하는 패턴입니다.
같은 개체에서 다른 작업을 수행해야 합니다.


예를 들어 원이 있고 하나는 빨간색이고 하나는 녹색입니다.
원에 대한 고유 정보를 수신하고 추가 색상 정보를 추가합니다.
수신한 색상 정보를 사용하여 다른 색상을 호출할 수 있습니다.
그린 소스, 레드 소스 등의 클래스를 생성한 경우 해당 서클에 대한 고유 정보가 중복되어 클래스마다 색상이 증가하여 관리에 어려움이 있습니다.

장점
구현하고자 하는 인터페이스와 완전히 결합되어 있지 않기 때문에 구현과 추상화된 부분을 분리할 수 있다.
추상 및 실제 구현을 확장할 수 있습니다.
추상 부분을 구현하는 상속 클래스를 변경해도 클라이언트 측에 영향을 미치지 않습니다.

불리
디자인이 복잡해집니다.

 

Structure

https://ko.wikipedia.org/wiki/%EB%B8%8C%EB%A6%AC%EC%A7%80_%ED%8C%A8%ED%84%B4

 

Console Result

package com.example.design.pattern;

interface DrawAPI {
	public void drawCircle(int radius, int x, int y);
}

class RedCircle implements DrawAPI {
	@Override
	public void drawCircle(int radius, int x, int y) {
		System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]");
	}
}

class GreenCircle implements DrawAPI {
	@Override
	public void drawCircle(int radius, int x, int y) {
		System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", " + y + "]");
	}
}

abstract class Shape {
	protected DrawAPI drawAPI;

	protected Shape(DrawAPI drawAPI) {
		this.drawAPI = drawAPI;
	}

	public abstract void draw();
}

class Circle extends Shape {
	private int x, y, radius;

	public Circle(int x, int y, int radius, DrawAPI drawAPI) {
		super(drawAPI);
		this.x = x;
		this.y = y;
		this.radius = radius;
	}

	public void draw() {
		drawAPI.drawCircle(radius, x, y);
	}
}

class MyBridge {
	public static void main(String[] args) {
		Shape redCircle = new Circle(100, 100, 10, new RedCircle());
		Shape greenCircle = new Circle(100, 100, 10, new GreenCircle());

		redCircle.draw();
		greenCircle.draw();
	}
}

 

Console Result

Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]

 

Conclusion

N/A

728x90
1
반응형

+ Recent posts