추상 공장 패턴
10278 단어 creationaljavadesignpatternspattern
참가자들
암호
public class Main {
public static void main(String[] args) {
// Abstract factory #1
AbstractFactory factory1 = new ConcreteFactory1();
Client client1 = new Client(factory1);
client1.run();
// Abstract factory #2
AbstractFactory factory2 = new ConcreteFactory2();
Client client2 = new Client(factory2);
client2.run();
}
}
public interface AbstractFactory {
AbstractProductA createProductA();
AbstractProductB createProductB();
}
public class ConcreteFactory1 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA1();
}
@Override
public AbstractProductB createProductB() {
return new ProductB1();
}
}
public class ConcreteFactory2 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA2();
}
@Override
public AbstractProductB createProductB() {
return new ProductB2();
}
}
public interface AbstractProductA {
}
public interface AbstractProductB {
void interact(AbstractProductA a);
}
public class ProductA1 implements AbstractProductA {
}
public class ProductB1 implements AbstractProductB {
@Override
public void interact(AbstractProductA a) {
System.out.println(this.getClass().getSimpleName() + " interacts with " + a.getClass().getSimpleName());
}
}
public class ProductA2 implements AbstractProductA {
}
public class ProductB2 implements AbstractProductB {
@Override
public void interact(AbstractProductA a) {
System.out.println(this.getClass().getSimpleName() + " interacts with " + a.getClass().getSimpleName());
}
}
public class Client {
private AbstractProductA abstractProductA;
private AbstractProductB abstractProductB;
public Client(AbstractFactory factory) {
abstractProductB = factory.createProductB();
abstractProductA = factory.createProductA();
}
public void run() {
abstractProductB.interact(abstractProductA);
}
}
산출
ProductB1 interacts with ProductA1
ProductB2 interacts with ProductA2
eidherjulian61
/
디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(추상 공장 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/eidher/abstract-factory-3opg
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
public class Main {
public static void main(String[] args) {
// Abstract factory #1
AbstractFactory factory1 = new ConcreteFactory1();
Client client1 = new Client(factory1);
client1.run();
// Abstract factory #2
AbstractFactory factory2 = new ConcreteFactory2();
Client client2 = new Client(factory2);
client2.run();
}
}
public interface AbstractFactory {
AbstractProductA createProductA();
AbstractProductB createProductB();
}
public class ConcreteFactory1 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA1();
}
@Override
public AbstractProductB createProductB() {
return new ProductB1();
}
}
public class ConcreteFactory2 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA2();
}
@Override
public AbstractProductB createProductB() {
return new ProductB2();
}
}
public interface AbstractProductA {
}
public interface AbstractProductB {
void interact(AbstractProductA a);
}
public class ProductA1 implements AbstractProductA {
}
public class ProductB1 implements AbstractProductB {
@Override
public void interact(AbstractProductA a) {
System.out.println(this.getClass().getSimpleName() + " interacts with " + a.getClass().getSimpleName());
}
}
public class ProductA2 implements AbstractProductA {
}
public class ProductB2 implements AbstractProductB {
@Override
public void interact(AbstractProductA a) {
System.out.println(this.getClass().getSimpleName() + " interacts with " + a.getClass().getSimpleName());
}
}
public class Client {
private AbstractProductA abstractProductA;
private AbstractProductB abstractProductB;
public Client(AbstractFactory factory) {
abstractProductB = factory.createProductB();
abstractProductA = factory.createProductA();
}
public void run() {
abstractProductB.interact(abstractProductA);
}
}
ProductB1 interacts with ProductA1
ProductB2 interacts with ProductA2
eidherjulian61 / 디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(추상 공장 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/eidher/abstract-factory-3opg텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)