Skip to content

Instantly share code, notes, and snippets.

@DScheglov
Last active January 20, 2021 08:05
Show Gist options
  • Select an option

  • Save DScheglov/42121d286f51fe5ae0628e02e6c3d2c9 to your computer and use it in GitHub Desktop.

Select an option

Save DScheglov/42121d286f51fe5ae0628e02e6c3d2c9 to your computer and use it in GitHub Desktop.
factory-method
public abstract class CodingService {
abstract Developer createDeveloper();
public void writeCode() {
Developer dev = createDeveloper();
String code = dev.writeCode();
System.out.println(code);
}
}
public interface Developer {
String writeCode();
}
public class JavaCodingService extends CodingService {
@Override
Developer createDeveloper() {
return new JavaDeveloper();
}
}
public class JavaDeveloper implements Developer {
@Override
public String writeCode() {
return "-- some java code --";
}
}
public class PhpCodingService extends CodingService {
@Override
Developer createDeveloper() {
return new PhpDeveloper();
}
}
public class PhpDeveloper implements Developer {
@Override
public String writeCode() {
return "-- some php code --";
}
}
public class Program {
public static void main(String[] args) {
writeJavaCode();
writePhpCode();
}
public static void writeJavaCode() {
CodingService codingService = new JavaCodingService();
codingService.writeCode();
}
public static void writePhpCode() {
CodingService codingService = new PhpCodingService();
codingService.writeCode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment