Created
May 23, 2019 11:32
-
-
Save n0tb0dy/90187e99c86e43771d16c1eebecde8fa to your computer and use it in GitHub Desktop.
Пример фабричных методов Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pro.java.factory1; | |
import static pro.java.util.Print.*; | |
// Пример фабричных методов. Делаем фабрику сервисов | |
// это то, что будет выпускать наша фабрика, это продукт | |
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() { | |
println("Implementation1 method1"); | |
} | |
@Override | |
public void method2() { | |
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() { | |
println("Implementation2 method1"); | |
} | |
@Override | |
public void method2() { | |
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")); | |
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; | |
println(i1.getName()); | |
} | |
if (s instanceof Implementation2) { | |
Implementation2 i2 = (Implementation2) s; | |
println(i2.getName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment