Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
            页面加载耗时 0.00 毫秒·物理内存 161.8MB ·虚拟内存 1438.3MB
        
        欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
        
        
        反模式遵循与预定义设计模式相反的策略。该策略包括解决常见问题的通用方法,这些方法可以形式化,通常可以视为一种良好的开发实践。通常,反模式是相反的并且是不希望的。反模式是软件开发中使用的某些模式,被认为是不良的编程习惯。
现在让我们看看反模式的一些重要特征。
这些模式实际上会破坏您的代码并使您做错事。以下是对此的简单说明:
# Filename : example.py
# Date : 2020-08-22
class Rectangle(object):
    def __init__(self, width, height):
        self._width = width
        self._height = height
        r = Rectangle(5, 6)
        # direct access of protected member
        print("Width: {:d}".format(r._width))
如果易于理解和根据要求进行修改,则该程序可维护。导入模块可以看作是可维护性的一个例子。
# Filename : example.py # Date : 2020-08-22 import math x = math.ceil(y) # or import multiprocessing as mp pool = mp.pool(8)
以下示例有助于反模式的演示-
# Filename : example.py
# Date : 2020-08-22
#Bad
def filter_for_foo(l):
   r = [e for e in l if e.find("foo") != -1]
   if not check_some_critical_condition(r):
      return None
   return r
res = filter_for_foo(["bar","foo","faz"])
if res is not None:
   #continue processing
   pass
#Good
def filter_for_foo(l):
   r = [e for e in l if e.find("foo") != -1]
   if not check_some_critical_condition(r):
      raise SomeException("critical condition unmet!")
   return r
try:
   res = filter_for_foo(["bar","foo","faz"])
   #continue processing
except SomeException:
   i = 0
while i < 10:
   do_something()
   #we forget to increment i
该示例包括使用Python创建函数的好坏标准的演示。
处理异常也是设计模式的主要标准。例外是在程序执行期间发生的错误。当发生特定错误时,生成异常非常重要。这有助于减少程序崩溃。 为什么要使用异常?异常是在程序中处理错误和特殊条件的便捷方法。当用户认为指定 ...