# Carrier Pattern class Carrier: def __init__(self): self._items = []
from abc import ABC, abstractmethod from typing import List
Implementing Carrier and Builder Patterns with a Framework
# Create a builder builder = ConcreteBuilder()
# Create items item1 = Item("Item 1") item2 = Item("Item 2")
def build(self): return self._carrier
def add(self, item): self._items.append(item)
The Builder pattern, on the other hand, is a creational design pattern that separates the construction of complex objects from their representation. This pattern allows for more control over the construction process and makes it easier to create complex objects.
# Build a carrier using the builder carrier = ( builder.add_item(item1) .add_item(item2) .build() )
# Example usage class Item: def __init__(self, name): self._name = name
# Carrier Pattern class Carrier: def __init__(self): self._items = []
from abc import ABC, abstractmethod from typing import List
Implementing Carrier and Builder Patterns with a Framework
# Create a builder builder = ConcreteBuilder()
# Create items item1 = Item("Item 1") item2 = Item("Item 2")
def build(self): return self._carrier
def add(self, item): self._items.append(item)
The Builder pattern, on the other hand, is a creational design pattern that separates the construction of complex objects from their representation. This pattern allows for more control over the construction process and makes it easier to create complex objects.
# Build a carrier using the builder carrier = ( builder.add_item(item1) .add_item(item2) .build() )
# Example usage class Item: def __init__(self, name): self._name = name