1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef ENTITY_FACTORY_H
16 #define ENTITY_FACTORY_H
17 
18 #include <functional>
19 
20 #include "data/inner_link.h"
21 #include "entity/p2p_entity.h"
22 #include "entity/wifi_direct_entity.h"
23 
24 namespace OHOS::SoftBus {
25 class EntityFactory {
26 public:
GetInstance()27     static EntityFactory& GetInstance()
28     {
29         static EntityFactory instance;
30         return instance;
31     }
32 
33     using Creator = std::function<WifiDirectEntity&(InnerLink::LinkType)>;
Register(const Creator & creator)34     void Register(const Creator &creator)
35     {
36         creator_ = creator;
37     }
38 
GetEntity(InnerLink::LinkType type)39     WifiDirectEntity& GetEntity(InnerLink::LinkType type)
40     {
41         if (creator_ == nullptr) {
42             return P2pEntity::GetInstance();
43         }
44         return creator_(type);
45     }
46 
47 private:
48     EntityFactory() = default;
49     Creator creator_;
50 };
51 }
52 #endif
53