Head First design mode adapter mode and appearance mode

Source: Internet
Author: User

Head First design mode adapter mode and appearance mode

Objective:

Before we've talked about decorator patterns, wrapping objects and giving new responsibilities, this chapter also wraps objects, just to make them look less like themselves and something else. This allows the interface of the class to be transformed into the desired interface in the design to implement the same interface, and another pattern will be described to wrap the object to simplify the interface.

1. Adapter Introduction 1.1 Object-oriented adapter

Real-world adapters such as AC adapters located between the American Plug and the European socket. What is an object-oriented adapter?

An object-oriented adapter is the conversion of an interface into another interface to meet customer expectations.

For example, there is a software system, I hope it can be used with a new vendor class library, but this vendor design interface is different from the old interface, and you do not want to change the existing code, so this time need an adapter to complete the existing system and the new vendor class library docking.

1.2 In-Use adapter

We're talking about ducks in strategy mode. Look at a simplified version of the example.

1.2.1 Duck Interface

Publicinterface Duck    {       void quack ();//duck called       Void Fly ();//Duck Fly}

1.2.2 Duck Sub-category (green-headed Duck) realization

Publicclass Mallardduck:duck    {public        void quack ()        {            Console.WriteLine ("Quack");        }        public void Fly ()        {            Console.WriteLine ("Fly");}        }

1.2.3 New poultry interface and specific implementation

Publicinterface Turkey    {        void gobble ();//giggle       void Fly ();//Fly}publicclass Wildturkey:turkey    { Public        void Gobble ()        {            Console.WriteLine ("Gobblegobble");        }        public void Fly ()        {            Console.WriteLine ("I ' m Flyinga short distance");        }    }

1.2.4 Adapter

Publicclass turkeyadapter:duck//implements the type interface    {        Turkey Turkey that you want to convert to;        Public Turkeyadapter (Turkey Turkey)//Get the object reference of the adapter and use the construct to get this reference        {            this.turkey = Turkey;        }         public void Quack ()//Implements all methods in the interface        {            turkey. Gobble ();        }         public void Fly ()        {for            (var i=0;i<5;i++)            Turkey. Fly ();        }}

1.2.5 Test

Mallardduckduck = new Mallardduck ();            Wildturkey Turkey = Newwildturkey ();            Duck Turkeyadapter = Newturkeyadapter (Turkey);             Console.WriteLine ("The Turkeysyas ...");            Turkey. Gobble ();            Turkey. Fly ();             Console.WriteLine ("The Ducksays ...");            Turkeyadapter.quack ();            Turkeyadapter.fly ();             Console.read ();

The results are as follows:

1.3 Adapter Mode parsing

The process for customers to use the adapter is as follows:

The client makes a request to the adapter by invoking the adapter's method through the target interface.

The adapter uses the adaptor interface to translate the request into one or more of the calling interfaces of the adapter.

The result of the call received by the customer.

2. Define the adapter mode

Adapter mode: Transforms the interface of a class into another interface that the customer expects. Adapters allow classes that are incompatible with the original interface to work seamlessly.

The pattern can be transformed by creating an adapter to allow incompatible interfaces to become compatible, allowing the client to decouple from the implemented interface. The class diagram is as follows:

The adapter is full of OO design principles: Use object combinations to modify the interface wrapper to the adapter. Any sub-class that is a companion can be used with an adapter. The pattern is to bind the client and the interface, not the implementation.

There are actually two types of adapters: Object adapters and Class adapters. The class adapter is implemented through multiple inheritance, and the object adapter uses a combination of methods to pass the request to the appropriate person.

Class Adapter diagram:

3. Define the Appearance mode

Appearance mode: Provides a unified interface for accessing a group of interfaces for a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use.

The appearance mode allows us to avoid tight coupling between the customer and the subsystem. The class diagram is as follows:

4. Minimum knowledge principle

Least knowledge Principle: Talk to your closest friend only.

The least-knowledge principle tells us to reduce the interaction between objects. This principle hopes that when designing, do not let too many classes to be coupled together, lest modify part of the system, will affect other parts.

How do you avoid having too many classes coupled together? There are mainly the following policies:

In terms of objects, within the method of the object, we should only invoke methods that fall into the following scopes:

L The object itself

An object that is passed in as a parameter to a method

L any object created or instantiated by this method

L Any component of the object

The following example:

(Do not use this principle) get the thermometer object from the weather station and then get the problem from the thermometer object

Publicfloat gettemp () {   thermometerthermometer=station. Getthermometer ();   return thermometer. Gettemperature ();}

(using this principle) we added a method to the weather station to request temperature from the thermometer. Can reduce the data of the classes we depend on.

Publicfloat gettemp () {  Return station. Gettemperature ();}

5. Summary

When you need to use an existing class and its interface does not meet your needs, you need to use an adapter.

L Use appearances when it is necessary to simplify and unify a large interface or a group of responsible interfaces.

L Adapter changes the interface to meet customer expectations.

L appearance decouple the customer from a complex subsystem.

L The difficulty of implementing an adapter depends on the size and complexity of the target interface.

L Implement a look that requires the subsystem to be combined into the skin, and then delegate the work to the subsystem for execution.

The adapter is divided into object adapters and class adapters, and class adapters require multiple inheritance.

L can achieve more than one appearance for a subsystem.

The adapter wraps an object to change its interface; The decorator wraps an object to add new behaviors and responsibilities, while the façade "wraps" a group of objects to simplify its interface.

Head First design mode adapter mode and appearance 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.