Design Mode 8 -- adapter Mode

Source: Internet
Author: User

Design Mode directory:

Design Pattern 1 -- Observer Pattern

Design Pattern 2 -- decorator pattern

Design Pattern 3 -- iterator and combination pattern (iterator)

Design Pattern 4 -- iterator and combination pattern (combination)

Design Mode 5-work mode

Design Mode 6-single-piece Mode

Design Mode 7 -- command mode

Design Mode 8 -- adapter Mode

 

Overview

Part 1 Introduction

Part 1 Implementation

Part 1 Definition

 

Part 1 Introduction

What is the OO adapter? In reality, it is everywhere. For example, if you want to use a laptop made in the United States in European countries, you may need an AC adapter. In fact, the OO adapter and the real-world adapter play the same role; convert one interface to another to meet the customer's expectations.

 

Part 1 Implementation

See the adapter implementation in use:

The first is the Duck interface:

 1 /** 2  * 鸭子接口 3  * @ClassName: Duck 4  * TODO 5  * @author Xingle 6  * @date 2014-10-9 下午3:33:04 7  */ 8 public interface Duck { 9     10     public void quack();11     public void fly();12 13 }

 

A child of a duck

 1 /** 2  * 绿头鸭  3  * @ClassName: MallardDuck 4  * TODO 5  * @author Xingle 6  * @date 2014-10-9 下午3:38:54 7  */ 8 public class MallardDuck implements Duck{ 9 10     @Override11     public void quack() {12         System.out.println("Quack");13     }14 15     @Override16     public void fly() {17         System.out.println("I‘m flying.");18     }19 20 }

 

Turkey:

 1 /** 2  * 火鸡 3  * @ClassName: Turkey 4  * TODO 5  * @author Xingle 6  * @date 2014-10-9 下午3:54:37 7  */ 8 public interface Turkey { 9     10     public void gobble();11     public void fly();12 13 }

 

 

 1 /** 2  * 火鸡的具体实现类 3  * @ClassName: WildTurkey 4  * TODO 5  * @author Xingle 6  * @date 2014-10-9 下午3:55:23 7  */ 8 public class WildTurkey implements Turkey{ 9 10     @Override11     public void gobble() {12         System.out.println("Gobble gobble.");13     }14 15     @Override16     public void fly() {17         System.out.println("I‘m flying a short distance.");18     }19 20 }

 

Duck adapter:

 1 /** 2  * 鸭子适配器 3  * @ClassName: TurkeyAdapter 4  * TODO 5  * @author Xingle 6  * @date 2014-10-9 下午3:58:12 7  */ 8 public class TurkeyAdapter implements Duck{ 9     10     Turkey turkey;11     12     public TurkeyAdapter(Turkey turkey){13         this.turkey = turkey;14     }15 16     @Override17     public void quack() {18         turkey.gobble();19     }20 21     @Override22     public void fly() {23         for(int i=0;i<5;i++){24             turkey.fly();25         }26         27     }28 29 }

 

Test procedure:

 1 /** 2  *  3  * @ClassName: TurkeyTestDrive 4  * TODO 5  * @author Xingle 6  * @date 2014-10-9 下午4:03:21 7  */ 8 public class TurkeyTestDrive { 9     public static void main(String[] args){10         MallardDuck duck = new MallardDuck();11         WildTurkey turkey = new WildTurkey();12         13         Duck turkeyAdapter = new TurkeyAdapter(turkey);14         System.out.println("The Turkey says...");15         turkey.gobble();16         turkey.fly();17         18         System.out.println("\nThe Duck says...");19         testDuck(duck);20         21         System.out.println("\nThe TurkeyAdapter says...");22         testDuck(turkeyAdapter);23     }24 25      static void testDuck(Duck duck) {26          duck.quack();27          duck.fly();28     }29 30 }

 

Execution result:

 

Part 1 Definition

The adapter is

Design Mode 8 -- adapter Mode

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.