Full Code of yogykwan/design-patterns-py for AI

master f2175f1055dd cached
25 files
29.6 KB
7.8k tokens
325 symbols
1 requests
Download .txt
Repository: yogykwan/design-patterns-py
Branch: master
Commit: f2175f1055dd
Files: 25
Total size: 29.6 KB

Directory structure:
gitextract_kmnmm8_p/

├── .gitignore
├── README.md
├── abstract_factory.py
├── adapter.py
├── bridge.py
├── builder.py
├── chain_of_responsibility.py
├── command.py
├── composite.py
├── decorator.py
├── facade.py
├── factory_method.py
├── flyweight.py
├── interpreter.py
├── iterator.py
├── mediator.py
├── memento.py
├── observer.py
├── prototype.py
├── proxy.py
├── singleton.py
├── state.py
├── strategy.py
├── template_method.py
└── visitor.py

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
.idea/

================================================
FILE: README.md
================================================
# design-patterns
《大话设计模式》中23种设计模式案例的Python版本实现。样例忠于原书,某些地方根据Python特性做了修改,如abstract、private、reflection等。


## 读书笔记
* [创建型模式](http://jennica.space/2016/12/28/design-patterns-creational/)
* [结构型模式](http://jennica.space/2016/12/30/design-patterns-structural/)
* [行为型模式](http://jennica.space/2017/01/03/design-patterns-behavioral/)

## C++版
[design-patterns-cpp](https://github.com/yogykwan/design-patterns-cpp)

================================================
FILE: abstract_factory.py
================================================
import abc


class User:
    """class user"""


class IUser:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def insert_user(self, user):
        """insert user"""

    @abc.abstractmethod
    def get_user(self):
        """get user"""


class SqlserverUser(IUser):
    def insert_user(self, user):
        print "Insert user into Sqlserver"

    def get_user(self):
        print "Get user from Sqlserver"


class AccessUser(IUser):
    def insert_user(self, user):
        print "Insert user into Access"

    def get_user(self):
        print "Get user from Access"


class Department:
    """class department"""


class IDepartment:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def insert_department(self, department):
        """insert department"""

    def get_department(self):
        """get department"""


class SqlserverDepartment(IDepartment):
    def insert_department(self, department):
        print "Insert department into Sqlserver"

    def get_department(self):
        print "Get department from Sqlserver"


class AccessDepartment(IDepartment):
    def insert_department(self, department):
        print "Insert department into Access"

    def get_department(self):
        print "Get department from Access"


class IFactory:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def create_user(self):
        """create user"""

    @abc.abstractmethod
    def create_department(self):
        """create deparment"""


class SqlserverFactory(IFactory):
    def create_user(self):
        return SqlserverUser()

    def create_department(self):
        return SqlserverDepartment()


class AccessFactory(IFactory):
    def create_user(self):
        return AccessUser()

    def create_department(self):
        return AccessDepartment()


if __name__ == "__main__":
    # use sqlserver to insert/get user/department
    i_factory = SqlserverFactory()
    i_user = i_factory.create_user()
    i_user.insert_user(User())
    i_user.get_user()
    i_department = i_factory.create_department()
    i_department.insert_department(Department())
    i_department.get_department()

    # use access to insert/get user/department
    i_factory = AccessFactory()  # just replace SqlserverFactory with AccessFactory
    i_user = i_factory.create_user()
    i_user.insert_user(User())
    i_user.get_user()
    i_department = i_factory.create_department()
    i_department.insert_department(Department())
    i_department.get_department()


================================================
FILE: adapter.py
================================================
import abc


class Player:
    __metaclass__ = abc.ABCMeta

    def __init__(self, n):
        self.name = n

    @abc.abstractmethod
    def attack(self):
        """attack"""

    @abc.abstractmethod
    def defense(self):
        """defense"""


class Center(Player):
    def attack(self):
        print "center " + self.name + " attack"

    def defense(self):
        print "center " + self.name + " defense"


class ForeignCenter:
    def __init__(self, n):
        self.name = n

    def gong(self):
        print "foreign center " + self.name + " attack"

    def shou(self):
        print "foreign center " + self.name + " defense"


class Translator(Player):
    def __init__(self, n):
        Player.__init__(self, n)
        self.foreign_center = ForeignCenter(n)

    def attack(self):
        self.foreign_center.gong()

    def defense(self):
        self.foreign_center.shou()


if __name__ == "__main__":
    # center attack/defense
    center = Center("Russell")
    center.attack()
    center.defense()

    # foreign center gong/shou through translator's attack/defense
    translator = Translator("YaoMing")
    translator.attack()
    translator.defense()


================================================
FILE: bridge.py
================================================
import abc


class HandsetSoft:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def run(self):
        """soft run"""


class HandsetGame(HandsetSoft):
    def run(self):
        print "run game"


class HandsetAddressList(HandsetSoft):
    def run(self):
        print "run address list"


class HandsetBrand:
    __metaclass__ = abc.ABCMeta

    def __init__(self, s):
        self.soft = s

    @abc.abstractmethod
    def run(self):
        """run"""


class HandsetBrandM(HandsetBrand):
    def run(self):
        print "handset brand M : ",
        self.soft.run()


class HandsetBrandN(HandsetBrand):
    def run(self):
        print "handset brand N : ",
        self.soft.run()


if __name__ == "__main__":
    handset_brand_m = HandsetBrandM(HandsetGame())
    handset_brand_m.run()
    handset_brand_m = HandsetBrandM(HandsetAddressList())
    handset_brand_m.run()

    handset_brand_n = HandsetBrandN(HandsetGame())
    handset_brand_n.run()
    handset_brand_n = HandsetBrandN(HandsetAddressList())
    handset_brand_n.run()


================================================
FILE: builder.py
================================================
import abc


class Pen:
    """class pen"""


class Graphics:
    """class graphics"""


class PersonBuilder:
    __metaclass__ = abc.ABCMeta

    def __init__(self, p, g):
        self.pen = p
        self.grahics = g

    @abc.abstractmethod
    def BuildHead(self):
        """build head"""

    @abc.abstractmethod
    def BuildBody(self):
        """build body"""


class PersonThinBuilder(PersonBuilder):
    def BuildHead(self):
        print "build thin head"

    def BuildBody(self):
        print "build thin body"


class PersonFatBuilder(PersonBuilder):
    def BuildHead(self):
        print "build fat head"

    def BuildBody(self):
        print "build fat body"


class PersonDirector:
    def __init__(self, pb):
        self.person_builder = pb

    def create_person(self):
        self.person_builder.BuildHead()
        self.person_builder.BuildBody()


if __name__ == "__main__":
    # build thin person
    person_builder = PersonThinBuilder(Pen(), Graphics())
    director = PersonDirector(person_builder)
    director.create_person()

    # build fat person
    person_builder = PersonFatBuilder(Pen(), Graphics())  # only replace PersonThinBuilder with PersonFatBuilder
    director = PersonDirector(person_builder)
    director.create_person()


================================================
FILE: chain_of_responsibility.py
================================================
import abc


class Request:
    def __init__(self, t, n):
        self.type = t
        self.number = n


class Manager:
    __metaclass__ = abc.ABCMeta

    def __init__(self, n):
        self.name = n
        self.superior = None

    def set_superior(self, s):
        self.superior = s

    @abc.abstractmethod
    def request_applications(self, r):
        """resolve request"""


class CommonManager(Manager):
    def request_applications(self, r):
        if r.type == "leave application" and r.number <= 2:
            print self.name + " : approve"
        else:
            self.superior.request_applications(r)


class Majordomo(Manager):
    def request_applications(self, r):
        if r.type == "leave application" and r.number <= 5:
            print self.name + " : approve"
        else:
            self.superior.request_applications(r)


class GeneralManager(Manager):
    def request_applications(self, r):
        if r.type == "leave application":
            print self.name + " : approve"
        elif r.type == "salary increase":
            if r.number <= 500:
                print self.name + " : approve"
            else:
                print self.name + " : not approve"


if __name__ == "__main__":
    common_manager = CommonManager("JingLi")
    majordomo = Majordomo("ZongJian")
    general_manager = GeneralManager("ZongJingLi")
    common_manager.set_superior(majordomo)
    majordomo.set_superior(general_manager)

    request = Request("leave application", 4)
    common_manager.request_applications(request)

    request = Request("salary increase", 1000)
    common_manager.request_applications(request)


================================================
FILE: command.py
================================================
import abc


class Barbecuer(object):
    def bake_mutton(self):
        print "bake mutton"

    def bake_chicken(self):
        print "bake chicken"


class Command(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, r):
        self.receiver = r

    @abc.abstractmethod
    def execute_command(self):
        pass


class BakeMuttonCommand(Command):
    def execute_command(self):
        self.receiver.bake_mutton()


class BakeChickenCommand(Command):
    def execute_command(self):
        self.receiver.bake_chicken()


class Waiter(object):
    def __init__(self):
        self.commands = []

    def set_order(self, o):
        if isinstance(o, BakeChickenCommand):
            print "chicken sold out"
        else:
            print "add order: " + type(o).__name__
            self.commands.append(o)

    def cancel_order(self, o):
        print "cancel order: " + type(o).__name__
        self.commands.remove(o)

    def notify(self):
        for command in self.commands:
            command.execute_command()


if __name__ == "__main__":
    barbecuer = Barbecuer()
    bake_mutton_command = BakeMuttonCommand(barbecuer)
    bake_chicken_command = BakeChickenCommand(barbecuer)
    waiter = Waiter()
    waiter.set_order(bake_mutton_command)
    waiter.set_order(bake_mutton_command)
    waiter.set_order(bake_chicken_command)
    waiter.notify()

================================================
FILE: composite.py
================================================
import abc


class Company:
    __metaclass__ = abc.ABCMeta

    def __init__(self, n):
        self.name = n

    @abc.abstractmethod
    def add(self, c):
        """add"""

    @abc.abstractmethod
    def display(self, d):
        """display"""

    @abc.abstractmethod
    def line_of_duty(self):
        """duty line"""


class HrDepartment(Company):
    def __init__(self, n):
        Company.__init__(self, n)

    def add(self, c):
        print "leaf can't add component"

    def display(self, d):
        print "--" * d + self.name

    def line_of_duty(self):
        print self.name + " : human resources"


class FinanceDepartment(Company):
    def __init__(self, n):
        Company.__init__(self, n)

    def add(self, c):
        print "leaf can't add component"

    def display(self, d):
        print "--" * d + self.name

    def line_of_duty(self):
        print self.name + " : finance analysis"


class ConcreteCompany(Company):
    def __init__(self, n):
        Company.__init__(self, n)
        self.companies = []

    def add(self, c):
        self.companies.append(c)

    def display(self, d):
        print "--" * d + self.name
        for company in self.companies:
            company.display(d + 1)

    def line_of_duty(self):
        for company in self.companies:
            company.line_of_duty()


if __name__ == "__main__":
    beijing_head_office = ConcreteCompany("Beijing Head Office")
    beijing_head_office.add(HrDepartment("Beijing HR"))
    beijing_head_office.add(FinanceDepartment("Beijing Finance"))

    huadong_branch_office = ConcreteCompany("Huadong Branch Office")
    huadong_branch_office.add(HrDepartment("Huadong HR"))
    huadong_branch_office.add(FinanceDepartment("Huadong Finance"))
    beijing_head_office.add(huadong_branch_office)

    nanjing_office = ConcreteCompany("Nanjing Office")
    nanjing_office.add(HrDepartment("Nanjing HR"))
    nanjing_office.add(FinanceDepartment("Nanjing Finance"))
    huadong_branch_office.add(nanjing_office)

    hangzhou_office = ConcreteCompany("Hangzhou Office")
    hangzhou_office.add(HrDepartment("Hangzhou HR"))
    hangzhou_office.add(FinanceDepartment("Hangzhou Finance"))
    huadong_branch_office.add(hangzhou_office)

    beijing_head_office.display(0)

    beijing_head_office.line_of_duty()


================================================
FILE: decorator.py
================================================
import abc


class Person:
    def show(self):
        print "person"


class Finery(Person):
    def __init__(self, c):
        self.component = c

    def show(self):
        """finery show"""


class Tie(Finery):
    def show(self):
        print "tie ",
        self.component.show()


class Suit(Finery):
    def show(self):
        print "suit ",
        self.component.show()


class Shoes(Finery):
    def show(self):
        print "shoes ",
        self.component.show()


if __name__ == "__main__":
    person = Person()
    tie = Tie(person)
    suit = Suit(tie)
    shoes = Shoes(suit)
    shoes.show()


================================================
FILE: facade.py
================================================
class Stock1:
    def buy(self):
        print "buy stock1"

    def sell(self):
        print "sell stock1"


class Stock2:
    def buy(self):
        print "buy stock2"

    def sell(self):
        print "sell stock2"


class Reality1:
    def buy(self):
        print "buy reality1"

    def sell(self):
        print "sell reality1"


class Fund:
    def __init__(self):
        self.stock1 = Stock1()
        self.stock2 = Stock2()
        self.reality1 = Reality1()

    def buy(self):
        self.stock1.buy()
        self.stock2.buy()
        self.reality1.buy()

    def sell(self):
        self.stock1.sell()
        self.stock2.sell()
        self.reality1.sell()


if __name__ == "__main__":
    fund = Fund()
    fund.buy()
    fund.sell()


================================================
FILE: factory_method.py
================================================
import abc


class Leifeng:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def wash(self):
        """"wash"""

    @abc.abstractmethod
    def sweep(self):
        """sweep"""

    @abc.abstractmethod
    def buy_rice(self):
        """buy rice"""


class Undergraduate(Leifeng):
    def wash(self):
        print "undergraduate wash"

    def sweep(self):
        print "undergraduate sweep"

    def buy_rice(self):
        print "undergraduate buy rice"


class Volunteer(Leifeng):
    def wash(self):
        print "volunteer wash"

    def sweep(self):
        print "volunteer sweep"

    def buy_rice(self):
        print "volunteer buy rice"


class IFactory:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def CreateLeifeng(self):
        """create class leifeng"""


class UndergraduateFactory(IFactory):
    def CreateLeifeng(self):
        return Undergraduate()


class VolunteerFactory(IFactory):
    def CreateLeifeng(self):
        return Volunteer()


if __name__ == "__main__":
    # create undergraduate to sweep
    i_factory = UndergraduateFactory()
    leifeng = i_factory.CreateLeifeng()
    leifeng.sweep()

    # create volunteer to wash
    i_factory = VolunteerFactory()  # just replace UndergraduateFactory with VolunteerFactory
    leifeng = i_factory.CreateLeifeng()
    leifeng.wash()


================================================
FILE: flyweight.py
================================================
import abc


class User:
    def __init__(self, n):
        self.name = n


class Website:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def use(self, u):
        """users use website"""


class ConcreteWebsite(Website):
    def __init__(self, wn):
        self.website_name = wn

    def use(self, u):
        print u.name + " use " + self.website_name


class WebsiteFactory:
    flyweights = {}

    def get_website_category(self, n):
        if self.flyweights.has_key(n) is False:
            self.flyweights[n] = ConcreteWebsite(n)
        return self.flyweights[n]

    def get_website_count(self):
        print len(self.flyweights.keys())


if __name__ == "__main__":
    website_factory = WebsiteFactory()
    alice = User("Alice")
    bob = User("Bob")

    website = website_factory.get_website_category("bbs")
    website.use(alice)
    website.use(bob)
    website_factory.get_website_count()

    website = website_factory.get_website_category("blog")
    website.use(alice)
    website.use(bob)
    website_factory.get_website_count()

    website = website_factory.get_website_category("bbs")
    website.use(alice)
    website.use(bob)
    website_factory.get_website_count()


================================================
FILE: interpreter.py
================================================
import abc


class Context(object):
    def __init__(self, t):
        self.text = t


class Expression(object):
    def interpret(self, c):
        text = c.text.split(' ')
        key = text[0]
        value = float(text[1])
        c.text = ' '.join(text[2:])
        self.execute(key, value)

    @abc.abstractmethod
    def execute(self, k, v):
        pass


class Scale(Expression):
    def execute(self, k, v):
        scale_dic = {1: 'bass', 2: 'alto', 3: 'treble'}
        print scale_dic[v] + " ",


class Note(Expression):
    def execute(self, k, v):
        print k + " ",


class ExpressionFactory(object):
    @staticmethod
    def create_expression(t):
        type = "Scale" if t == 'O' else "Note"
        obj = globals()[type]()
        return obj


if __name__ == "__main__":
    context = Context("O 2 E 0.5 G 0.5 A 3")
    while len(context.text):
        expression = ExpressionFactory.create_expression(context.text[0])
        expression.interpret(context)



================================================
FILE: iterator.py
================================================
import abc


class Aggregate:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def create_iterator(self):
        """create iterator"""


class List(Aggregate):
    def __init__(self):
        self.items = []

    def create_iterator(self):
        return ListIterator(self)

    def count(self):
        return len(self.items)

    def __setitem__(self, key, value):
        if key >= len(self.items):
            self.items.append(value)
        else:
            self.items[key] = value

    def __getitem__(self, i):
        return self.items[i]


class Iterator:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def first_item(self):
        """"""

    @abc.abstractmethod
    def next_item(self):
        """"""

    @abc.abstractmethod
    def is_done(self):
        """"""

    @abc.abstractmethod
    def current_item(self):
        """"""

class ListIterator(Iterator):
    def __init__(self, a):
        self.current = 0
        self.aggregate = a

    def first_item(self):
        return self.aggregate[0]

    def next_item(self):
        result = None
        self.current += 1
        if self.current < self.aggregate.count():
            result = self.aggregate[self.current]
        return result

    def is_done(self):
        return self.current >= self.aggregate.count()

    def current_item(self):
        return self.aggregate[self.current]


if __name__ == "__main__":
    list = List()
    list[0] = 1
    list[1] = 2
    list[2] = 3
    list_iterator = list.create_iterator()
    print list_iterator.first_item()
    print list_iterator.current_item()
    print list_iterator.next_item()
    print list_iterator.next_item()
    print list_iterator.is_done()
    print list_iterator.next_item()
    print list_iterator.is_done()





================================================
FILE: mediator.py
================================================
import abc


class Country(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, un):
        self.un = un

    def declare(self, m):
        self.un.declare(self, m)

    @abc.abstractmethod
    def get_message(self, m):
        pass


class Usa(Country):
    def get_message(self, m):
        print "USA gets: \"" + m + "\""


class Iraq(Country):
    def get_message(self, m):
        print "Iraq gets: \"" + m + "\""


class UnitedNations:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def declare(self, c, m):
        pass


class SecurityCouncil(UnitedNations):
    def __init__(self):
        self.usa = None
        self.iraq = None

    def declare(self, c, m):
        if c == self.usa:
            self.iraq.get_message(m)
        elif c == self.iraq:
            self.usa.get_message(m)
        else:
            print "err: SecurityCouncil/declare"


if __name__ == "__main__":
    security_council = SecurityCouncil()
    usa = Usa(security_council)
    iraq = Iraq(security_council)
    security_council.usa = usa
    security_council.iraq = iraq
    usa.declare("Stop nuclear weapons")
    iraq.declare("No nuclear here")

================================================
FILE: memento.py
================================================
class StateMemento(object):
    def __init__(self, h, m):
        self.hp = h
        self.mp = m


class GameRole(object):
    def __init__(self):
        self.hp = 100
        self.mp = 100

    def fight(self):
        self.hp = 0
        self.mp = 0

    def create_memento(self):
        return StateMemento(self.hp, self.mp)

    def recovery_state(self, m):
        self.hp = m.hp
        self.mp = m.mp

    def state_display(self):
        print str(self.hp) + " " + str(self.mp)


class StateCaretaker(object):
    def __init__(self, m):
        self.__memento = m  # .__memento ==> ._StateCaretaker__memento (Name Mangling)

    def get_memento(self):
        return self.__memento


if __name__ == "__main__":
    game_role = GameRole()
    state_caretaker = StateCaretaker(game_role.create_memento())
    game_role.state_display()

    game_role.fight()
    game_role.state_display()

    game_role.recovery_state(state_caretaker.get_memento())
    game_role.state_display()

================================================
FILE: observer.py
================================================
import abc


class Observer:
    __metaclass__ = abc.ABCMeta

    def __init__(self, n, nr):
        self.name = n
        self.notifier = nr

    @abc.abstractmethod
    def update(self):
        """updated by notifier"""


class NbaObserver(Observer):
    def update(self):
        print self.name + ", " + self.notifier.state + ", close NBA"


class StockObserver(Observer):
    def update(self):
        print self.name + ", " + self.notifier.state + ", close stock"


class Notifier:
    def __init__(self):
        self.observers = []
        self.state = ""

    def attach(self, o):
        self.observers.append(o)

    def detach(self, o):
        self.observers.remove(o)

    def notify(self):
        for observer in self.observers:
            observer.update()


class Boss(Notifier):
    pass


class Secretary(Notifier):
    pass


if __name__ == "__main__":
    boss = Boss()
    nba_observer = NbaObserver("Bob", boss)
    boss.attach(nba_observer)
    stock_observer = StockObserver("Alice", boss)
    boss.attach(stock_observer)
    boss.state = "boss's back himself"
    boss.notify()


================================================
FILE: prototype.py
================================================
import copy


class WorkExperience:
    def __init__(self):
        self.company = ""
        self.time_area = ""


class Resume:
    def __init__(self, n):
        self.name = n
        self.work_experience = WorkExperience()

    def set_personal_info(self, s, a):
        self.sex = s
        self.age = a

    def set_work_experience(self, c, ta):
        self.work_experience.company = c
        self.work_experience.time_area = ta

    def print_resume(self):
        print self.name + ", " + self.sex + ", " + self.age + ", " \
              + self.work_experience.company + " : " + self.work_experience.time_area

    def clone(self):
        new_resume = copy.deepcopy(self)
        new_resume.work_experience = copy.deepcopy(self.work_experience)
        return new_resume


if __name__ == "__main__":
    resume1 = Resume("Bob")
    resume1.set_personal_info("M", "24")
    resume1.set_work_experience("Google", "2015")
    resume2 = resume1
    resume2.set_personal_info("F", "22")
    resume2.set_work_experience("Twitter", "2015")
    resume1.print_resume()
    resume2.print_resume()

    resume1 = Resume("Bob")
    resume1.set_personal_info("M", "24")
    resume1.set_work_experience("Google", "2015")
    resume2 = copy.copy(resume1)
    resume2.set_personal_info("F", "22")
    resume2.set_work_experience("Twitter", "2015")
    resume1.print_resume()
    resume2.print_resume()

    resume1 = Resume("Bob")
    resume1.set_personal_info("M", "24")
    resume1.set_work_experience("Google", "2015")
    resume2 = copy.deepcopy(resume1)
    resume2.set_personal_info("F", "22")
    resume2.set_work_experience("Twitter", "2015")
    resume1.print_resume()
    resume2.print_resume()

    resume1 = Resume("Bob")
    resume1.set_personal_info("M", "24")
    resume1.set_work_experience("Google", "2015")
    resume2 = resume1.clone()
    resume2.set_personal_info("F", "22")
    resume2.set_work_experience("Twitter", "2015")
    resume1.print_resume()
    resume2.print_resume()


================================================
FILE: proxy.py
================================================
import abc


class GiveGift:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def __init__(self, sg):
        """init"""

    @abc.abstractmethod
    def give_dolls(self):
        """give dolls"""

    @abc.abstractmethod
    def give_flowers(self):
        """give flowers"""


class SchoolGirl:
    def __init__(self, n):
        self.name = n


class Pursuit(GiveGift):
    def __init__(self, sg):
        GiveGift.__init__(self, sg)
        self.school_girl = sg

    def give_dolls(self):
        print "give " + self.school_girl.name + " dolls"

    def give_flowers(self):
        print "give " + self.school_girl.name + " flowers"


class Proxy(GiveGift):
    def __init__(self, sg):
        GiveGift.__init__(self, sg)
        self.pursuit = Pursuit(sg)

    def give_dolls(self):
        self.pursuit.give_dolls()

    def give_flowers(self):
        self.pursuit.give_flowers()


if __name__ == "__main__":
    school_girl = SchoolGirl("Alice")
    proxy = Proxy(school_girl)
    proxy.give_flowers()
    proxy.give_dolls()


================================================
FILE: singleton.py
================================================
import threading


class Singleton(object):
    instance = None
    lock = threading.RLock()

    @classmethod
    def __new__(cls):
        if cls.instance is None:
            cls.lock.acquire()
            if cls.instance is None:
                cls.instance = super(Singleton, cls).__new__(cls)
            cls.lock.release()
        return cls.instance


if __name__ == "__main__":
    instance1 = Singleton()
    instance2 = Singleton()
    print id(instance1) == id(instance2)


================================================
FILE: state.py
================================================
import abc


class Work:
    def __init__(self, h, f):
        self.hour = h
        self.finished = f
        self.state = WorkingState()

    def set_state(self, s):
        self.state = s

    def write_program(self):
        self.state.write_program(self)


class State:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def write_program(self, w):
        """write program according to workstate"""


class RestState(State):
    def write_program(self, w):
        print str(w.hour) + " : return to rest"


class SleepingState(State):
    def write_program(self, w):
        print str(w.hour)+ " : sleeping"


class OvertimeState(State):
    def write_program(self, w):
        if w.finished is True:
            w.set_state(RestState())
            w.write_program()
        elif w.hour < 21:
            print str(w.hour) + " : overtime"
        else:
            w.set_state(SleepingState())
            w.write_program()


class WorkingState(State):
    def write_program(self, w):
        if w.hour < 17:
            print str(w.hour) + " : working"
        else:
            w.set_state(OvertimeState())
            w.write_program()


if __name__ == "__main__":
    work = Work(15, False)
    work.write_program()

    work = Work(20, False)
    work.write_program()

    work = Work(22, False)
    work.write_program()

    work = Work(20, True)
    work.write_program()

================================================
FILE: strategy.py
================================================
import abc


class CashSuper:
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def accept_cash(self, c):
        """caculate cash"""


class CashNormal(CashSuper):
    def accept_cash(self, c):
        return c


class CashRebate(CashSuper):
    def __init__(self, r):
        self.rebate = r

    def accept_cash(self, c):
        return c * self.rebate


class CashReturn(CashSuper):
    def __init__(self, c, r):
        self.condition = c
        self.money = r

    def accept_cash(self, c):
        return c - int(c / self.condition) * self.money


class CashContext:
    def __init__(self, t, s):
        self.cash = None
        if t == "normal":
            self.cash = CashNormal()
        elif t == "rebate":
            self.cash = CashRebate(float(s))
        elif t == "return":
            args = s.split(' ')
            self.cash = CashReturn(float(args[0]), float(args[1]))

    def get_result(self, c):
        return self.cash.accept_cash(c)


if __name__ == "__main__":
    cash_context = CashContext("normal", "")
    print cash_context.get_result(1000)

    cash_context = CashContext("rebate", "0.8")
    print cash_context.get_result(1000)

    cash_context = CashContext("return", "300 100")
    print cash_context.get_result(1000)


================================================
FILE: template_method.py
================================================
import abc


class ExamPaper(object):
    __metaclass__ = abc.ABCMeta

    def question1(self):
        print "question1: " + self.answer1()

    @abc.abstractmethod
    def answer1(self):
        return ""

    def question2(self):
        print "question2: " + self.answer2()

    @abc.abstractmethod
    def answer2(self):
        return ""

    def question3(self):
        print "question3: " + self.answer3()

    @abc.abstractmethod
    def answer3(self):
        return ""


class ExamPaperA(ExamPaper):
    def answer1(self):
        return "a"

    def answer2(self):
        return "a"

    def answer3(self):
        return "b"


class ExamPaperB(ExamPaper):
    def answer1(self):
        return "b"

    def answer2(self):
        return "b"

    def answer3(self):
        return "c"


if __name__ == "__main__":
    test_paper = ExamPaperA()
    print "Paper A:"
    test_paper.question1()
    test_paper.question2()
    test_paper.question3()

    print "Paper B:"
    test_paper = ExamPaperB()
    test_paper.question1()
    test_paper.question2()
    test_paper.question3()


================================================
FILE: visitor.py
================================================
import abc


class Action(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def get_man_conclusion(self, p):
        pass

    @abc.abstractmethod
    def get_woman_conclusion(self, p):
        pass


class Success(Action):
    def get_man_conclusion(self, p):
        print "man success"

    def get_woman_conclusion(self, p):
        print "woman success"


class Failure(Action):
    def get_man_conclusion(self, p):
        print "man failure"

    def get_woman_conclusion(self, p):
        print "woman failure"


class Person(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def accept(self, a):
        pass


class Man(Person):
    def accept(self, a):
        a.get_man_conclusion(self)


class Woman(Person):
    def accept(self, a):
        a.get_woman_conclusion(self)


class ObjectStructure(object):
    def __init__(self):
        self.people = []

    def attach(self, p):
        self.people.append(p)

    def detach(self, p):
        self.people.remove(p)

    def display(self, a):
        for person in self.people:
            person.accept(a)


if __name__ == "__main__":
    man = Man()
    woman = Woman()
    object_structure = ObjectStructure()
    object_structure.attach(man)
    object_structure.attach(woman)
    object_structure.display(Success())
    object_structure.display(Failure())
Download .txt
gitextract_kmnmm8_p/

├── .gitignore
├── README.md
├── abstract_factory.py
├── adapter.py
├── bridge.py
├── builder.py
├── chain_of_responsibility.py
├── command.py
├── composite.py
├── decorator.py
├── facade.py
├── factory_method.py
├── flyweight.py
├── interpreter.py
├── iterator.py
├── mediator.py
├── memento.py
├── observer.py
├── prototype.py
├── proxy.py
├── singleton.py
├── state.py
├── strategy.py
├── template_method.py
└── visitor.py
Download .txt
SYMBOL INDEX (325 symbols across 23 files)

FILE: abstract_factory.py
  class User (line 4) | class User:
  class IUser (line 8) | class IUser:
    method insert_user (line 12) | def insert_user(self, user):
    method get_user (line 16) | def get_user(self):
  class SqlserverUser (line 20) | class SqlserverUser(IUser):
    method insert_user (line 21) | def insert_user(self, user):
    method get_user (line 24) | def get_user(self):
  class AccessUser (line 28) | class AccessUser(IUser):
    method insert_user (line 29) | def insert_user(self, user):
    method get_user (line 32) | def get_user(self):
  class Department (line 36) | class Department:
  class IDepartment (line 40) | class IDepartment:
    method insert_department (line 44) | def insert_department(self, department):
    method get_department (line 47) | def get_department(self):
  class SqlserverDepartment (line 51) | class SqlserverDepartment(IDepartment):
    method insert_department (line 52) | def insert_department(self, department):
    method get_department (line 55) | def get_department(self):
  class AccessDepartment (line 59) | class AccessDepartment(IDepartment):
    method insert_department (line 60) | def insert_department(self, department):
    method get_department (line 63) | def get_department(self):
  class IFactory (line 67) | class IFactory:
    method create_user (line 71) | def create_user(self):
    method create_department (line 75) | def create_department(self):
  class SqlserverFactory (line 79) | class SqlserverFactory(IFactory):
    method create_user (line 80) | def create_user(self):
    method create_department (line 83) | def create_department(self):
  class AccessFactory (line 87) | class AccessFactory(IFactory):
    method create_user (line 88) | def create_user(self):
    method create_department (line 91) | def create_department(self):

FILE: adapter.py
  class Player (line 4) | class Player:
    method __init__ (line 7) | def __init__(self, n):
    method attack (line 11) | def attack(self):
    method defense (line 15) | def defense(self):
  class Center (line 19) | class Center(Player):
    method attack (line 20) | def attack(self):
    method defense (line 23) | def defense(self):
  class ForeignCenter (line 27) | class ForeignCenter:
    method __init__ (line 28) | def __init__(self, n):
    method gong (line 31) | def gong(self):
    method shou (line 34) | def shou(self):
  class Translator (line 38) | class Translator(Player):
    method __init__ (line 39) | def __init__(self, n):
    method attack (line 43) | def attack(self):
    method defense (line 46) | def defense(self):

FILE: bridge.py
  class HandsetSoft (line 4) | class HandsetSoft:
    method run (line 8) | def run(self):
  class HandsetGame (line 12) | class HandsetGame(HandsetSoft):
    method run (line 13) | def run(self):
  class HandsetAddressList (line 17) | class HandsetAddressList(HandsetSoft):
    method run (line 18) | def run(self):
  class HandsetBrand (line 22) | class HandsetBrand:
    method __init__ (line 25) | def __init__(self, s):
    method run (line 29) | def run(self):
  class HandsetBrandM (line 33) | class HandsetBrandM(HandsetBrand):
    method run (line 34) | def run(self):
  class HandsetBrandN (line 39) | class HandsetBrandN(HandsetBrand):
    method run (line 40) | def run(self):

FILE: builder.py
  class Pen (line 4) | class Pen:
  class Graphics (line 8) | class Graphics:
  class PersonBuilder (line 12) | class PersonBuilder:
    method __init__ (line 15) | def __init__(self, p, g):
    method BuildHead (line 20) | def BuildHead(self):
    method BuildBody (line 24) | def BuildBody(self):
  class PersonThinBuilder (line 28) | class PersonThinBuilder(PersonBuilder):
    method BuildHead (line 29) | def BuildHead(self):
    method BuildBody (line 32) | def BuildBody(self):
  class PersonFatBuilder (line 36) | class PersonFatBuilder(PersonBuilder):
    method BuildHead (line 37) | def BuildHead(self):
    method BuildBody (line 40) | def BuildBody(self):
  class PersonDirector (line 44) | class PersonDirector:
    method __init__ (line 45) | def __init__(self, pb):
    method create_person (line 48) | def create_person(self):

FILE: chain_of_responsibility.py
  class Request (line 4) | class Request:
    method __init__ (line 5) | def __init__(self, t, n):
  class Manager (line 10) | class Manager:
    method __init__ (line 13) | def __init__(self, n):
    method set_superior (line 17) | def set_superior(self, s):
    method request_applications (line 21) | def request_applications(self, r):
  class CommonManager (line 25) | class CommonManager(Manager):
    method request_applications (line 26) | def request_applications(self, r):
  class Majordomo (line 33) | class Majordomo(Manager):
    method request_applications (line 34) | def request_applications(self, r):
  class GeneralManager (line 41) | class GeneralManager(Manager):
    method request_applications (line 42) | def request_applications(self, r):

FILE: command.py
  class Barbecuer (line 4) | class Barbecuer(object):
    method bake_mutton (line 5) | def bake_mutton(self):
    method bake_chicken (line 8) | def bake_chicken(self):
  class Command (line 12) | class Command(object):
    method __init__ (line 15) | def __init__(self, r):
    method execute_command (line 19) | def execute_command(self):
  class BakeMuttonCommand (line 23) | class BakeMuttonCommand(Command):
    method execute_command (line 24) | def execute_command(self):
  class BakeChickenCommand (line 28) | class BakeChickenCommand(Command):
    method execute_command (line 29) | def execute_command(self):
  class Waiter (line 33) | class Waiter(object):
    method __init__ (line 34) | def __init__(self):
    method set_order (line 37) | def set_order(self, o):
    method cancel_order (line 44) | def cancel_order(self, o):
    method notify (line 48) | def notify(self):

FILE: composite.py
  class Company (line 4) | class Company:
    method __init__ (line 7) | def __init__(self, n):
    method add (line 11) | def add(self, c):
    method display (line 15) | def display(self, d):
    method line_of_duty (line 19) | def line_of_duty(self):
  class HrDepartment (line 23) | class HrDepartment(Company):
    method __init__ (line 24) | def __init__(self, n):
    method add (line 27) | def add(self, c):
    method display (line 30) | def display(self, d):
    method line_of_duty (line 33) | def line_of_duty(self):
  class FinanceDepartment (line 37) | class FinanceDepartment(Company):
    method __init__ (line 38) | def __init__(self, n):
    method add (line 41) | def add(self, c):
    method display (line 44) | def display(self, d):
    method line_of_duty (line 47) | def line_of_duty(self):
  class ConcreteCompany (line 51) | class ConcreteCompany(Company):
    method __init__ (line 52) | def __init__(self, n):
    method add (line 56) | def add(self, c):
    method display (line 59) | def display(self, d):
    method line_of_duty (line 64) | def line_of_duty(self):

FILE: decorator.py
  class Person (line 4) | class Person:
    method show (line 5) | def show(self):
  class Finery (line 9) | class Finery(Person):
    method __init__ (line 10) | def __init__(self, c):
    method show (line 13) | def show(self):
  class Tie (line 17) | class Tie(Finery):
    method show (line 18) | def show(self):
  class Suit (line 23) | class Suit(Finery):
    method show (line 24) | def show(self):
  class Shoes (line 29) | class Shoes(Finery):
    method show (line 30) | def show(self):

FILE: facade.py
  class Stock1 (line 1) | class Stock1:
    method buy (line 2) | def buy(self):
    method sell (line 5) | def sell(self):
  class Stock2 (line 9) | class Stock2:
    method buy (line 10) | def buy(self):
    method sell (line 13) | def sell(self):
  class Reality1 (line 17) | class Reality1:
    method buy (line 18) | def buy(self):
    method sell (line 21) | def sell(self):
  class Fund (line 25) | class Fund:
    method __init__ (line 26) | def __init__(self):
    method buy (line 31) | def buy(self):
    method sell (line 36) | def sell(self):

FILE: factory_method.py
  class Leifeng (line 4) | class Leifeng:
    method wash (line 8) | def wash(self):
    method sweep (line 12) | def sweep(self):
    method buy_rice (line 16) | def buy_rice(self):
  class Undergraduate (line 20) | class Undergraduate(Leifeng):
    method wash (line 21) | def wash(self):
    method sweep (line 24) | def sweep(self):
    method buy_rice (line 27) | def buy_rice(self):
  class Volunteer (line 31) | class Volunteer(Leifeng):
    method wash (line 32) | def wash(self):
    method sweep (line 35) | def sweep(self):
    method buy_rice (line 38) | def buy_rice(self):
  class IFactory (line 42) | class IFactory:
    method CreateLeifeng (line 46) | def CreateLeifeng(self):
  class UndergraduateFactory (line 50) | class UndergraduateFactory(IFactory):
    method CreateLeifeng (line 51) | def CreateLeifeng(self):
  class VolunteerFactory (line 55) | class VolunteerFactory(IFactory):
    method CreateLeifeng (line 56) | def CreateLeifeng(self):

FILE: flyweight.py
  class User (line 4) | class User:
    method __init__ (line 5) | def __init__(self, n):
  class Website (line 9) | class Website:
    method use (line 13) | def use(self, u):
  class ConcreteWebsite (line 17) | class ConcreteWebsite(Website):
    method __init__ (line 18) | def __init__(self, wn):
    method use (line 21) | def use(self, u):
  class WebsiteFactory (line 25) | class WebsiteFactory:
    method get_website_category (line 28) | def get_website_category(self, n):
    method get_website_count (line 33) | def get_website_count(self):

FILE: interpreter.py
  class Context (line 4) | class Context(object):
    method __init__ (line 5) | def __init__(self, t):
  class Expression (line 9) | class Expression(object):
    method interpret (line 10) | def interpret(self, c):
    method execute (line 18) | def execute(self, k, v):
  class Scale (line 22) | class Scale(Expression):
    method execute (line 23) | def execute(self, k, v):
  class Note (line 28) | class Note(Expression):
    method execute (line 29) | def execute(self, k, v):
  class ExpressionFactory (line 33) | class ExpressionFactory(object):
    method create_expression (line 35) | def create_expression(t):

FILE: iterator.py
  class Aggregate (line 4) | class Aggregate:
    method create_iterator (line 8) | def create_iterator(self):
  class List (line 12) | class List(Aggregate):
    method __init__ (line 13) | def __init__(self):
    method create_iterator (line 16) | def create_iterator(self):
    method count (line 19) | def count(self):
    method __setitem__ (line 22) | def __setitem__(self, key, value):
    method __getitem__ (line 28) | def __getitem__(self, i):
  class Iterator (line 32) | class Iterator:
    method first_item (line 36) | def first_item(self):
    method next_item (line 40) | def next_item(self):
    method is_done (line 44) | def is_done(self):
    method current_item (line 48) | def current_item(self):
  class ListIterator (line 51) | class ListIterator(Iterator):
    method __init__ (line 52) | def __init__(self, a):
    method first_item (line 56) | def first_item(self):
    method next_item (line 59) | def next_item(self):
    method is_done (line 66) | def is_done(self):
    method current_item (line 69) | def current_item(self):

FILE: mediator.py
  class Country (line 4) | class Country(object):
    method __init__ (line 7) | def __init__(self, un):
    method declare (line 10) | def declare(self, m):
    method get_message (line 14) | def get_message(self, m):
  class Usa (line 18) | class Usa(Country):
    method get_message (line 19) | def get_message(self, m):
  class Iraq (line 23) | class Iraq(Country):
    method get_message (line 24) | def get_message(self, m):
  class UnitedNations (line 28) | class UnitedNations:
    method declare (line 32) | def declare(self, c, m):
  class SecurityCouncil (line 36) | class SecurityCouncil(UnitedNations):
    method __init__ (line 37) | def __init__(self):
    method declare (line 41) | def declare(self, c, m):

FILE: memento.py
  class StateMemento (line 1) | class StateMemento(object):
    method __init__ (line 2) | def __init__(self, h, m):
  class GameRole (line 7) | class GameRole(object):
    method __init__ (line 8) | def __init__(self):
    method fight (line 12) | def fight(self):
    method create_memento (line 16) | def create_memento(self):
    method recovery_state (line 19) | def recovery_state(self, m):
    method state_display (line 23) | def state_display(self):
  class StateCaretaker (line 27) | class StateCaretaker(object):
    method __init__ (line 28) | def __init__(self, m):
    method get_memento (line 31) | def get_memento(self):

FILE: observer.py
  class Observer (line 4) | class Observer:
    method __init__ (line 7) | def __init__(self, n, nr):
    method update (line 12) | def update(self):
  class NbaObserver (line 16) | class NbaObserver(Observer):
    method update (line 17) | def update(self):
  class StockObserver (line 21) | class StockObserver(Observer):
    method update (line 22) | def update(self):
  class Notifier (line 26) | class Notifier:
    method __init__ (line 27) | def __init__(self):
    method attach (line 31) | def attach(self, o):
    method detach (line 34) | def detach(self, o):
    method notify (line 37) | def notify(self):
  class Boss (line 42) | class Boss(Notifier):
  class Secretary (line 46) | class Secretary(Notifier):

FILE: prototype.py
  class WorkExperience (line 4) | class WorkExperience:
    method __init__ (line 5) | def __init__(self):
  class Resume (line 10) | class Resume:
    method __init__ (line 11) | def __init__(self, n):
    method set_personal_info (line 15) | def set_personal_info(self, s, a):
    method set_work_experience (line 19) | def set_work_experience(self, c, ta):
    method print_resume (line 23) | def print_resume(self):
    method clone (line 27) | def clone(self):

FILE: proxy.py
  class GiveGift (line 4) | class GiveGift:
    method __init__ (line 8) | def __init__(self, sg):
    method give_dolls (line 12) | def give_dolls(self):
    method give_flowers (line 16) | def give_flowers(self):
  class SchoolGirl (line 20) | class SchoolGirl:
    method __init__ (line 21) | def __init__(self, n):
  class Pursuit (line 25) | class Pursuit(GiveGift):
    method __init__ (line 26) | def __init__(self, sg):
    method give_dolls (line 30) | def give_dolls(self):
    method give_flowers (line 33) | def give_flowers(self):
  class Proxy (line 37) | class Proxy(GiveGift):
    method __init__ (line 38) | def __init__(self, sg):
    method give_dolls (line 42) | def give_dolls(self):
    method give_flowers (line 45) | def give_flowers(self):

FILE: singleton.py
  class Singleton (line 4) | class Singleton(object):
    method __new__ (line 9) | def __new__(cls):

FILE: state.py
  class Work (line 4) | class Work:
    method __init__ (line 5) | def __init__(self, h, f):
    method set_state (line 10) | def set_state(self, s):
    method write_program (line 13) | def write_program(self):
  class State (line 17) | class State:
    method write_program (line 21) | def write_program(self, w):
  class RestState (line 25) | class RestState(State):
    method write_program (line 26) | def write_program(self, w):
  class SleepingState (line 30) | class SleepingState(State):
    method write_program (line 31) | def write_program(self, w):
  class OvertimeState (line 35) | class OvertimeState(State):
    method write_program (line 36) | def write_program(self, w):
  class WorkingState (line 47) | class WorkingState(State):
    method write_program (line 48) | def write_program(self, w):

FILE: strategy.py
  class CashSuper (line 4) | class CashSuper:
    method accept_cash (line 8) | def accept_cash(self, c):
  class CashNormal (line 12) | class CashNormal(CashSuper):
    method accept_cash (line 13) | def accept_cash(self, c):
  class CashRebate (line 17) | class CashRebate(CashSuper):
    method __init__ (line 18) | def __init__(self, r):
    method accept_cash (line 21) | def accept_cash(self, c):
  class CashReturn (line 25) | class CashReturn(CashSuper):
    method __init__ (line 26) | def __init__(self, c, r):
    method accept_cash (line 30) | def accept_cash(self, c):
  class CashContext (line 34) | class CashContext:
    method __init__ (line 35) | def __init__(self, t, s):
    method get_result (line 45) | def get_result(self, c):

FILE: template_method.py
  class ExamPaper (line 4) | class ExamPaper(object):
    method question1 (line 7) | def question1(self):
    method answer1 (line 11) | def answer1(self):
    method question2 (line 14) | def question2(self):
    method answer2 (line 18) | def answer2(self):
    method question3 (line 21) | def question3(self):
    method answer3 (line 25) | def answer3(self):
  class ExamPaperA (line 29) | class ExamPaperA(ExamPaper):
    method answer1 (line 30) | def answer1(self):
    method answer2 (line 33) | def answer2(self):
    method answer3 (line 36) | def answer3(self):
  class ExamPaperB (line 40) | class ExamPaperB(ExamPaper):
    method answer1 (line 41) | def answer1(self):
    method answer2 (line 44) | def answer2(self):
    method answer3 (line 47) | def answer3(self):

FILE: visitor.py
  class Action (line 4) | class Action(object):
    method get_man_conclusion (line 8) | def get_man_conclusion(self, p):
    method get_woman_conclusion (line 12) | def get_woman_conclusion(self, p):
  class Success (line 16) | class Success(Action):
    method get_man_conclusion (line 17) | def get_man_conclusion(self, p):
    method get_woman_conclusion (line 20) | def get_woman_conclusion(self, p):
  class Failure (line 24) | class Failure(Action):
    method get_man_conclusion (line 25) | def get_man_conclusion(self, p):
    method get_woman_conclusion (line 28) | def get_woman_conclusion(self, p):
  class Person (line 32) | class Person(object):
    method accept (line 36) | def accept(self, a):
  class Man (line 40) | class Man(Person):
    method accept (line 41) | def accept(self, a):
  class Woman (line 45) | class Woman(Person):
    method accept (line 46) | def accept(self, a):
  class ObjectStructure (line 50) | class ObjectStructure(object):
    method __init__ (line 51) | def __init__(self):
    method attach (line 54) | def attach(self, p):
    method detach (line 57) | def detach(self, p):
    method display (line 60) | def display(self, a):
Condensed preview — 25 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (34K chars).
[
  {
    "path": ".gitignore",
    "chars": 6,
    "preview": ".idea/"
  },
  {
    "path": "README.md",
    "chars": 407,
    "preview": "# design-patterns\n《大话设计模式》中23种设计模式案例的Python版本实现。样例忠于原书,某些地方根据Python特性做了修改,如abstract、private、reflection等。\n\n\n## 读书笔记\n* [创建"
  },
  {
    "path": "abstract_factory.py",
    "chars": 2489,
    "preview": "import abc\n\n\nclass User:\n    \"\"\"class user\"\"\"\n\n\nclass IUser:\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n  "
  },
  {
    "path": "adapter.py",
    "chars": 1178,
    "preview": "import abc\n\n\nclass Player:\n    __metaclass__ = abc.ABCMeta\n\n    def __init__(self, n):\n        self.name = n\n\n    @abc.a"
  },
  {
    "path": "bridge.py",
    "chars": 1052,
    "preview": "import abc\n\n\nclass HandsetSoft:\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def run(self):\n        \"\"\"s"
  },
  {
    "path": "builder.py",
    "chars": 1273,
    "preview": "import abc\n\n\nclass Pen:\n    \"\"\"class pen\"\"\"\n\n\nclass Graphics:\n    \"\"\"class graphics\"\"\"\n\n\nclass PersonBuilder:\n    __meta"
  },
  {
    "path": "chain_of_responsibility.py",
    "chars": 1646,
    "preview": "import abc\n\n\nclass Request:\n    def __init__(self, t, n):\n        self.type = t\n        self.number = n\n\n\nclass Manager:"
  },
  {
    "path": "command.py",
    "chars": 1377,
    "preview": "import abc\n\n\nclass Barbecuer(object):\n    def bake_mutton(self):\n        print \"bake mutton\"\n\n    def bake_chicken(self)"
  },
  {
    "path": "composite.py",
    "chars": 2311,
    "preview": "import abc\n\n\nclass Company:\n    __metaclass__ = abc.ABCMeta\n\n    def __init__(self, n):\n        self.name = n\n\n    @abc."
  },
  {
    "path": "decorator.py",
    "chars": 615,
    "preview": "import abc\n\n\nclass Person:\n    def show(self):\n        print \"person\"\n\n\nclass Finery(Person):\n    def __init__(self, c):"
  },
  {
    "path": "facade.py",
    "chars": 754,
    "preview": "class Stock1:\n    def buy(self):\n        print \"buy stock1\"\n\n    def sell(self):\n        print \"sell stock1\"\n\n\nclass Sto"
  },
  {
    "path": "factory_method.py",
    "chars": 1345,
    "preview": "import abc\n\n\nclass Leifeng:\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def wash(self):\n        \"\"\"\"was"
  },
  {
    "path": "flyweight.py",
    "chars": 1209,
    "preview": "import abc\n\n\nclass User:\n    def __init__(self, n):\n        self.name = n\n\n\nclass Website:\n    __metaclass__ = abc.ABCMe"
  },
  {
    "path": "interpreter.py",
    "chars": 984,
    "preview": "import abc\n\n\nclass Context(object):\n    def __init__(self, t):\n        self.text = t\n\n\nclass Expression(object):\n    def"
  },
  {
    "path": "iterator.py",
    "chars": 1784,
    "preview": "import abc\n\n\nclass Aggregate:\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def create_iterator(self):\n  "
  },
  {
    "path": "mediator.py",
    "chars": 1163,
    "preview": "import abc\n\n\nclass Country(object):\n    __metaclass__ = abc.ABCMeta\n\n    def __init__(self, un):\n        self.un = un\n\n "
  },
  {
    "path": "memento.py",
    "chars": 987,
    "preview": "class StateMemento(object):\n    def __init__(self, h, m):\n        self.hp = h\n        self.mp = m\n\n\nclass GameRole(objec"
  },
  {
    "path": "observer.py",
    "chars": 1107,
    "preview": "import abc\n\n\nclass Observer:\n    __metaclass__ = abc.ABCMeta\n\n    def __init__(self, n, nr):\n        self.name = n\n     "
  },
  {
    "path": "prototype.py",
    "chars": 1997,
    "preview": "import copy\n\n\nclass WorkExperience:\n    def __init__(self):\n        self.company = \"\"\n        self.time_area = \"\"\n\n\nclas"
  },
  {
    "path": "proxy.py",
    "chars": 1047,
    "preview": "import abc\n\n\nclass GiveGift:\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def __init__(self, sg):\n      "
  },
  {
    "path": "singleton.py",
    "chars": 485,
    "preview": "import threading\n\n\nclass Singleton(object):\n    instance = None\n    lock = threading.RLock()\n\n    @classmethod\n    def _"
  },
  {
    "path": "state.py",
    "chars": 1394,
    "preview": "import abc\n\n\nclass Work:\n    def __init__(self, h, f):\n        self.hour = h\n        self.finished = f\n        self.stat"
  },
  {
    "path": "strategy.py",
    "chars": 1270,
    "preview": "import abc\n\n\nclass CashSuper:\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def accept_cash(self, c):\n   "
  },
  {
    "path": "template_method.py",
    "chars": 1093,
    "preview": "import abc\n\n\nclass ExamPaper(object):\n    __metaclass__ = abc.ABCMeta\n\n    def question1(self):\n        print \"question1"
  },
  {
    "path": "visitor.py",
    "chars": 1363,
    "preview": "import abc\n\n\nclass Action(object):\n    __metaclass__ = abc.ABCMeta\n\n    @abc.abstractmethod\n    def get_man_conclusion(s"
  }
]

About this extraction

This page contains the full source code of the yogykwan/design-patterns-py GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 25 files (29.6 KB), approximately 7.8k tokens, and a symbol index with 325 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!