Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. interface IPolaczenie {
  2.  
  3. char get(int indeks);
  4. void set(int indeks, char c);
  5. int length();
  6.  
  7. }
  8.  
  9. public class Baza {
  10.  
  11. private char[] tab = new char[100];
  12.  
  13. /* ... */
  14.  
  15. private Baza(){}
  16.  
  17. private static Baza instance = new Baza();
  18.  
  19. public static Baza getInstance() { return instance; }
  20.  
  21. public static IPolaczenie getPolaczenie() {
  22. return Polaczenie.getInstance();
  23. }
  24.  
  25. private static class Polaczenie implements IPolaczenie {
  26.  
  27. private Baza baza;
  28.  
  29. /* ... */
  30.  
  31. private static int i = 0;
  32.  
  33. private static Polaczenie[] polaczenia = { new Polaczenie(), new Polaczenie(), new Polaczenie() };
  34.  
  35. private Polaczenie(){
  36. baza = Baza.getInstance();
  37. }
  38.  
  39. public static IPolaczenie getInstance() {
  40.  
  41. /* ... */
  42.  
  43. i = (i+1) % polaczenia.length;
  44. return polaczenia[i];
  45.  
  46. }
  47.  
  48. public char get(int indeks) {
  49. return baza.tab[indeks];
  50. }
  51.  
  52. public void set(int indeks, char c) {
  53. baza.tab[indeks] = c;
  54. }
  55.  
  56. public int length() {
  57. return baza.tab.length;
  58. }
  59. }
  60.  
  61. public static void main(String[] args) {
  62.  
  63. Baza b = Baza.getInstance();
  64.  
  65. IPolaczenie p1 = Baza.getPolaczenie();
  66. IPolaczenie p2 = Baza.getPolaczenie();
  67. IPolaczenie p3 = Baza.getPolaczenie();
  68. IPolaczenie p4 = Baza.getPolaczenie();
  69.  
  70. System.out.println("p1: " + p1.length());
  71. System.out.println("p2: " + p2.length());
  72. System.out.println("p3: " + p3.length());
  73. System.out.println("p4: " + p4.length());
  74.  
  75. p1.set(0, 'c');
  76.  
  77. System.out.println();
  78. System.out.println("p1: " + p1.get(0));
  79. System.out.println("p2: " + p2.get(0));
  80. System.out.println("p3: " + p3.get(0));
  81. System.out.println("p4: " + p4.get(0));
  82.  
  83. System.out.println();
  84. System.out.println("p1: " + p1);
  85. System.out.println("p4: " + p4);
  86.  
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement