SoFunction
Updated on 2024-10-29

Python design patterns of the combination pattern principle and usage examples of analysis

This article example describes the Python design pattern of the combination pattern principle and usage. Shared for your reference, as follows:

Combination pattern (Composite Pattern): the combination of objects into a tree structure to represent the "part of the whole" hierarchical structure, the combination of modes to make the user on a single object and the combination of the use of objects has the consistency of the object.

Here is a demo of the combination pattern:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
big talk about design patterns
design pattern——Combined mode
Combined mode(Composite Pattern):Combine objects into a tree structure to represent the“portion-synthesis”hierarchy,Combined mode使得用户对单个对象和组合对象的使用具有一致性.
"""
# Abstract an organization class
class Component(object):
  def __init__(self, name):
     = name
  def add(self,comp):
    pass
  def remove(self,comp):
    pass
  def display(self, depth):
    pass
# Leaf nodes
class Leaf(Component):
  def add(self,comp):
    print 'Cannot add subordinate nodes'
  def remove(self,comp):
    print 'Cannot delete subordinate nodes'
  def display(self, depth):
    strtemp = ''
    for i in range(depth):
      strtemp += strtemp+'-'
    print strtemp+
# Branches
class Composite(Component):
  def __init__(self, name):
     = name
     = []
  def add(self,comp):
    (comp)
  def remove(self,comp):
    (comp)
  def display(self, depth):
    strtemp = ''
    for i in range(depth):
      strtemp += strtemp+'-'
    print strtemp+
    for comp in :
      (depth+2)
if __name__ == "__main__":
  # Generate tree roots
  root = Composite("root")
  #2 leaves growing on the root
  (Leaf('leaf A'))
  (Leaf('leaf B'))
  # Branches growing from roots Composite X
  comp = Composite("Composite X")
  (Leaf('leaf XA'))
  (Leaf('leaf XB'))
  (comp)
  # Branches growing from roots Composite X
  comp2 = Composite("Composite XY")
  #Composite X grows 2 leaves
  (Leaf('leaf XYA'))
  (Leaf('leaf XYB'))
  (comp2)
  # Two more leaves grew from the root, C and D. D didn't open up and fell off. #
  (Leaf('Leaf C'))
  leaf = Leaf("Leaf D")
  (leaf)
  (leaf)
  # Showcase Organization
  (1)

The results of the run are as follows:

The design of the above class is shown below:

Application Scenarios:

Where there is a need to reflect a hierarchical structure of parts and wholes

When you want the user to ignore the difference between a composite object and a single object and use all objects in the composite structure in a unified way

More about Python related content can be viewed on this site's topic: thePython Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope the description of this article will help you in Python programming.