Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e4cd1e

Browse files
fly weight pattern
design with handling tags in database
1 parent 28cf773 commit 1e4cd1e

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

‎design-pattern/flyweight/HashTag.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sdp.flyweight;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class HashTag implements Tag{
8+
private String tagName;
9+
private int primaryKey;
10+
11+
public HashTag(String tagName, int primaryKey){
12+
this.tagName = tagName;
13+
this.primaryKey = primaryKey;
14+
}
15+
16+
@Override
17+
public String name(){
18+
return tagName;
19+
}
20+
21+
@Override
22+
public int id(){
23+
return primaryKey;
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package sdp.flyweight;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
*
7+
* @author rafiul islam
8+
*/
9+
public class HashTagTable {
10+
private static int autoPrimaryKey = 1;
11+
private static HashMap table = new HashMap();
12+
13+
public static HashTag getOrCreate(String tag){
14+
tag = tag.toLowerCase();
15+
HashTag hashTag = (HashTag) table.get(tag);
16+
17+
if(hashTag == null){
18+
hashTag = new HashTag(tag, autoPrimaryKey++);
19+
table.put(tag, hashTag);
20+
}
21+
return hashTag;
22+
}
23+
}

‎design-pattern/flyweight/Main.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sdp.flyweight;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public class Main {
8+
public static void main(String[] args) {
9+
String[] tags = {"endgame","EndGame", "avengers", "Avengers", "ENDGAME"};
10+
11+
HashTag twitterHashTag;
12+
for(String item : tags){
13+
twitterHashTag = HashTagTable.getOrCreate(item);
14+
System.out.println(twitterHashTag.id()+" : "+twitterHashTag.name());
15+
}
16+
}
17+
}

‎design-pattern/flyweight/Tag.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package sdp.flyweight;
2+
3+
/**
4+
*
5+
* @author rafiul islam
6+
*/
7+
public interface Tag {
8+
public String name();
9+
public int id();
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /