Имеется интерфейс DAO и его реализация MemoryDAO. Для того, чтобы скрыть от пользователя конечную реализацию, нужно создать DAOFactory по паттерну Factory. Как это сделать?
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package onlinestore.core.dao;
import onlinestore.core.entities.AbstractEntity;
import onlinestore.core.entities.User;
public interface DAO
{
void save(AbstractEntity o);
Object findById(long id, Class objectClass);
void remove(AbstractEntity o);
// Методы для User
User findUserByLogin(String name);
} |
|
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| package onlinestore.core.dao;
import onlinestore.core.entities.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MemoryDAO implements DAO
{
private static Map users = new HashMap();
private static Map categories = new HashMap();
private static Map items = new HashMap();
private static Map orders = new HashMap();
public void save(AbstractEntity o)
{
boolean savePermission = true;
if (o instanceof User)
{
// Проверяем на уникальность login:
User user = findUserByLogin(((User) o).getLogin());
if (user != null)
{
users.put(((AbstractEntity) o).getId(), o);
}
}
if (o instanceof Category)
{
categories.put(((AbstractEntity) o).getId(), o);
}
if (o instanceof Item)
{
items.put(((AbstractEntity) o).getId(), o);
}
if (o instanceof Order)
{
orders.put(((AbstractEntity) o).getId(), o);
}
}
public Object findById(long id, Class objectClass)
{
if (User.class.equals(objectClass))
{
return users.get(id);
}
if (Category.class.equals(objectClass))
{
return categories.get(id);
}
if (Item.class.equals(objectClass))
{
return items.get(id);
}
if (Order.class.equals(objectClass))
{
return orders.get(id);
}
return null;
}
public void remove(AbstractEntity o)
{
if (o instanceof User)
{
users.remove(((User) o).getId());
}
if (o instanceof Category)
{
categories.remove(((Category) o).getId());
}
if (o instanceof Item)
{
items.remove(((Item) o).getId());
}
if (o instanceof Order)
{
orders.remove(((Order) o).getId());
}
}
// Методы для User
public User findUserByLogin(String name)
{
Iterator it = users.values().iterator();
while (it.hasNext())
{
User user = (User) it.next();
if (user.getLogin().equals(name))
{
return user;
}
}
return null;
}
} |
|
Добавлено через 2 часа 21 минуту
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| public class DAOFactory
{
// Список DAO типов, поддерживаемых Factory
public static int MemoryDAO = 1;
public static DAOFactory getDAOFactory(int whichFactory)
{
switch(whichFactory)
{
case 1:
return new MemoryDAO();
break;
default
return null;
}
}
} |
|
return new MemoryDAO(); - эта строчка выдает ошибку. Пишет, что требуется класс DAOFactory, а мы имеем класс MemoryDAO. Как исправить?