Last active
January 20, 2021 08:05
-
-
Save DScheglov/42121d286f51fe5ae0628e02e6c3d2c9 to your computer and use it in GitHub Desktop.
factory-method
This file contains hidden or 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
| public abstract class CodingService { | |
| abstract Developer createDeveloper(); | |
| public void writeCode() { | |
| Developer dev = createDeveloper(); | |
| String code = dev.writeCode(); | |
| System.out.println(code); | |
| } | |
| } |
This file contains hidden or 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
| public interface Developer { | |
| String writeCode(); | |
| } |
This file contains hidden or 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
| public class JavaCodingService extends CodingService { | |
| @Override | |
| Developer createDeveloper() { | |
| return new JavaDeveloper(); | |
| } | |
| } |
This file contains hidden or 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
| public class JavaDeveloper implements Developer { | |
| @Override | |
| public String writeCode() { | |
| return "-- some java code --"; | |
| } | |
| } |
This file contains hidden or 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
| public class PhpCodingService extends CodingService { | |
| @Override | |
| Developer createDeveloper() { | |
| return new PhpDeveloper(); | |
| } | |
| } |
This file contains hidden or 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
| public class PhpDeveloper implements Developer { | |
| @Override | |
| public String writeCode() { | |
| return "-- some php code --"; | |
| } | |
| } |
This file contains hidden or 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
| 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