シングルトンオブジェクトの従業員、Crunchify Java POJO、および詳細なTestCaseを使用した完全なエンドツーエンドのJavaチュートリアル
公開: 2014-11-06 このJavaチュートリアルでは、エンタープライズプロジェクトでSingleton pattern
を使用するために必要なすべての詳細な手順について説明します。 シングルトンオブジェクトを使用して実行時に必要なデータをプッシュし、同じシングルトンオブジェクトを使用して実行時にデータを取得します。 シングルトンオブジェクトにはどこからでもアクセスできます。 シングルトンパターンについて詳しく知りたい場合は、このシングルトンチュートリアルに従ってください。
始めましょう
ステップ1
単純なCrunchifyオブジェクトの作成POJO: CrunchifyObject.java
ステップ2
すべてのセッターとゲッターを作成します。
ステップ-3
シングルトンクラスの作成: CrunchifySingleton.java
ステップ-4
すべてのユーティリティメソッドをシングルトンクラスに書き込みます
ステップ-5
最後に、テストクラスを作成します: CrunchifyObjectTest.java
以下のJSON依存関係が必要です。
1 2 3 4 5 |
< dependency > < groupId > org . json < / groupId > < artifactId > json < / artifactId > < version > 20151123 < / version > < / dependency > |
CrunchifyObject.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package crunchify . com . tutorial ; import java . util . List ; /** * @author Crunchify.com * This is simple CrunchifyObject POJO contains another class "Employee" as a field */ public class CrunchifyObject { public String name ; public String address ; public List <Employee> employees ; public String getName ( ) { return name ; } public void setName ( String name ) { this . name = name ; } public String getAddress ( ) { return address ; } public void setAddress ( String address ) { this . address = address ; } public List <Employee> getEmployees ( ) { return employees ; } public void setEmployees ( List <Employee> employees ) { this . employees = employees ; } // Class Employee public class Employee { public String firstName ; public String lastName ; public int phoneNumber ; 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 int getPhoneNumber ( ) { return phoneNumber ; } public void setPhoneNumber ( int phoneNumber ) { this . phoneNumber = phoneNumber ; } } } |
CrunchifySingleton.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package crunchify . com . tutorial ; import java . util . ArrayList ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; import org . json . JSONArray ; import org . json . JSONObject ; import crunchify . com . tutorial . CrunchifyObject . Employee ; /** * @author Crunchify.com * This is Singleton class used to access from any where from your project. Here we are using it * from CrunchifyObjectTest.java * */ public class CrunchifySingleton { private List <Employee> employees = new ArrayList <Employee> ( ) ; private static CrunchifySingleton singletonInstance = null ; private CrunchifyObject crunchify = new CrunchifyObject ( ) ; public static CrunchifySingleton getCrunchifySingletonInstance ( ) { if ( singletonInstance == null ) { singletonInstance = new CrunchifySingleton ( ) ; } return singletonInstance ; } public void addEmployee ( Employee employee ) { synchronized ( employees ) { employees . add ( employee ) ; } } public List <Employee> getEmployee ( ) { return employees ; } public void addCrunchify ( CrunchifyObject crunchify ) { synchronized ( crunchify ) { this . crunchify = crunchify ; } } public String getCrunchifyObject ( ) { JSONObject obj = new JSONObject ( ) ; obj . put ( "name" , crunchify . getName ( ) ) ; obj . put ( "address" , crunchify . getAddress ( ) ) ; JSONArray employeeJson = new JSONArray ( ) ; for ( Employee emp : employees ) { Map < String , String > employeeData = new HashMap < String , String > ( ) ; employeeData . put ( "firstName" , emp . getFirstName ( ) ) ; employeeData . put ( "lastName" , emp . getLastName ( ) ) ; employeeData . put ( "phoneNumber" , emp . getPhoneNumber ( ) + "" ) ; employeeJson . put ( employeeData ) ; } obj . put ( "employees" , employeeJson ) ; return obj . toString ( ) ; } } |

CrunchifyObjectTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package crunchify . com . tutorial ; import crunchify . com . tutorial . CrunchifyObject . Employee ; /** * @author Crunchify.com * This is test class used to create all required objects and use Singleton object to push and * retrieve value at runtime. You could use the same singleton object from anywhere in your enterprise project */ public class CrunchifyObjectTest { static CrunchifySingleton instance = CrunchifySingleton . getCrunchifySingletonInstance ( ) ; public static void main ( String [ ] args ) { populateAndSaveDataFromSingletonObject ( ) ; retrieveDataFromSingletonObject ( ) ; } private static void retrieveDataFromSingletonObject ( ) { String result = instance . getCrunchifyObject ( ) ; System . out . println ( result ) ; } private static void populateAndSaveDataFromSingletonObject ( ) { // Create CrunchifyObject CrunchifyObject crunchifyObj = new CrunchifyObject ( ) ; crunchifyObj . setName ( "Crunchify.com" ) ; crunchifyObj . setAddress ( "NYC - US" ) ; // Create Employee1 Employee emp1 = crunchifyObj . new Employee ( ) ; emp1 . setFirstName ( "Mike" ) ; emp1 . setLastName ( "Rose" ) ; emp1 . setPhoneNumber ( 1222222222 ) ; // Create Employee2 Employee emp2 = crunchifyObj . new Employee ( ) ; emp2 . setFirstName ( "app" ) ; emp2 . setLastName ( "shah" ) ; emp2 . setPhoneNumber ( 1333333333 ) ; // Create Employee3 Employee emp3 = crunchifyObj . new Employee ( ) ; emp3 . setFirstName ( "Dexter" ) ; emp3 . setLastName ( "My" ) ; emp3 . setPhoneNumber ( 1444444444 ) ; // Add all Employee Object to CrunchifySingleton object instance . addEmployee ( emp1 ) ; instance . addEmployee ( emp2 ) ; instance . addEmployee ( emp3 ) ; // Add Employees to CrunchifyObject crunchifyObj . setEmployees ( instance . getEmployee ( ) ) ; // Now finally add CrunchifyObject to same CrunchifySingleton object which will use to retrieve all data instance . addCrunchify ( crunchifyObj ) ; } } |
結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ "address" : "NYC - US" , "name" : "Crunchify.com" , "employees" : [ { "lastName" : "Rose" , "phoneNumber" : "1222222222" , "firstName" : "Mike" } , { "lastName" : "Shah" , "phoneNumber" : "1333333333" , "firstName" : "app" } , { "lastName" : "My" , "phoneNumber" : "1444444444" , "firstName" : "Dexter" } ] } |