shaico111 commited on
Commit
10e5fe7
·
verified ·
1 Parent(s): 3323e99

Upload batch 277 of 407

Browse files
Files changed (10) hide show
  1. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/Animal.java +9 -0
  2. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/AnimalFactory.java +24 -0
  3. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/Lion.java +20 -0
  4. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/PopulationSimulator.java +50 -0
  5. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/Tiger.java +19 -0
  6. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/Car.java +20 -0
  7. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/TrafficSimulator.java +51 -0
  8. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/Truck.java +20 -0
  9. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/Vehicle.java +8 -0
  10. שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Sol/Car.java +20 -0
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/Animal.java ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Animals;
2
+
3
+ public interface Animal {
4
+
5
+ String getAnimalType();
6
+ int[] getLocation();
7
+ void setLocation(int latitude, int longitude);
8
+
9
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/AnimalFactory.java ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Animals;
2
+
3
+ import java.util.HashMap;
4
+
5
+ public class AnimalFactory {
6
+
7
+ private HashMap<Integer, Animal> animals = new HashMap<>();
8
+
9
+ public Animal getAnimal(int type) {
10
+ if(animals.containsKey(type)) {
11
+ return animals.get(type);
12
+ } else {
13
+ Animal animal = null;
14
+ if(type == 0) {
15
+ animal = new Lion();
16
+ } else if(type == 1) {
17
+ animal = new Tiger();
18
+ }
19
+ animals.put(type, animal);
20
+ return animal;
21
+ }
22
+ }
23
+
24
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/Lion.java ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Animals;
2
+
3
+ public class Lion implements Animal {
4
+
5
+ private int[] location = new int[2];
6
+
7
+ public String getAnimalType() {
8
+ return "Lion";
9
+ }
10
+
11
+ public void setLocation(int latitude, int longitude) {
12
+ location[0] = latitude;
13
+ location[1] = longitude;
14
+ }
15
+
16
+ public int[] getLocation() {
17
+ return location;
18
+ }
19
+
20
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/PopulationSimulator.java ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Animals;
2
+
3
+ import java.util.ArrayList;
4
+ import java.util.*;
5
+ import java.util.Random;
6
+ import java.util.concurrent.Executors;
7
+ import java.util.concurrent.ScheduledExecutorService;
8
+ import java.util.concurrent.TimeUnit;
9
+
10
+ public class PopulationSimulator {
11
+
12
+ static List<Animal> animals = new ArrayList();
13
+ private static AnimalFactory animalFactory = new AnimalFactory();
14
+
15
+ public static void main(String[] args) {
16
+
17
+ Runnable createAnimals = new Runnable() {
18
+ public void run() {
19
+ createRandomAnimal();
20
+ }
21
+ };
22
+
23
+ Runnable removeAnimals = new Runnable() {
24
+ public void run() {
25
+ removeAnimal();
26
+ }
27
+ };
28
+
29
+ ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
30
+ executor.scheduleAtFixedRate(createAnimals, 0, 3, TimeUnit.SECONDS);
31
+ executor.scheduleAtFixedRate(removeAnimals, 5, 5, TimeUnit.SECONDS);
32
+ }
33
+
34
+ private static void createRandomAnimal() {
35
+ Random random = new Random();
36
+ int randInt = random.nextInt(2);
37
+
38
+ Animal animal = animalFactory.getAnimal(randInt);
39
+ animal.setLocation(random.nextInt(1000), random.nextInt(1000));
40
+ System.out.println("Creating " + animal + ", type: " + animal.getAnimalType() +
41
+ ", location: " + animal.getLocation()[0] + " " + animal.getLocation()[1]);
42
+ animals.add(animal);
43
+ }
44
+
45
+ private static void removeAnimal() {
46
+ System.out.println("Removing " + animals.get(0));
47
+ animals.remove(0);
48
+ }
49
+
50
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Animals/Tiger.java ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Animals;
2
+
3
+ public class Tiger implements Animal {
4
+
5
+ private int[] location = new int[2];
6
+
7
+ public String getAnimalType() {
8
+ return "Tiger";
9
+ }
10
+
11
+ public void setLocation(int latitude, int longitude) {
12
+ location[0] = latitude;
13
+ location[1] = longitude;
14
+ }
15
+
16
+ public int[] getLocation() {
17
+ return location;
18
+ }
19
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/Car.java ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Prob;
2
+
3
+ public class Car implements Vehicle {
4
+
5
+ private int[] location = new int[2];
6
+
7
+ public String getType() {
8
+ return "Car";
9
+ }
10
+
11
+ public void setLocation(int latitude, int longitude) {
12
+ location[0] = latitude;
13
+ location[1] = longitude;
14
+ }
15
+
16
+ public int[] getLocation() {
17
+ return location;
18
+ }
19
+
20
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/TrafficSimulator.java ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Prob;
2
+
3
+ import java.util.*;
4
+ import java.util.concurrent.Executors;
5
+ import java.util.concurrent.ScheduledExecutorService;
6
+ import java.util.concurrent.TimeUnit;
7
+
8
+ public class TrafficSimulator {
9
+
10
+ static List<Vehicle> vehiclesList = new ArrayList<Vehicle>();
11
+
12
+ public static void main(String[] args) {
13
+
14
+ Runnable createVehicles = new Runnable() {
15
+ public void run() {
16
+ createRandomCar();
17
+ }
18
+ };
19
+
20
+ Runnable removeVehicles = new Runnable() {
21
+ public void run() {
22
+ removeCar();
23
+ }
24
+ };
25
+
26
+ ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
27
+ executor.scheduleAtFixedRate(createVehicles, 0, 3, TimeUnit.SECONDS);
28
+ executor.scheduleAtFixedRate(removeVehicles, 5, 5, TimeUnit.SECONDS);
29
+ }
30
+
31
+ private static void createRandomCar() {
32
+ Random random = new Random();
33
+ int randInt = random.nextInt(2);
34
+ Vehicle vehicle = null;
35
+ if(randInt == 0) {
36
+ vehicle = new Car();
37
+ } else if(randInt == 1) {
38
+ vehicle = new Truck();
39
+ }
40
+ vehicle.setLocation(random.nextInt(1000), random.nextInt(1000));
41
+ System.out.println("Creating " + vehicle + ", type: " + vehicle.getType() +
42
+ ", location: " + vehicle.getLocation()[0] + " " + vehicle.getLocation()[1]);
43
+ vehiclesList.add(vehicle);
44
+ }
45
+
46
+ private static void removeCar() {
47
+ System.out.println("Removing " + vehiclesList.get(0));
48
+ vehiclesList.remove(0);
49
+ }
50
+
51
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/Truck.java ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Prob;
2
+
3
+ public class Truck implements Vehicle {
4
+
5
+ private int[] location = new int[2];
6
+
7
+ public String getType() {
8
+ return "Truck";
9
+ }
10
+
11
+ public void setLocation(int latitude, int longitude) {
12
+ location[0] = latitude;
13
+ location[1] = longitude;
14
+ }
15
+
16
+ public int[] getLocation() {
17
+ return location;
18
+ }
19
+
20
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Prob/Vehicle.java ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Prob;
2
+
3
+ public interface Vehicle {
4
+
5
+ String getType();
6
+ int[] getLocation();
7
+ void setLocation(int latitude, int longitude);
8
+ }
שנה ב/סמסטר ב/הנדסת תוכנה/תרגילי כיתה/תרגול 8-9 - דוג_ קוד של המרצה ל-Design Patterns/Flyweight_Sol/Car.java ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package JCEOODCourse.DesignPatterns.Flyweight_Sol;
2
+
3
+ public class Car implements Vehicle {
4
+
5
+ private int[] location = new int[2];
6
+
7
+ public String getModel() {
8
+ return "Car";
9
+ }
10
+
11
+ public void setLocation(int latitude, int longitude) {
12
+ location[0] = latitude;
13
+ location[1] = longitude;
14
+ }
15
+
16
+ public int[] getLocation() {
17
+ return location;
18
+ }
19
+
20
+ }