Overview
Abstract Factory Pattern π
Β Β μ°κ΄λ μλΈ ν΄λμ€λ₯Ό νΉμ κ·Έλ£ΉμΌλ‘ λ¬Άμ΄ ν λ²μ κ΅μ²΄ κ°λ₯νκ² λ§λ λμμΈ ν¨ν΄ Factory methodμ νμ₯μ΄λΌκ³ λ³Ό μ μλ€.
Class Diagram
Example Code π
package abstractfactory;
public interface MachineA {
public void process();
}
package abstractfactory;
public class MachineA1 implements MachineA{
@Override
public void process() {
System.out.println("excute MachineA1");
}
}
package abstractfactory;
public class MachineA2 implements MachineA{
@Override
public void process() {
System.out.println("excute MachineA2");
}
}
package abstractfactory;
public interface MachineB {
public void process();
}
package abstractfactory;
public class MachineB1 implements MachineB{
@Override
public void process() {
System.out.println("excute MachineB1");
}
}
package abstractfactory;
public class MachineB2 implements MachineB{
@Override
public void process() {
System.out.println("excute MachineB1");
}
}
package abstractfactory;
public interface MachineFactory {
public MachineA getMachineA();
public MachineB getMachineB();
}
package abstractfactory;
public class MachineFactoryA implements MachineFactory{
@Override
public MachineA getMachineA() {
return new MachineA1();
}
@Override
public MachineB getMachineB() {
return new MachineB1();
}
}
package abstractfactory;
public class MachineFactoryB implements MachineFactory{
@Override
public MachineA getMachineA() {
return new MachineA2();
}
@Override
public MachineB getMachineB() {
return new MachineB2();
}
}
Main Program π
package abstractfactory;
public class FactoryMain {
public static void main(String[] args) {
MachineFactory machineFactoryA = new MachineFactoryA();
machineFactoryA.getMachineA().process();
machineFactoryA.getMachineB().process();
MachineFactory machineFactoryB = new MachineFactoryB();
machineFactoryB.getMachineA().process();
machineFactoryB.getMachineB().process();
}
}
excute MachineA1
excute MachineB1
excute MachineA2
excute MachineB1
μ λμνλ€.
Closing Remarks π©
Abstract Factory Pattern
Β Β λμΌν μ²λ¦¬ λ‘μ§μ νμ ν΄λμ€μ κ²°ν©μ ν΅ν΄ μ νμ μΌλ‘ κ°μ²΄λ₯Ό μμ±ν μ μλ€. λ€λ§, μλ‘μ΄ κ°μ²΄κ° μΆκ°λλ€λ©΄ νμ ν΄λμ€λ κ°μ΄ μΆκ°ν΄μΌ λκ³ νμ₯ μ λͺ¨λ νμ ν΄λμ€μ μμ μ΄ νμν μλ μλ€.
Factory method
Β Β 1κ°μ νμ ν΄λμ€ λ΄ λ§€κ°λ³μλ₯Ό ν΅ν΄ μμ±μ μ νμ μΌλ‘ μ²λ¦¬νλ―λ‘ λ€νμ±μ λ¨μ μ ν΄κ²°ν μ μλ€.
λ€λ§, μλ‘μ΄ κ°μ²΄κ° μΆκ°λλ€λ©΄ μ‘°κ±΄μ΄ μΆκ°λμ΄μΌ νκ³ νμ₯ μ νμ ν΄λμ€μ κ·λͺ¨κ° 컀μ§κΈ° λλ¬Έμ μ μ§ λ³΄μκ° μ΄λ €μΈ μ μλ€.