From 50e6ed18df2a2c0711504ef156b62356c89e3fe8 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 7 Oct 2024 10:36:54 +0200 Subject: [PATCH] added abstract factory example --- .../simple_abstract_factory/AbstractFactory.java | 6 ++++++ .../AbstractProductA.java | 4 ++++ .../AbstractProductB.java | 4 ++++ .../ConcreteFactory1.java | 13 +++++++++++++ .../ConcreteFactory2.java | 13 +++++++++++++ src/learn/simple_abstract_factory/Main.java | 16 ++++++++++++++++ src/learn/simple_abstract_factory/ProductA1.java | 8 ++++++++ src/learn/simple_abstract_factory/ProductA2.java | 8 ++++++++ src/learn/simple_abstract_factory/ProductB1.java | 8 ++++++++ src/learn/simple_abstract_factory/ProductB2.java | 8 ++++++++ 10 files changed, 88 insertions(+) create mode 100644 src/learn/simple_abstract_factory/AbstractFactory.java create mode 100644 src/learn/simple_abstract_factory/AbstractProductA.java create mode 100644 src/learn/simple_abstract_factory/AbstractProductB.java create mode 100644 src/learn/simple_abstract_factory/ConcreteFactory1.java create mode 100644 src/learn/simple_abstract_factory/ConcreteFactory2.java create mode 100644 src/learn/simple_abstract_factory/Main.java create mode 100644 src/learn/simple_abstract_factory/ProductA1.java create mode 100644 src/learn/simple_abstract_factory/ProductA2.java create mode 100644 src/learn/simple_abstract_factory/ProductB1.java create mode 100644 src/learn/simple_abstract_factory/ProductB2.java diff --git a/src/learn/simple_abstract_factory/AbstractFactory.java b/src/learn/simple_abstract_factory/AbstractFactory.java new file mode 100644 index 0000000..0a04cda --- /dev/null +++ b/src/learn/simple_abstract_factory/AbstractFactory.java @@ -0,0 +1,6 @@ +package learn.simple_abstract_factory; + +public interface AbstractFactory { + public AbstractProductA createProductA(); + public AbstractProductB createProductB(); +} diff --git a/src/learn/simple_abstract_factory/AbstractProductA.java b/src/learn/simple_abstract_factory/AbstractProductA.java new file mode 100644 index 0000000..d022053 --- /dev/null +++ b/src/learn/simple_abstract_factory/AbstractProductA.java @@ -0,0 +1,4 @@ +package learn.simple_abstract_factory; + +public interface AbstractProductA { +} diff --git a/src/learn/simple_abstract_factory/AbstractProductB.java b/src/learn/simple_abstract_factory/AbstractProductB.java new file mode 100644 index 0000000..835b2dc --- /dev/null +++ b/src/learn/simple_abstract_factory/AbstractProductB.java @@ -0,0 +1,4 @@ +package learn.simple_abstract_factory; + +public interface AbstractProductB { +} diff --git a/src/learn/simple_abstract_factory/ConcreteFactory1.java b/src/learn/simple_abstract_factory/ConcreteFactory1.java new file mode 100644 index 0000000..e042d94 --- /dev/null +++ b/src/learn/simple_abstract_factory/ConcreteFactory1.java @@ -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(); + } +} diff --git a/src/learn/simple_abstract_factory/ConcreteFactory2.java b/src/learn/simple_abstract_factory/ConcreteFactory2.java new file mode 100644 index 0000000..d7054f0 --- /dev/null +++ b/src/learn/simple_abstract_factory/ConcreteFactory2.java @@ -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(); + } +} diff --git a/src/learn/simple_abstract_factory/Main.java b/src/learn/simple_abstract_factory/Main.java new file mode 100644 index 0000000..7099d3a --- /dev/null +++ b/src/learn/simple_abstract_factory/Main.java @@ -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); + } +} diff --git a/src/learn/simple_abstract_factory/ProductA1.java b/src/learn/simple_abstract_factory/ProductA1.java new file mode 100644 index 0000000..b8efdc1 --- /dev/null +++ b/src/learn/simple_abstract_factory/ProductA1.java @@ -0,0 +1,8 @@ +package learn.simple_abstract_factory; + +public class ProductA1 implements AbstractProductA { + @Override + public String toString() { + return "ProductA1"; + } +} diff --git a/src/learn/simple_abstract_factory/ProductA2.java b/src/learn/simple_abstract_factory/ProductA2.java new file mode 100644 index 0000000..8e037db --- /dev/null +++ b/src/learn/simple_abstract_factory/ProductA2.java @@ -0,0 +1,8 @@ +package learn.simple_abstract_factory; + +public class ProductA2 implements AbstractProductA { + @Override + public String toString() { + return "ProductA2"; + } +} diff --git a/src/learn/simple_abstract_factory/ProductB1.java b/src/learn/simple_abstract_factory/ProductB1.java new file mode 100644 index 0000000..3be4e97 --- /dev/null +++ b/src/learn/simple_abstract_factory/ProductB1.java @@ -0,0 +1,8 @@ +package learn.simple_abstract_factory; + +public class ProductB1 implements AbstractProductB { + @Override + public String toString() { + return "ProductB1"; + } +} diff --git a/src/learn/simple_abstract_factory/ProductB2.java b/src/learn/simple_abstract_factory/ProductB2.java new file mode 100644 index 0000000..835d7f5 --- /dev/null +++ b/src/learn/simple_abstract_factory/ProductB2.java @@ -0,0 +1,8 @@ +package learn.simple_abstract_factory; + +public class ProductB2 implements AbstractProductB { + @Override + public String toString() { + return "ProductB2"; + } +}