From fe9f89ad96f22d26f2e1a2a3c7f8513afdf2fb20 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 22 Aug 2020 21:07:24 +0300 Subject: [PATCH 1/2] BAEL-45087 - DAO vs Repository Pattern --- .../repositoryvsdaopattern/Tweet.java | 37 +++++++++++ .../repositoryvsdaopattern/TweetDao.java | 9 +++ .../repositoryvsdaopattern/TweetDaoImpl.java | 17 +++++ .../baeldung/repositoryvsdaopattern/User.java | 63 +++++++++++++++++++ .../repositoryvsdaopattern/UserDao.java | 13 ++++ .../repositoryvsdaopattern/UserDaoImpl.java | 33 ++++++++++ .../UserRepository.java | 21 +++++++ .../UserRepositoryImpl.java | 50 +++++++++++++++ 8 files changed, 243 insertions(+) create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/Tweet.java create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDao.java create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDaoImpl.java create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDao.java create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDaoImpl.java create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepository.java create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/Tweet.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/Tweet.java new file mode 100644 index 0000000000..13576f32f6 --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/Tweet.java @@ -0,0 +1,37 @@ +package com.baeldung.repositoryvsdaopattern; + +import java.util.Date; + +public class Tweet { + + private String email; + + private String tweetText; + + private Date dateCreated; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getTweetText() { + return tweetText; + } + + public void setTweetText(String tweetText) { + this.tweetText = tweetText; + } + + public Date getDateCreated() { + return dateCreated; + } + + public void setDateCreated(Date dateCreated) { + this.dateCreated = dateCreated; + } + +} diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDao.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDao.java new file mode 100644 index 0000000000..144c04e0ad --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDao.java @@ -0,0 +1,9 @@ +package com.baeldung.repositoryvsdaopattern; + +import java.util.List; + +public interface TweetDao { + + List fetchTweets(String email); + +} diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDaoImpl.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDaoImpl.java new file mode 100644 index 0000000000..6e4e8a8985 --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/TweetDaoImpl.java @@ -0,0 +1,17 @@ +package com.baeldung.repositoryvsdaopattern; + +import java.util.ArrayList; +import java.util.List; + +public class TweetDaoImpl implements TweetDao { + + @Override + public List fetchTweets(String email) { + List tweets = new ArrayList(); + + //call Twitter API and prepare Tweet object + + return tweets; + } + +} diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java new file mode 100644 index 0000000000..86d3554f7e --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java @@ -0,0 +1,63 @@ +package com.baeldung.repositoryvsdaopattern; + +import java.util.List; + +public class User { + + private Long id; + private String userName; + private String firstName; + private String lastName; + private String email; + + private List tweets; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public List getTweets() { + return tweets; + } + + public void setTweets(List tweets) { + this.tweets = tweets; + } + +} diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDao.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDao.java new file mode 100644 index 0000000000..47fe2e3262 --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDao.java @@ -0,0 +1,13 @@ +package com.baeldung.repositoryvsdaopattern; + +public interface UserDao { + + void create(User user); + + User read(Long id); + + void update(User user); + + void delete(String userName); + +} diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDaoImpl.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDaoImpl.java new file mode 100644 index 0000000000..24ca04263b --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserDaoImpl.java @@ -0,0 +1,33 @@ +package com.baeldung.repositoryvsdaopattern; + +import javax.persistence.EntityManager; + +public class UserDaoImpl implements UserDao { + + private final EntityManager entityManager; + + public UserDaoImpl(EntityManager entityManager) { + this.entityManager = entityManager; + } + + @Override + public void create(User user) { + entityManager.persist(user); + } + + @Override + public User read(Long id) { + return entityManager.find(User.class, id); + } + + @Override + public void update(User user) { + + } + + @Override + public void delete(String userName) { + + } + +} diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepository.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepository.java new file mode 100644 index 0000000000..1da384958d --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepository.java @@ -0,0 +1,21 @@ +package com.baeldung.repositoryvsdaopattern; + +import java.util.List; + +public interface UserRepository { + + User get(Long id); + + void add(User user); + + void update(User user); + + void remove(User user); + + User findByUserName(String userName); + + User findByEmail(String email); + + List fetchTweets(User user); + +} diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java new file mode 100644 index 0000000000..6d7334c1ab --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java @@ -0,0 +1,50 @@ +package com.baeldung.repositoryvsdaopattern; + +import java.util.List; + +public class UserRepositoryImpl implements UserRepository { + + private UserDaoImpl userDaoImpl; + private TweetDaoImpl tweetDaoImpl; + + @Override + public User get(Long id) { + User user = userDaoImpl.read(id); + + List tweets = tweetDaoImpl.fetchTweets(user.getEmail()); + user.setTweets(tweets); + + return user; + } + + @Override + public void add(User user) { + userDaoImpl.create(user); + } + + @Override + public void remove(User user) { + + } + + @Override + public void update(User user) { + + } + + @Override + public List fetchTweets(User user) { + return null; + } + + @Override + public User findByUserName(String userName) { + return null; + } + + @Override + public User findByEmail(String email) { + return null; + } + +} From bc36751276ff2f2d9224cd180887b9e53ca95a16 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 12 Sep 2020 06:30:35 +0300 Subject: [PATCH 2/2] BAEL-4507 - UserSocialMedia class added --- .../baeldung/repositoryvsdaopattern/User.java | 12 ------------ .../UserRepositoryImpl.java | 5 +++-- .../repositoryvsdaopattern/UserSocialMedia.java | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserSocialMedia.java diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java index 86d3554f7e..8cd4fd0a00 100644 --- a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/User.java @@ -1,7 +1,5 @@ package com.baeldung.repositoryvsdaopattern; -import java.util.List; - public class User { private Long id; @@ -10,8 +8,6 @@ public class User { private String lastName; private String email; - private List tweets; - public Long getId() { return id; } @@ -51,13 +47,5 @@ public class User { public void setEmail(String email) { this.email = email; } - - public List getTweets() { - return tweets; - } - - public void setTweets(List tweets) { - this.tweets = tweets; - } } diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java index 6d7334c1ab..806b44e9d5 100644 --- a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserRepositoryImpl.java @@ -9,7 +9,7 @@ public class UserRepositoryImpl implements UserRepository { @Override public User get(Long id) { - User user = userDaoImpl.read(id); + UserSocialMedia user = (UserSocialMedia) userDaoImpl.read(id); List tweets = tweetDaoImpl.fetchTweets(user.getEmail()); user.setTweets(tweets); @@ -34,7 +34,8 @@ public class UserRepositoryImpl implements UserRepository { @Override public List fetchTweets(User user) { - return null; + return tweetDaoImpl.fetchTweets(user.getEmail()); + } @Override diff --git a/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserSocialMedia.java b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserSocialMedia.java new file mode 100644 index 0000000000..bf729620af --- /dev/null +++ b/patterns/design-patterns-architectural/src/main/java/com/baeldung/repositoryvsdaopattern/UserSocialMedia.java @@ -0,0 +1,17 @@ +package com.baeldung.repositoryvsdaopattern; + +import java.util.List; + +public class UserSocialMedia extends User { + + private List tweets; + + public List getTweets() { + return tweets; + } + + public void setTweets(List tweets) { + this.tweets = tweets; + } + +}