ویرگول
ورودثبت نام
امید آرام - توسعه دهنده نرم افزار
امید آرام - توسعه دهنده نرم افزار
خواندن ۷ دقیقه·۲ ماه پیش

اصطلاحات Tightly Coupled و Loosely Coupled در کدنویسی

In software development, the terms "tightly coupled" and "loosely coupled" refer to the degree to which components of a system depends on each other.

در توسعه نرم‌افزار، اصطلاحات «tightly coupled» و «loosely coupled» به میزان وابستگی اجزای یک سیستم به یکدیگر اشاره دارد.

Tightly Coupled Code: Tightly coupled code is when a group of classes are highly dependent on one another. This isn't necessarily a bad thing, but it can make the code harder to test because of the dependent classes are so intertwined. They can't be used independently or substituted easily.

In tightly coupled systems, each component or class in the system knows details about many other components or classes. They are interdependent, meaning that if one component changes, it can have a ripple effect on all other components that depend on it. This can make the system as a whole more difficult to maintain, because changes in one place can require changes in many other places.

Tightly coupled systems can also be more difficult to test, because each component might rely on many other components to function correctly. This means that to test just one component, you might need to also set up and manage many other components.

Here's an example of tightly coupled code in C#:

کد Tightly Coupled: کدی است که در آن گروهی از کلاس ها به شدت به یکدیگر وابسته هستند. این لزوماً چیز بدی نیست، اما می‌تواند تست کد را سخت‌تر کند زیرا کلاس‌های وابسته بسیار در هم تنیده شده‌اند. آنها را نمی توان به طور مستقل استفاده کرد یا به راحتی جایگزین کرد.

در سیستم‌های tightly coupled، هر جزء یا کلاسی در سیستم، جزئیات بسیاری از اجزا یا کلاس‌های دیگر را می‌داند. آنها به یکدیگر وابسته هستند، به این معنی که اگر یک جزء تغییر کند، موجی از تغییرات بر سایر اجزای وابسته به آن خواهید داشت. این می تواند حفظ سیستم را به طور کلی دشوارتر کند، زیرا تغییرات در یک مکان منجر به تغییرات در بسیاری از مکان های دیگر میشود.

تست کردن سیستم‌های tightly coupled نیز دشوارتر است، زیرا هر جزء ممکن است برای عملکرد صحیح به بسیاری از اجزای دیگر متکی باشد. این بدان معنی است که برای تست فقط یک کامپوننت، ممکن است لازم باشد بسیاری از کامپوننت های دیگر را نیز تنظیم و مدیریت کنید.

در اینجا یک مثال از کدهای tightly coupled در #C آمده است:

public class Customer { public int Id {get; set; } public string Name {get; set; } } public class CustomerBusinessLogic { public CustomerBusinessLogic() { _customerDataAccess = new CustomerDataAccess(); } private CustomerDataAccess _customerDataAccess; }Explain
In this example, CustomerBusinessLogic is tightly coupled to CustomerDataAccess. It directly instantiates CustomerDataAccess, making it difficult to substitute a different implementation of mock it for testing.

در این مثال، CustomerBusinessLogic به شدت با CustomerDataAccess وابستگی دارد چرا که مستقیماً یک آبجکت از آن ساخته است، و باعث شده است که جایگزینی آن با یک کلاس mock برای تست، دشوار (یا عملاً غیرممکن) شود.


Loosely Coupled Code: Loosely coupled code is when the components are made independent as much as possible. This is generally considered a good practice as it makes the code more flexible, easier to reuse, and easier to test because components can be tested independently and substituted easily.

In loosely coupled systems, components or classes are designed to interact with each other as little as possible. They still communicate and interact, but they do so through well-defined interfaces, without needing to know the details of how other components are implemented.

1. Easier Maintenance: Because each component is independent, changes in one component don't require changes in other components. This makes the system as a whole easier to maintain.

2. Improved Testability: Components can be tested independently, without needing to set up and manage other components. This makes it easier to write unit tests, and makes the tests more reliable, because they're less likely to be affected by changes in other parts of the system.

3. Greater Flexibility and Reusability: Because components don't depend on each other, they can be more easily reused in different parts of the system, or even in different systems. They can also be replaced or upgraded without affecting other components.

Here's an example of loosely coupled code in C#:

کد Loosely Coupled: کدی است که اجزا آن تا حد امکان مستقل ساخته شوند. این به طور کلی یک ویژگی خوب به حساب می آید، زیرا کد را انعطاف‌پذیرتر میکند و استفاده مجدد از آن را آسان‌تر می‌کند. همچنین تست آن کد را آسان‌تر می‌کند، چرا که اجزا را می‌توان به‌طور مستقل تست کرد و به راحتی جایگزین کرد.

