Created
July 5, 2017 11:21
-
-
Save AdelinGhanaem/a5ab952feb043a3367b63e03b6dc7f67 to your computer and use it in GitHub Desktop.
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
| package com.design.patterns.factory.Adapter; | |
| /** | |
| * Created by Adelin_Ghanayem on 05-Jul-17. | |
| */ | |
| public class AdapterFromUsersRepositoryToMySQLUserRepository implements UsersRepository { // implements used interface by client ! | |
| //compose the adaptee class/interface | |
| private MySQLUserRepository mySQLUserRepository; | |
| //constructor ... | |
| @Override | |
| public void save(AnotherUserType anotherUserType) { | |
| //Adapt any object that needs adaptation ... | |
| User user = new User(); | |
| user.setName(anotherUserType.getName()); | |
| mySQLUserRepository.save(user); | |
| } | |
| } |
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
| package com.design.patterns.factory.Adapter; | |
| /** | |
| * Created by Adelin_Ghanayem on 05-Jul-17. | |
| */ | |
| public class AnotherUserType { | |
| private int id; | |
| private String username; | |
| private String name; | |
| public AnotherUserType(int id, String username, String name) { | |
| this.id = id; | |
| this.username = username; | |
| this.name = name; | |
| } | |
| public int getId() { | |
| return id; | |
| } | |
| public String getUsername() { | |
| return username; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| //setters and getters. | |
| } |
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
| package com.design.patterns.factory.Adapter; | |
| /** | |
| * Created by Adelin_Ghanayem on 05-Jul-17. | |
| */ | |
| public class Client { | |
| private UsersRepository usersRepository; | |
| public void createNew(AnotherUserType anotherUserType){ | |
| //validate anotherUserType | |
| // send some emails ... | |
| // do any business logic | |
| //then ... | |
| usersRepository.save(anotherUserType); | |
| } | |
| } |
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
| package com.design.patterns.factory.Adapter; | |
| /** | |
| * Created by Adelin_Ghanayem on 05-Jul-17. | |
| */ | |
| public class MySQLUserRepository { // Adaptee interface | |
| //Uses JDBC to save the user in MySQL ... | |
| public void save(User user) { | |
| //save in some J | |
| } | |
| public User getById(String id){ | |
| //return a user from MySQL using JDBC ... | |
| return new User(); | |
| } | |
| } |
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
| package com.design.patterns.factory.Adapter; | |
| /** | |
| * Created by Adelin_Ghanayem on 05-Jul-17. | |
| */ | |
| public class User { | |
| private int age; | |
| private String name; | |
| //constructor, getters, and setters ! | |
| public int getAge() { | |
| return age; | |
| } | |
| public void setAge(int age) { | |
| this.age = age; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| } |
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
| package com.design.patterns.factory.Adapter; | |
| /** | |
| * Created by Adelin_Ghanayem on 05-Jul-17. | |
| */ | |
| public interface UsersRepository { | |
| void save(AnotherUserType anotherUserType); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment