Example of the Python design Pattern Adapter Pattern

Source: Internet
Author: User
Tags in python

Python design mode

Three types:

1. Creation type
* Flexible object creation *
-Abstract factory
-Builder
-Factory method
-Prototype
-Singleton
2. Structural
* Convert one object to another, or combine small objects into large objects *
-Adapter
-Bridging
-Combination
-Modifier
-Appearance
-Enjoy RMB
-Proxy
3. Behavior type
* Focus on the process of work, interaction between algorithms and objects *
-Responsibility chain
-Command
-Interpreter
-Iterator
-Intermediary
-Memorandum
-Observer
-Status
-Policy
-Template method
-Visitor

Adapter mode Adapter Pattern

Concept:

It is an interface adaptation technology that allows you to use a class that is incompatible with another interface.
During use, the interfaces of both classes do not need to be modified.
Scenario:

Put a class out of its original application scenario and run it in another environment, and this class cannot be modified.
Example:

Create a person and click it to display the name and talk.

The code is as follows: Copy code

Class Person (object): "A representation of a person in 2D Land" def init (self, name): self. name = name def make_noise (self): return "hello"

Def click_creature (creature): "" React to a click by retrieving the creature's name and what is says "return creature. name, creature. make_noise ()

Create a dog

The code is as follows: Copy code

Class Dog (object): "A representation of a dog in 2D Land" def init (self, name): self. name = name def bark (self): return "woof"

The dog only has bark and does not have make_noise. It must be adapted.

The code is as follows: Copy code

The Class Adapter
The Object Adapter
The Class Adapter
From dog import Dog
Class Creature (object ):
"The base class for creatures in 2D Land """
Def make_noise (self ):
"""
This is a technique to fake an ABC
In Python 2.X
"""
Raise NotImplementedError

Class Person (Creature ):
"A representation of a person in 2D Land """
Def _ init _ (self, name ):
Self. name = name

Def make_noise (self ):
Return "hello"

Class DogClassAdapter (Creature, Dog ):
"Adapts the Dog class through multiple inheritance """
Def _ init _ (self, name ):
Dog. _ init _ (self, name)

Def make_noise (self ):
"""
Provide the 'make _ noise 'method that
The client expects
"""
Return self. bark ()
The Object Adapter
Class DogObjectAdapter (Creature ):
"Adapts the Dog class through encapsulation """
Def _ init _ (self, canine ):
Self. canine = canine
Def make_noise (self ):
"This is the only method that's adapted """
Return self. canine. bark ()
Def _ getattr _ (self, attr ):
"Everything else is delegated to the object """
Return getattr (self. canine, attr)
* Simple *
From dog import Dog
Class Person (object ):
"A representation of a person in 2D Land """
Def _ init _ (self, name ):
Self. name = name

Def make_noise (self ):
Return "hello"

Class DogAdapter (object ):
"Adapts the Dog class through encapsulation """
Def _ init _ (self, canine ):
Self. canine = canine

Def make_noise (self ):
"This is the only method that's adapted """
Return self. canine. bark ()

Def _ getattr _ (self, attr ):
"Everything else is delegated to the object """
Return getattr (self. canine, attr)

Def click_creature (creature ):
"""
React to a click by showing the creature's
Name and what is says
"""

Return (creature. name, creature. make_noise ())
Test
From dog import Dog
From listing3 import Person, DogAdapter
Def exercise_system ():
Person = Person ("Bob ")
Canine = DogAdapter (Dog ("Fido "))

For critter in (person, canine ):

Print critter. name, "says", critter. make_noise ()

If _ name _ = "_ main __":
Exercise_system ()

Create more creatures, cats, birds...
####

The code is as follows: Copy code

Class Cat (object ):
"A representation of a cat in 2D Land """
Def _ init _ (self, name ):
Self. name = name
Def meow (self ):
Return "meow"


Class CreatureAdapter (object ):
"Adapts a creature for clients in 2D Land """
Def _ init _ (self, creature, make_noise ):
"Pass in the function to use as 'Make _ noise '"""
Self. creature = creature
Self. make_noise = make_noise
Def _ getattr _ (self, attr ):
"Everything else is delegated to the object """
Return getattr (self. creature, attr)

Test

The code is as follows: Copy code


From dog import Dog
From cat import Cat
From twodeeland import Person, CreatureAdapter
Def exercise_system ():
Person = Person ("Bob ")
Fido = Dog ("Fido ")
Canine = CreatureAdapter (fido, fido. bark)
Whiskers = Cat ("Whiskers ")
Feline = CreatureAdapter (whiskers, whiskers. meow)

For critter in (person, canine, feline ):
Print critter. name, "says", critter. make_noise

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.