728x90
반응형
원래 클라이언트 코드
WhiteshipFactory.java
public class WhiteshipFactory extends DefaultShipFactory{
public Ship createShip(){
Ship ship = new Whiteship();
ship.setAnchor(new WhiteAnchor());
ship.setWhell(new WhiteWheel());
return ship;
}
}
- 지금 구체화되어 있는데 인터페이스 생성하자
ShipPartsFactory.java
public interface ShipPartsFactory {
Anchor createAnchor();
Wheel createWheel();
}
- 이게 추상 팩토리
WhiteahipFactory.java
public class WhiteshipFactory extends DefaultShipFactory{
public Ship createShip(){
Ship ship = new Whiteship();
ship.setAnchor(new WhiteAnchor());
ship.setWhell(new WhiteWheel());
return ship;
}
}
WhiteshipPartsFactory.java
public class WhiteshipPartsFactory implements ShipPartsFactory{
@Override
public Anchor createAnchor() {
return new WhiteAnchor();
}
@Override
public Wheel createWheel(){
return new WhiteWheel();
}
}
Anchor 관련 인터페이스
public interface Anchor {
}
///
public class WhiteAnchor implements Anchor {
}
Wheel 관련 인터페이스
public interface Wheel {
}
///
public class WhiteWheel implements Wheel{
}
적용하기
WhiteshipFactory | before
public class WhiteshipFactory extends DefaultShipFactory{
public Ship createShip(){
@Override
Ship ship = new Whiteship();
ship.setAnchor(new WhiteAnchor());
ship.setWhell(new WhiteWheel());
return ship;
}
}
WhiteshipFactory | after
public class WhiteshipFactory extends DefaultShipFactory{
private ShipPartsFactory shipPartsFactory;
public WhiteshipFactory(ShipPartsFactory shipPartsFactory){
this.shipPartsFactory = shipPartsFactory;
}
@Override
public Ship createShip(){
Ship ship = new Whiteship();
ship.setAnchor(shipPartsFactory.createAnchor());
ship.setWhell(shipPartsFactory.createWheel());
return ship;
}
}
Ship.java | before
public class Ship {
private String name;
private String color;
private String logo;
private Wheel wheel;
private Anchor anchor;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@Override
public String toString() {
return "Ship{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", logo='" + logo + '\'' +
'}';
}
public void setAnchor(WhiteAnchor anchor) {
}
public void setWheel(WhiteWheel wheel) {
}
}
- 이전은 구체적인 클래스여서 저렇게 되어있었지만
Ship.java | after
public class Ship {
private String name;
private String color;
private String logo;
private Wheel wheel;
private Anchor anchor;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@Override
public String toString() {
return "Ship{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", logo='" + logo + '\'' +
'}';
}
public void setAnchor(Anchor anchor) {
}
public void setWheel(Wheel wheel) {
}
}
WhiteAnchorPro 추가
-
public class WhitePartsProFactory implements ShipPartsFactory{ @Override public Anchor createAnchor() { return new WhiteAnchorPro(); } @Override public Wheel createWheel() { return new WhiteWheelPro(); } }
-
public class WhiteAnchorPro implements Anchor{ }
-
public class WhiteWheelPro implements Wheel{ }
- 위와 같이 준비를 해주면 이제 pro를 추가할 수 있는것
Ship.java 수정
public class Ship {
private String name;
private String color;
private String logo;
private Wheel wheel;
private Anchor anchor;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@Override
public String toString() {
return "Ship{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", logo='" + logo + '\'' +
'}';
}
public Wheel getWheel() {
return wheel;
}
public Anchor getAnchor() {
return anchor;
}
}
ShipInventory.java
public class ShipInventory {
public static void main(String[] args){
ShipFactory shipFactory = new WhiteshipFactory(new WhitePartsProFactory());
Ship ship = shipFactory.createShip();
System.out.println(ship.getAnchor().getClass());
System.out.println(ship.getWheel().getClass());
}
}
ShipFactory shipFactory = new WhiteshipFactory(new WhitePartsProFactory());
ShipFactory shipFactory = new WhiteshipFactory(new WhitePartsFactory());
- 제품군에 팩토리에 따라서 달라지는거지 팩토리 자체를 바꾸는것이 아니기 때문에 더 좋아진것
- 단일 책임원칙을 지켰고, 여러 가지 제품군 만들기 때문에 이거 지키지 않은것 아니냐 할 수 있긴함
- 개방 폐쇄 원칙을 지켰다고 볼 수 있음
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
2021.11.21_추상팩토리패턴04.자바와스프링에서찾아보는패턴 (0) | 2021.11.21 |
---|---|
2021.11.21_추상팩토리패턴03.장단점 (0) | 2021.11.21 |
2021.11.21_추상팩토리패턴01.패턴소개 (0) | 2021.11.21 |
2021.11.18_팩토리메소드패턴05.자바와스프링에서찾아보는패턴 (0) | 2021.11.18 |
2021.11.18_팩토리메소드패턴04.장점과단점 (0) | 2021.11.18 |
댓글