-
-
Notifications
You must be signed in to change notification settings - Fork 26.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
data transfer hash(issue 1269) #1704
Changes from 2 commits
079f7a2
d8da32a
ed579ba
4bdabd3
94bca2c
9fee9e1
2c2114c
7b92358
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@startuml | ||
|
||
Class data_transfer_hash { | ||
+key | ||
-value | ||
+getVelue(key) | ||
} | ||
class Business_objects | ||
class Presentation_tier_objects | ||
data_transfer_hash<--Business_objects : create or use | ||
data_transfer_hash<--Presentation_tier_objects : create or use | ||
@enduml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.iluwatar</groupId> | ||
<artifactId>java-design-patterns</artifactId> | ||
<version>1.24.0-SNAPSHOT</version> | ||
|
||
<name>Data_Transfer_Hash</name> | ||
<!-- FIXME change it to the project's website --> | ||
<url>http://www.example.com</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
Comment on lines
+11
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this part |
||
|
||
<build> | ||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> | ||
<plugins> | ||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> | ||
<plugin> | ||
<artifactId>maven-clean-plugin</artifactId> | ||
<version>3.1.0</version> | ||
</plugin> | ||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>2.5.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>2.8.2</version> | ||
</plugin> | ||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> | ||
<plugin> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.7.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-project-info-reports-plugin</artifactId> | ||
<version>3.0.0</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
Comment on lines
+30
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many of the plugins are already specified in the parent pom.xml. Check how the other pattern pom.xmls have been defined e.g. https://github.com/iluwatar/java-design-patterns/blob/master/abstract-factory/pom.xml |
||
</build> | ||
</project> |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,75 @@ | ||||||||
--- | ||||||||
layout: pattern | ||||||||
title: data transfer hash | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
folder: Data_Transfer_Hash | ||||||||
permalink: /patterns/Data_Transfer_Hash/ | ||||||||
categories: Architectural | ||||||||
tags: | ||||||||
- Performance | ||||||||
Comment on lines
+6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest structural and decoupling. See https://github.com/iluwatar/java-design-patterns/wiki/07.-Categories-and-Tags |
||||||||
--- | ||||||||
|
||||||||
## Also known as | ||||||||
|
||||||||
* data transfer hash | ||||||||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the original pattern name, right? No need to specify it here. |
||||||||
|
||||||||
## Intent | ||||||||
|
||||||||
Providing a class called data transfer hash, in order to send message between other class. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intent should describe the pattern very clearly and concisely. Please try to rephrase. |
||||||||
|
||||||||
## Explanation | ||||||||
|
||||||||
|
||||||||
> Data transfer hashes may be implemented as simple hash tables (or HashMaps). | ||||||||
A more robust implementation uses a container object to hold the hash, as well as identifying information, type-safe data retrieval methods, and well-known keys. | ||||||||
> https://www.oreilly.com/library/view/j2ee-design-patterns/0596004273/re12.html | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the link to another section, see https://github.com/iluwatar/java-design-patterns/wiki/02.-Pattern-template |
||||||||
|
||||||||
**Programmatic Example** | ||||||||
|
||||||||
We have an function `create` and `get`. | ||||||||
|
||||||||
```java | ||||||||
public static void main( String[] args ) | ||||||||
{ | ||||||||
Hash hash= new Hash(); | ||||||||
Business business=new Business(); | ||||||||
Presentation presentation=new Presentation(); | ||||||||
business.create("a", "aaa", hash); | ||||||||
System.out.println("Business create "+"aaa"); | ||||||||
business.create("b", "bbb", hash); | ||||||||
System.out.println("Business create "+"bbb"); | ||||||||
business.create("c", "ccc", hash); | ||||||||
System.out.println("Business create "+"ccc"); | ||||||||
|
||||||||
presentation.get("a", hash); | ||||||||
System.out.println("Presentation heard "+presentation.get("a", hash)); | ||||||||
presentation.get("b", hash); | ||||||||
System.out.println("Presentation heard "+presentation.get("b", hash)); | ||||||||
} | ||||||||
|
||||||||
``` | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
Program output: | ||||||||
|
||||||||
```java | ||||||||
Business create aaa | ||||||||
Business create bbb | ||||||||
Business create ccc | ||||||||
Presentation heard aaa | ||||||||
Presentation heard bbb | ||||||||
``` | ||||||||
|
||||||||
## Class Diagram | ||||||||
|
||||||||
![alt text](./etc/dataTransferHash.png "data Transfer Hash pattern class diagram") | ||||||||
|
||||||||
## Applicability | ||||||||
|
||||||||
Use the data transfer hash pattern when you only care about the creation of a object, not how to create | ||||||||
and manage it. | ||||||||
|
||||||||
* Not only string but other types canbe value but key must be string | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package Data_Transfer_Hash; | ||
/* | ||
Data transfer hashes may be implemented as simple hash tables (or HashMaps). | ||
A more robust implementation uses a container object to hold the hash, as well as identifying information, type-safe data retrieval methods, and well-known keys. | ||
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would first explain what the pattern is, then how this example implements it |
||
*/ | ||
|
||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
Hash hash= new Hash(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
Business business=new Business(); | ||
Presentation presentation=new Presentation(); | ||
business.create("a", "aaa", hash); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use some time to come up with a more interesting example than 'a' and 'aaa' |
||
System.out.println("Business create "+"aaa"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a proper logger, check elsewhere too |
||
business.create("b", "bbb", hash); | ||
System.out.println("Business create "+"bbb"); | ||
business.create("c", "ccc", hash); | ||
System.out.println("Business create "+"ccc"); | ||
|
||
presentation.get("a", hash); | ||
System.out.println("Presentation heard "+presentation.get("a", hash)); | ||
presentation.get("b", hash); | ||
System.out.println("Presentation heard "+presentation.get("b", hash)); | ||
|
||
|
||
|
||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package Data_Transfer_Hash; | ||
/*Business objects | ||
Create hashes for sending data, or use values in received hashes.*/ | ||
public class Business { | ||
public void create(String k,Object v,Hash hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a more descriptive name for the method |
||
{ | ||
hash.create(k, v); | ||
|
||
} | ||
public Object get(String k,Hash hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider naming |
||
{ | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extra spaces |
||
return hash.get(k); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package Data_Transfer_Hash; | ||
|
||
import java.util.Hashtable; | ||
/*Data transfer hash | ||
Stores data for transport between layers as a set of values associated with well-known keys.*/ | ||
public class Hash { | ||
private Hashtable hash; | ||
Hash() | ||
{hash=new Hashtable();} | ||
public void create(String k,Object v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would |
||
{ | ||
this.hash.put(k, v); | ||
} | ||
public Object get(String k) | ||
{ | ||
return this.hash.get(k); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package Data_Transfer_Hash; | ||
/*Presentation tier objects | ||
Create hashes for sending data, or use values in received hashes. */ | ||
public class Presentation { | ||
public void create(String k,Object v,Hash hash) | ||
{ | ||
hash.create(k, v); | ||
|
||
} | ||
public Object get(String k,Hash hash) | ||
{ | ||
|
||
return hash.get(k); | ||
|
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove the extra line spaces |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package Data_Transfer_Hash; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class AppTest | ||
{ | ||
@Test | ||
public void getTrueCvreate() | ||
{ | ||
Hash hash= new Hash(); | ||
Business business=new Business(); | ||
business.create("a", "aaa", hash); | ||
|
||
} | ||
@Test | ||
public void getTrueAnswer() | ||
{ | ||
Hash hash= new Hash(); | ||
Business business=new Business(); | ||
Presentation presentation=new Presentation(); | ||
business.create("a", "aaa", hash); | ||
business.create("b", "bbb", hash); | ||
business.create("c", "ccc", hash); | ||
|
||
assertEquals(presentation.get("a", hash),"aaa"); | ||
assertEquals(presentation.get("b", hash),"bbb"); | ||
|
||
} | ||
@Test | ||
public void getTrueAnswerAfterChange() | ||
{ | ||
Hash hash= new Hash(); | ||
Business business=new Business(); | ||
Presentation presentation=new Presentation(); | ||
business.create("a", "aaa", hash); | ||
business.create("b", "bbb", hash); | ||
business.create("c", "ccc", hash); | ||
|
||
assertEquals(presentation.get("a", hash),"aaa"); | ||
assertEquals(presentation.get("b", hash),"bbb"); | ||
business.create("a", "abc", hash); | ||
assertEquals(presentation.get("a", hash),"abc"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Folder naming uses snake case. Data_Transfer_Hash -> data_transfer_hash