The Social Network Java Project
is a training project that aims to help
students improve their Java programming skills and learn new programming
concepts and techniques.
This tutorial is divided into chapters. Each one of them will implement a part of the project and let you learn a unique programming technique.
This tutorial is managed with Git which means that you can go through the different commits to lean about the different steps and development decisions. Each commit is self-documented.
This project uses Maven as a build system and Eclipse as a main IDE.
The following is a list of instructions that let you create a new Maven project using Eclipse:
- Click
File
>New
>Project...
- In the
New Project
wizard:- Click
Maven
>Maven Project
. ClickNext
. - In the
New Maven Project
window, checkCreate a simple project (...)
and the clickNext
. - Specify the
groupId
in theGroup Id
field.The
groupId
uniquely identifies your project across all projects. A group ID should follow Java's package name rules. This means it starts with a reversed domain name you control. For example,org.apache.maven
,org.apache.commons
For more details, see Maven naming conventions
- Specify the
artifactId
in theArtifact Id
field.The
artifactId
is the name of the jar without version. If you created it, then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar, you have to take the name of the jar as it's distributed. eg.maven
,commons-math
For more details, see Maven naming conventions
- Keep the default version name and click
Finish
.
- Click
The best way to import this Git project into Eclipse is to import it first as a
General Git Project
and then convert it to Maven Project
:
Import as a General Git Project:
- Click
File
>Import...
- In the
Import
wizard:- Click
Git
>Projects from Git
. ClickNext
. - Click
Clone URI
and then clickNext
. - In the
Source Git Repository
window, in theURI
field, enter an existing Git repository URL and clickNext
. - In the
Branch Selection
window, make sure that themaster
branch is selected and clickNext
. - In the
Local Destination
window, specify the destination directory or keep the default value and then clickNext
.
TheCloning from <Git repository>
window shows the progress of the cloning process. - In the menu
Wizard of project import
, selectImport as general project
. ClickNext
. - In the
Import Projects
window, confirm or edit the default parameters and clickFinish
.
- Click
Convert into Maven Project:
- In the projects tree, right click on the imported project.
- Scroll down and click
Configure
>Convert to Maven Project
.
In this chapter, we are going to implement the Contacts management
module.
We'll start by declaring the different necessary entities (classes, interfaces,
enumerations). This list will be enriched with further entities while developing
the other modules of this project.
The implementation of our project should follows the Google Coding Style. We can also find some details about that coding style in a document written by Twitter as part of its Java common libraries
- Choosing the right collections
- Always using interface type when declaring a collection.
- Use generic type and diamond operator
- Specify initial capacity of a collection if possible
- Prefer
isEmpty()
oversize()
- Do not use the classic for loop
- etc
For more details about Collections and Generics Best Practices
class User implements ContactItem:
login
password
firstName
lastName
dateOfBirth: Date
gender: Gender
photos: List<Photo>
locations: List<Location>
emails: List<Email>
phoneNumbers: List<PhoneNumber>
contacts: List<ContactItem>
enum Gender:
MALE, FEMALE
class Photo:
uri: URI
title
class Location:
country
address
label
class Email:
address
label
class PhoneNumber:
countryCallingCode: int
number: long
label
interface ContactItem
class Group implements ContactItem:
name
description
contacts: List<ContactItem>
It's a good practice to follow the MVC design pattern, so it's better that
we move the different entities in a dedicated package entities
.
You can find a basic implement of the MVC design pattern here:
https://www.geeksforgeeks.org/mvc-design-pattern
The repository pattern abstracts the data store and enables your business logic
to define read and write operations on a logical level.
So we'll dedicated a repositories
package to contain all repository classes.
The singleton design pattern restricts the instantiation of a class to one
"single" instance. This is useful when exactly one unique object is needed
across the whole system.
You can find more details and an example of the
implementation of the singleton design pattern here:
https://www.geeksforgeeks.org/singleton-class-java .
This pattern will be useful for the repository classes. Let's start by the
UserRepository
class.
This project is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0). See COPYRIGHT, LICENSE-APACHE, LICENSE-MIT, DOC-LICENSE, AUTHORS and CONTRIBUTORS for more details.