El Principio Abierto/Cerrado es el principio de diseño más básico en el mundo del desarrollo de software. Nos guía sobre cómo construir un sistema estable y flexible.
Definición:
Una entidad de software, como clase, módulo y función, debe estar abierta para la extensión y cerrada para la modificación.
El significado del principio de apertura y cierre es que una entidad de software debe expandirse para lograr el cambio, en lugar de modificar el código existente para lograr el cambio.
El principio de apertura y cierre es un principio que restringe el desarrollo y diseño actual para las cosas futuras de la entidad de software.
Nota: El principio de apertura y cierre está abierto para extensión y cerrado para modificación. No significa que no se hará ninguna modificación. Los cambios en los módulos de bajo nivel deben combinarse con módulos de alto nivel, de lo contrario, será un fragmento de código aislado y sin sentido.
La importancia del principio de apertura y cierre:
- El impacto de los principios de apertura y cierre en las pruebas
El principio de abrir y cerrar es mantener el código de prueba original aún capaz de ejecutarse normalmente, solo necesitamos probar el código extendido.
- El principio de apertura y cierre puede mejorar la reutilización.
En el diseño orientado a objetos , toda la lógica se combina a partir de la lógica atómica, en lugar de implementar una lógica empresarial en una clase de forma independiente. Solo de esta manera se puede reutilizar el código. Cuanto menor sea la granularidad, mayor será la posibilidad de ser reutilizado.
- El principio abierto y cerrado puede mejorar la mantenibilidad
- Requisitos de desarrollo orientado a objetos
Cómo utilizar el principio de apertura y cierre:
Primero , restrinja la extensión a través de la interfaz o clase abstracta, limite la extensión del límite y no permita métodos públicos que no existen en la interfaz o clase abstracta;
En segundo lugar , intente utilizar interfaces o clases abstractas para tipos de parámetros y objetos de referencia en lugar de clases de implementación;
Tercero , la capa de abstracción debe mantenerse lo más estable posible, una vez que se confirma, no se permite ninguna modificación.
La encapsulación de cambios tiene dos significados:
Primero, encapsule los mismos cambios en una interfaz o clase abstracta;
En segundo lugar, encapsule diferentes cambios en diferentes interfaces o clases abstractas. Dos cambios diferentes no deben aparecer en la misma interfaz o clase abstracta.
Ejemplos
Un ejemplo, abrir y cerrar puertas a mano.
Si hay una nueva demanda, es necesario aumentar la función de abrir y cerrar el cajón a mano. Para el código, debe agregar una clase Cajón y modificar la clase Mano al mismo tiempo.
Siguiendo el ejemplo del principio de apertura y cierre de OCP, abrir y cerrar la puerta a mano depende de una interfaz, y la clase de implementación específica implementa esta interfaz.
Cuando es necesario aumentar la cantidad de manos para abrir y cerrar el refrigerador, la clase de refrigerador implementa esta interfaz sin modificar la interfaz y la clase Hand original.
YoWhile exploring a new website, we came across a webpage that immediately caught our eye. We are incredibly impressed with what we have seen so far and eagerly look forward to your future updates. We are excited to further explore your website and uncover all the amazing features it has to offer.Talk to you soon. my page Come by 빅벳
AhoyWhile exploring a new website, we came across a webpage that immediately caught our eye. We are extremely impressed with what we have seen so far and are eagerly looking forward to your future updates. We can’t wait to explore your website further and uncover all the amazing features it has to offer.Adios. my page Come by 토렌트사이트
HiyaYour blog post was truly enjoyable to read! Once I came across your blog, I couldn’t resist delving into your other posts. The captivating content you provide has left me eagerly anticipating more. I’ve subscribed to your RSS feed and am excitedly looking forward to your future updates!Until we meet again. my web page… Come by 신논현 휴게텔
Howdy-doI am deeply moved by your post, which has sparked a strong curiosity within me to delve deeper into the subject. Your extensive knowledge and insightful perspectives are truly admirable, and I sincerely value your unique point of view. Thank you for generously sharing your thoughts and taking the time to do so! my page Come by 재테크 종류
How’s everything?Your blog post had me hooked from the start! Since stumbling upon your blog, I’ve been immersed in your other articles. The captivating content you provide has left me craving for more. I’ve subscribed to your RSS feed and am eagerly awaiting your upcoming updates!Keep in touch.
How’s it going?While exploring a new website, we came across a webpage that immediately caught our eye. We are extremely impressed with what we have seen so far and are eagerly looking forward to your future updates. We can’t wait to explore your website further and uncover all the amazing features it has to offer.
During our exploration of a new website, we stumbled upon a webpage that instantly grabbed our attention. We are thoroughly impressed with what we have seen thus far and eagerly anticipate your upcoming updates. We are excited to delve deeper into your website and discover all the incredible features it has to offer.Take it easy.
How are you?While exploring a new website, we came across a webpage that immediately caught our eye. We are incredibly impressed with what we have seen so far and eagerly look forward to your future updates. We are excited to further explore your website and uncover all the amazing features it has to offer.Ciao.
HowdyYour post was absolutely amazing and I thoroughly enjoyed reading it! I was hoping if you could kindly share additional insights on this topic. It would be immensely appreciated if you could delve into further details. Thank you immensely for your time and effort!Bye-bye.
SalutationsWe stumbled upon this webpage on another site and were intrigued to explore further. I’m really liking what I see, so I’ll be keeping up with your updates. Looking forward to delving deeper into your website.So long.
While exploring a new website, we came across a webpage that immediately caught our interest. We are incredibly impressed with our findings so far and eagerly look forward to your future updates. We are excited to further explore your website and uncover all the amazing features it has to offer.Stay well.
Hey, you!Your blog post was truly enjoyable to read! Once I came across your blog, I couldn’t resist delving into your other posts. The captivating content you provide has left me eagerly anticipating more. I’ve subscribed to your RSS feed and am excitedly looking forward to your future updates!
Hi-helloI was completely captivated by your blog post! Ever since I discovered your blog, I have been engrossed in your other articles. The engaging content you offer has left me wanting more. I have subscribed to your RSS feed and am eagerly anticipating your future updates!
AhoyYour blog post immediately caught my attention! Since stumbling upon your blog, I have been completely absorbed in your other articles. The engaging content you provide has left me wanting more. I have subscribed to your RSS feed and am eagerly looking forward to your upcoming updates!Be seeing you.
Appreciating the dedication you put into your site and detailed information you provide. It’s good to come across a blog every once in a while that isn’t the same old rehashed information. Great read! I’ve saved your site and I’m including your RSS feeds to my Google account.
I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thank you again!