در سیستم‌های loosely coupled، اجزا یا کلاس‌ها طوری طراحی می‌شوند که تا حد امکان کمتر با یکدیگر تعامل داشته باشند. آنها هنوز هم ارتباط و تعامل دارند، اما این کار را از طریق Interfaceها (رابط ها) انجام می دهند، بدون اینکه نیاز داشته باشند جزئیات نحوه پیاده سازی سایر اجزا را بدانند.

ویژگی های کد Loosely Coupled:

  1. نگهداری آسان تر: از آنجایی که هر جزء مستقل است، تغییر در یک جزء نیازی به تغییر در سایر اجزا ندارد. این امر نگهداری سیستم را در کل آسان تر می کند.
  2. افزایش قابلیت تست پذیری: کامپوننت ها را می توان به طور مستقل، بدون نیاز به راه اندازی و مدیریت اجزای دیگر، تست کرد. این کار نوشتن Unit Testها را آسان‌تر می‌کند و تست‌ها را قابل اعتمادتر می‌کند، زیرا احتمال کمتری دارد که تحت تأثیر تغییرات سایر بخش‌های سیستم قرار بگیرند.
  3. انعطاف پذیری (Flexibility) و قابلیت استفاده مجدد (Reusability) بیشتر : از آنجایی که اجزا به یکدیگر وابسته نیستند، می توان آنها را به راحتی در بخش های مختلف سیستم یا حتی در سیستم های مختلف مجدداً استفاده کرد. همچنین می‌توان آن‌ها را بدون تأثیرگذاری بر سایر اجزای آن جایگزین یا ارتقا داد.

در اینجا مثالی از کدهای loosely coupled در #C آمده است:

public interface ICustomerDataAccess { void Save(ICustomerDataAccess customer); } public class CustomerBusinessLogic { private ICustomerDataAccess _dataAccess; public CustomerBusinessLogic(ICustomerDataAccess dataAccess) { _dataAccess = dataAccess; } public void Save(CustomerBusinessLogic customer) { _dataAccess.Save(customer); } }Explain
How to Achieve Loose Coupling:

There are several techniques that can help achieve loose coupling:

1. Dependency Injection: Instead of having components create the objects they depend on, those objects are created elsewhere and passed in (injected) to the component that needs them.

2. Programming to Interfaces: Instead of having components interact with concrete classes, they interact with interfaces. This means that any class that implements the interface can be substituted in, without the component knowing or caring about the details of how that class is implemented.

3. Event-Driven Programming: Instead of components calling each other directly, they emit events that other components can listen for and respond to. This allows components to communicate and interact without needing to know about each other.

By using these techniques, you can create systems that are easier to maintain, test, and extend.

Happy Coding...

چندین تکنیک وجود دارد که می تواند به loosely coupled کمک کند:

  1. تزریق وابستگی (Dependency Injection): به جای اینکه کامپوننت ها اشیایی را که به آنها وابسته هستند ایجاد کنند، آن اشیا در جای دیگری ایجاد می شوند و به کامپوننتی که به آنها نیاز دارد پاس داده می شوند (تزریق می شوند).
  2. برنامه نویسی با Interfaceها: به جای اینکه کامپوننت ها مستقیم با متن کلاس ها تعامل داشته باشند، با Interfaceها تعامل دارند. این بدان معناست که هر کلاسی که آن Interface را پیاده‌سازی می‌کند، می‌تواند جایگزین شود، بدون اینکه کامپوننت بداند یا به جزئیات نحوه پیاده‌سازی آن کلاس اهمیت دهد.
  3. برنامه نویسی رویداد محور (Event-Driven): به جای اینکه کامپوننت ها مستقیماً یکدیگر را فراخوانی کنند، Eventهایی را منتشر می کنند که سایر کامپوننت ها می توانند به آنها گوش دهند و به آنها پاسخ دهند. این به کامپوننت ها اجازه می دهد تا بدون نیاز به دانستن در مورد یکدیگر، با یکدیگر ارتباط برقرار کرده و تعامل داشته باشند.

با استفاده از این تکنیک‌ها، می‌توانید سیستم‌هایی ایجاد کنید که نگهداری، تست و توسعه آن‌ها آسان‌تر باشد.

شاد باشید...

(لینک مطلب اصلی: در اولین کامنت)

tightly coupledLoose Couplingsoftwarecodingclean code
شاید از این پست‌ها خوشتان بیاید