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 16f7c89..26521db 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 4b4585b..e25bde0 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 5ed0915..d7a35e4 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 f3cf2d7..a55fb60 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 abfed4a..e2d0539 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 86cbce6..2300673 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 0d093d1..b77c65f 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)