-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactories.java
More file actions
161 lines (116 loc) · 3.64 KB
/
Copy pathFactories.java
File metadata and controls
161 lines (116 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package interfaces.factorymethod;
/**
* Created by WORK_WERT on 19.01.2017.
*/
// Пример фабричных методов. Делаем фабрику сервисов
// это то, что будет выпускать наша фабрика, это продукт
interface Service {
void method1();
void method2();
}
// а это наша фабрика сервисов
interface ServiceFactory {
Service getService(); // фабричный метод
Service getService(String name); // фабричный метод
}
// это первая имплементация "продукта" сервиса
class Implementation1 implements Service {
String serviceName;
public Implementation1() {
serviceName = "NoNaMe1";
}
public Implementation1(String name) {
serviceName = name;
}
@Override
public void method1() {
System.out.println("Implementation1 method1");
}
@Override
public void method2() {
System.out.println("Implementation1 method2");
}
public String getName() {
return serviceName;
}
}
// это первая имплементация фабрики
class Implementation1Factory implements ServiceFactory {
String serviceName;
public Implementation1Factory() {
serviceName = "NoName1F";
}
public Implementation1Factory(String name) {
serviceName = name;
}
// метод возвращающий реальный продукт Service
@Override
public Service getService() {
return new Implementation1(serviceName);
}
@Override
public Service getService(String name) {
return new Implementation1(name);
}
}
// это вторая имплементация "продукта" сервиса
class Implementation2 implements Service {
String serviceName;
public Implementation2() {
serviceName = "NoNaMe2";
}
public Implementation2(String name) {
serviceName = name;
}
@Override
public void method1() {
System.out.println("Implementation2 method1");
}
@Override
public void method2() {
System.out.println("Implementation2 method2");
}
public String getName() {
return serviceName;
}
}
// это вторая имплементация фабрики
class Implementation2Factory implements ServiceFactory {
String serviceName;
public Implementation2Factory() {
serviceName = "NoName2F";
}
public Implementation2Factory(String name) {
serviceName = name;
}
// метод возвращающий реальный продукт Service
@Override
public Service getService() {
return new Implementation2(serviceName);
}
@Override
public Service getService(String name) {
return new Implementation2(name);
}
}
public class Factories {
public static void main(String[] args) {
// выпускаем наши сервисы с помощью разных фабрик
serviceConsumer(new Implementation1Factory("Service1"));
System.out.println();
serviceConsumer(new Implementation2Factory("Service2"));
}
static void serviceConsumer(ServiceFactory fact) {
Service s = fact.getService();
s.method1();
s.method2();
if (s instanceof Implementation1) {
Implementation1 i1 = (Implementation1) s;
System.out.println(i1.getName());
}
if (s instanceof Implementation2) {
Implementation2 i2 = (Implementation2) s;
System.out.println(i2.getName());
}
}
}