Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. CompositeFoo compFoo = new CompositeFoo(fooDB, fooFILE, fooLIB)
  2.  
  3. import java.util.Collections;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6.  
  7. //interface used for composite class and data classes
  8. public abstract class Foo {
  9. abstract Set<String> getIds();
  10. abstract void addId(String id);
  11. }
  12.  
  13. //data source from file
  14. class FooFromFile extends Foo {
  15.  
  16. @Override
  17. Set<String> getIds() {
  18. //loads data from file
  19. }
  20.  
  21. @Override
  22. void addId(String id) {
  23. //changes file, add there one id
  24. }
  25. }
  26.  
  27. //data source from DB
  28. class FooFromDB extends Foo {
  29.  
  30. @Override
  31. Set<String> getIds() {
  32. //loads ids from DataBase
  33. }
  34.  
  35. @Override
  36. void addId(String id) {
  37. //adds id to database
  38. }
  39. }
  40.  
  41. //class i use now, combines all Foo's
  42. //gets info from all, updates only main data source
  43. //this one i want to make generic
  44. class CompositeFoo extends Foo {
  45. private Foo mainFoo;
  46. private Set<Foo> others;
  47.  
  48. public CompositeFoo(Foo mainSource, Foo ...otherSources ) {
  49. mainFoo = mainSource;
  50. others = new HashSet<>();
  51. Collections.addAll(others, otherSources);
  52. }
  53.  
  54. @Override
  55. Set<String> getIds() {
  56. HashSet<String> result = new HashSet<>();
  57. //load from main source
  58. result.addAll(mainFoo.getIds());
  59. //and from other sources
  60. for (Foo otherFoo : others ) {
  61. result.addAll(otherFoo.getIds());
  62. }
  63. //ids from all the sources
  64. return result;
  65. }
  66.  
  67. @Override
  68. void addId(String id) {
  69. //uses only main source, we don't want to add ID to all the sources
  70. mainFoo.addId(id);
  71. }
  72. }
  73.  
  74. public static void main(String[] args) {
  75. FooFromDB dbFooMain = new FooFromDB();
  76. FooFromFile fileFoo = new FooFromFile();
  77. CompositeFoo compFoo= new CompositeFoo(dbFooMain, fileFoo);
  78.  
  79. compFoo.add("new id"); //adds to dbFooMain only
  80. compFoo.getIds(); //gets from all sources (dbFooMain + fileFoo)
  81. }
  82.  
  83. public static void main(String[] args) {
  84. FooFromDB dbFooMain = new FooFromDB();
  85. FooFromFile fileFoo = new FooFromFile();
  86.  
  87. //something like this:
  88. MagicComposite<Foo> compFoo = new MagicComposite<>(dbFooMain, fileFoo);
  89. //or something like this:
  90. Foo compFoo = AnyCompositeFactory.createComposite(Foo.class, dbFoo, fileFoo);
  91.  
  92. //than this should work
  93. //adds to dbFooMain only. dbFoo.add("new id") called
  94. compFoo.add("new id");
  95.  
  96. //gets ids from all sources so dbFoo.getIds() + fileFoo.getIds() called and combined
  97. compFoo.getIds();
  98.  
  99.  
  100. //-----------------------------------------
  101. //for Bar the same magic class should do
  102. MagicComposite<Bar> compBar = new MagicComposite<>(dbBarMain, fileBar);
  103. //or
  104. Bar compBar = AnyCompositeFactory.createComposite(Bar.class, dbBar, fileBar);
  105. }
  106.  
  107. abstract class Bar{
  108.  
  109. @Magic(Type = retrieveInformationOnly)
  110. Set<String> getSomething();
  111.  
  112. @Magic(Type = addInformation)
  113. void addSomething(String id);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement