added abstract factory example

This commit is contained in:
Louis Heredero 2024-10-07 10:36:54 +02:00
parent 2f41073a0f
commit 50e6ed18df
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
10 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package learn.simple_abstract_factory;
public interface AbstractFactory {
public AbstractProductA createProductA();
public AbstractProductB createProductB();
}

View File

@ -0,0 +1,4 @@
package learn.simple_abstract_factory;
public interface AbstractProductA {
}

View File

@ -0,0 +1,4 @@
package learn.simple_abstract_factory;
public interface AbstractProductB {
}

View File

@ -0,0 +1,13 @@
package learn.simple_abstract_factory;
public class ConcreteFactory1 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA1();
}
@Override
public AbstractProductB createProductB() {
return new ProductB1();
}
}

View File

@ -0,0 +1,13 @@
package learn.simple_abstract_factory;
public class ConcreteFactory2 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA2();
}
@Override
public AbstractProductB createProductB() {
return new ProductB2();
}
}

View File

@ -0,0 +1,16 @@
package learn.simple_abstract_factory;
public class Main {
public static void main(String[] args) {
Client client1 = new Client(new ConcreteFactory1());
Client client2 = new Client(new ConcreteFactory1());
client1.setup();
client2.setAbstractFactory(new ConcreteFactory2());
client2.setup();
System.out.println(client1);
System.out.println(client2);
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_abstract_factory;
public class ProductA1 implements AbstractProductA {
@Override
public String toString() {
return "ProductA1";
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_abstract_factory;
public class ProductA2 implements AbstractProductA {
@Override
public String toString() {
return "ProductA2";
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_abstract_factory;
public class ProductB1 implements AbstractProductB {
@Override
public String toString() {
return "ProductB1";
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_abstract_factory;
public class ProductB2 implements AbstractProductB {
@Override
public String toString() {
return "ProductB2";
}
}