Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Data_Transfer_Hash/etc/dataTransferHash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions Data_Transfer_Hash/etc/dataTransferHash.puml
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
16 changes: 16 additions & 0 deletions Data_Transfer_Hash/java-design-patterns.iml
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>
75 changes: 75 additions & 0 deletions Data_Transfer_Hash/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Owner

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


<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
Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The 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>
75 changes: 75 additions & 0 deletions Data_Transfer_Hash/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
layout: pattern
title: data transfer hash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: data transfer hash
title: Data Transfer Hash

folder: Data_Transfer_Hash
permalink: /patterns/Data_Transfer_Hash/
categories: Architectural
tags:
- Performance
Comment on lines +6 to +8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

---

## Also known as

* data transfer hash
Comment on lines +11 to +13
Copy link
Owner

Choose a reason for hiding this comment

The 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.
Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


**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

31 changes: 31 additions & 0 deletions Data_Transfer_Hash/src/main/java/Data_Transfer_Hash/App.java
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
Copy link
Owner

Choose a reason for hiding this comment

The 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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use var in many occasions, check throughout the example

Business business=new Business();
Presentation presentation=new Presentation();
business.create("a", "aaa", hash);
Copy link
Owner

Choose a reason for hiding this comment

The 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");
Copy link
Owner

Choose a reason for hiding this comment

The 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));



}


}
16 changes: 16 additions & 0 deletions Data_Transfer_Hash/src/main/java/Data_Transfer_Hash/Business.java
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)
Copy link
Owner

Choose a reason for hiding this comment

The 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider naming

{


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra spaces

return hash.get(k);
}
}
19 changes: 19 additions & 0 deletions Data_Transfer_Hash/src/main/java/Data_Transfer_Hash/Hash.java
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would put be a better name?

{
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);

}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the extra line spaces

}
46 changes: 46 additions & 0 deletions Data_Transfer_Hash/src/test/java/Data_Transfer_Hash/AppTest.java
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");
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.