libaurum: Applying strategy pattern for OCP 22/304922/2
authorWoochan Lee <wc0917.lee@samsung.com>
Thu, 25 Jan 2024 12:22:35 +0000 (21:22 +0900)
committerwoochan lee <wc0917.lee@samsung.com>
Fri, 26 Jan 2024 06:54:02 +0000 (06:54 +0000)
Existing code was violating the SOLID principle OCP by being closed for additional events.

To solve this problem, the strategy pattern was applied so that even if new events were added,
the existing business logic remained unchanged and the structure was improved so that new classes could be added.

Change-Id: I386ef353d9a0427dc8e32a89fb1e386a30bafc76

libaurum/inc/Accessibility/AccessibleNode.h
libaurum/inc/Accessibility/AccessibleWatcher.h
libaurum/inc/Accessibility/IEventConsumer.h
libaurum/inc/Accessibility/IEventSource.h
libaurum/src/Accessibility/AccessibleNode.cc
libaurum/src/Accessibility/AccessibleWatcher.cc
libaurum/src/Impl/Accessibility/AtspiAccessibleWatcher.cc

index 16f7c898bc84d6af19143ac965995212d21e1ac8..26521db8122502f136dbd12a867cc961f9d24292 100644 (file)
@@ -152,7 +152,7 @@ public:
      *
      * @since_tizen 6.5
      */
-    void notify(int type, int type2, void *src) override;
+    void notify(EventType type, void *src) override;
 
     /**
      * @brief Changes Node state to invalidate.
index 4b4585b85e3139930d6e54152c4bf007321610e9..e25bde061c08f41aff1536257cb270ef567fb84f 100644 (file)
@@ -146,7 +146,7 @@ public:
     /**
      * @copydoc @IEventSource::notifyAll()
      */
-    void notifyAll(int type1, int type2, void *src) override;
+    void notifyAll(EventType type, void *src) override;
 
 private:
     std::set<std::shared_ptr<IEventConsumer>> mSources;
index 5ed09159b5703b1804d48233c376c74203441dec..d7a35e417de7bc7878d3d267a1516e522ce166d3 100644 (file)
@@ -29,19 +29,7 @@ namespace Aurum {
  */
 enum class EventType {
     none = 0,
-    Object = 1,
-};
-
-/**
- * @class ObjectEventType
- *
- * @ingroup aurum
- *
- * @brief Enum class for Object Event Type.
- */
-enum class ObjectEventType {
-    none = 0,
-    ObjectStateDefunct = 1,
+    ObjectDefunct = 1,
 };
 
 /**
@@ -64,13 +52,12 @@ public:
     /**
      * @brief Notifies event to source node.
      *
-     * @param[in] type1 @EventType
-     * @param[in] type2 @ObjectEventType
+     * @param[in] type @EventType
      * @param[in] src source Node ptr
      *
      * @since_tizen 6.5
      */
-    virtual void notify(int type1, int type2, void *src) = 0;
+    virtual void notify(EventType type, void *src) = 0;
 };
 
 }
index f3cf2d7230473743f30d4b0ffd50d45d0d895c7f..a55fb60d35b8be95acde16d4e521331d228569c9 100644 (file)
@@ -61,13 +61,12 @@ public:
     /**
      * @brief Notifies all attached event.
      *
-     * @param[in] type1 @EventType
-     * @param[in] type2 @ObjectEventType
+     * @param[in] type @EventType
      * @param[in] src source Node ptr
      *
      * @since_tizen 6.5
      */
-    virtual void notifyAll(int type1, int type2, void *src) = 0;
+    virtual void notifyAll(EventType type, void *src) = 0;
 };
 
 }
index abfed4ad033f53f4798795b8b237d94bd820d777..e2d0539b0fa325ec0df5b5f97322f76c27d3cbdf 100644 (file)
 
 using namespace Aurum;
 
+class Event {
+public:
+
+    Event(std::shared_ptr<AccessibleNode> node)
+    : mNode{node} {}
+
+    virtual ~Event(){};
+    virtual void handleEvent() = 0;
+protected:
+
+    std::shared_ptr<AccessibleNode> mNode;
+};
+
+class ObjectDefunctEvent : public Event {
+public:
+
+    ObjectDefunctEvent(std::shared_ptr<AccessibleNode> node)
+    : Event{node} {}
+
+    virtual ~ObjectDefunctEvent(){};
+    void handleEvent() override {
+        mNode->invalidate();
+    }
+};
+
 AccessibleNode::~AccessibleNode()
 {
 }
@@ -53,13 +78,15 @@ std::string AccessibleNode::description() {
     return ss.str();
 }
 
-void AccessibleNode::notify(int type1, int type2, void *src)
+void AccessibleNode::notify(EventType type, void *src)
 {
-    void *handler = getRawHandler();
+    if (src != getRawHandler()) return;
 
-    if ((EventType)type1 == EventType::Object && (ObjectEventType)type2 == ObjectEventType::ObjectStateDefunct) {
-        if (handler == src) invalidate();
-    }
+    std::unique_ptr<Event> event;
+    if (type == EventType::ObjectDefunct)
+        event = std::make_unique<ObjectDefunctEvent>(this->shared_from_this());
+
+    event->handleEvent();
 }
 
 void AccessibleNode::invalidate()
index 86cbce6db9d4728efee2dc39a7cbac6467e73d07..23006731ab8a56551edc01de95c9b056f2de8b09 100644 (file)
@@ -76,10 +76,10 @@ void AccessibleWatcher::detach(std::shared_ptr<IEventConsumer> source)
     }
 }
 
-void AccessibleWatcher::notifyAll(int type1, int type2, void *src)
+void AccessibleWatcher::notifyAll(EventType type, void *src)
 {
     std::unique_lock<std::mutex> lock(mLock);
     std::for_each(mSources.begin(), mSources.end(), [&](auto source){
-        source->notify(type1, type2, src);
+        source->notify(type, src);
     });
 }
index 0d093d11a2db931afeb4830cfc8331d859af6d57..b77c65f1a2dba468b42442c177011f038bc3c1ee 100644 (file)
@@ -354,7 +354,7 @@ void AtspiAccessibleWatcher::onAtspiEvents(AtspiEvent *event, void *watcher)
 void AtspiAccessibleWatcher::onObjectDefunct(AtspiAccessible *node)
 {
     LOGI("onObjectDefunct obj:%p", node);
-    notifyAll((int)EventType::Object, (int)ObjectEventType::ObjectStateDefunct, node);
+    notifyAll(EventType::ObjectDefunct, node);
 }
 
 bool AtspiAccessibleWatcher::executeAndWaitForEvents(const Runnable *cmd, const A11yEvent type, const int timeout, const std::string packageName, std::shared_ptr<AccessibleNode> obj, const int count)