From 1f866ca9b731cbdb95c8d29798180723707f9c9f Mon Sep 17 00:00:00 2001 From: Woochan Lee Date: Thu, 25 Jan 2024 21:22:35 +0900 Subject: [PATCH] libaurum: Applying strategy pattern for OCP 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 | 2 +- .../inc/Accessibility/AccessibleWatcher.h | 2 +- libaurum/inc/Accessibility/IEventConsumer.h | 19 ++-------- libaurum/inc/Accessibility/IEventSource.h | 5 +-- libaurum/src/Accessibility/AccessibleNode.cc | 37 ++++++++++++++++--- .../src/Accessibility/AccessibleWatcher.cc | 4 +- .../Accessibility/AtspiAccessibleWatcher.cc | 2 +- 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/libaurum/inc/Accessibility/AccessibleNode.h b/libaurum/inc/Accessibility/AccessibleNode.h index 16f7c89..26521db 100644 --- a/libaurum/inc/Accessibility/AccessibleNode.h +++ b/libaurum/inc/Accessibility/AccessibleNode.h @@ -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. diff --git a/libaurum/inc/Accessibility/AccessibleWatcher.h b/libaurum/inc/Accessibility/AccessibleWatcher.h index 4b4585b..e25bde0 100644 --- a/libaurum/inc/Accessibility/AccessibleWatcher.h +++ b/libaurum/inc/Accessibility/AccessibleWatcher.h @@ -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> mSources; diff --git a/libaurum/inc/Accessibility/IEventConsumer.h b/libaurum/inc/Accessibility/IEventConsumer.h index 5ed0915..d7a35e4 100644 --- a/libaurum/inc/Accessibility/IEventConsumer.h +++ b/libaurum/inc/Accessibility/IEventConsumer.h @@ -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; }; } diff --git a/libaurum/inc/Accessibility/IEventSource.h b/libaurum/inc/Accessibility/IEventSource.h index f3cf2d7..a55fb60 100644 --- a/libaurum/inc/Accessibility/IEventSource.h +++ b/libaurum/inc/Accessibility/IEventSource.h @@ -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; }; } diff --git a/libaurum/src/Accessibility/AccessibleNode.cc b/libaurum/src/Accessibility/AccessibleNode.cc index abfed4a..e2d0539 100644 --- a/libaurum/src/Accessibility/AccessibleNode.cc +++ b/libaurum/src/Accessibility/AccessibleNode.cc @@ -26,6 +26,31 @@ using namespace Aurum; +class Event { +public: + + Event(std::shared_ptr node) + : mNode{node} {} + + virtual ~Event(){}; + virtual void handleEvent() = 0; +protected: + + std::shared_ptr mNode; +}; + +class ObjectDefunctEvent : public Event { +public: + + ObjectDefunctEvent(std::shared_ptr 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; + if (type == EventType::ObjectDefunct) + event = std::make_unique(this->shared_from_this()); + + event->handleEvent(); } void AccessibleNode::invalidate() diff --git a/libaurum/src/Accessibility/AccessibleWatcher.cc b/libaurum/src/Accessibility/AccessibleWatcher.cc index 86cbce6..2300673 100644 --- a/libaurum/src/Accessibility/AccessibleWatcher.cc +++ b/libaurum/src/Accessibility/AccessibleWatcher.cc @@ -76,10 +76,10 @@ void AccessibleWatcher::detach(std::shared_ptr source) } } -void AccessibleWatcher::notifyAll(int type1, int type2, void *src) +void AccessibleWatcher::notifyAll(EventType type, void *src) { std::unique_lock lock(mLock); std::for_each(mSources.begin(), mSources.end(), [&](auto source){ - source->notify(type1, type2, src); + source->notify(type, src); }); } diff --git a/libaurum/src/Impl/Accessibility/AtspiAccessibleWatcher.cc b/libaurum/src/Impl/Accessibility/AtspiAccessibleWatcher.cc index 0d093d1..b77c65f 100644 --- a/libaurum/src/Impl/Accessibility/AtspiAccessibleWatcher.cc +++ b/libaurum/src/Impl/Accessibility/AtspiAccessibleWatcher.cc @@ -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 obj, const int count) -- 2.34.1