In the NBA I need to translate adapter mode

17.1 I need to translate the NBA!

17.2 Adapter mode

Adapter, a class of the client interface into another interface desired, such as those based Adapter mode otherwise because of incompatible interfaces can not work together with the work,

Some countries voltage 110v, and our voltage 220v, not a laptop voltage can be used, so there is a power adapter this thing,

Data and behavior of the system are correct, but does not match the interface, we should consider the use of adapters, the purpose is to make a original objects outside the control of the match with an interface adapter mode is mainly used want to reuse some of the existing classes, but the interface and multiplexing environmental requirements inconsistent,

namespace adapter mode 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            Target target = new new Adapter ();    // for the client, Request Target method is invoked, 
            target.Request (); 

            Console.Read (); 
        } 
    } 

    // customers expect the interface, the target may be specific or abstract class, an interface may be, 
    class the target 
    { 
        public  Virtual  void the request () 
        { 
            Console.WriteLine ( " normal request " ); 
        }
    }

    // adapter required classes, 
    class Adaptee 
    { 
        public  void specificRequest () 
        { 
            Console.WriteLine ( " Special Request " ); 
        } 
    } 

    // through a Adaptee object inside the package, the interface is configured to convert the source destination interface, 
    class Adapter: the Target 
    { 
        Private the Adaptee Adaptee = new new the Adaptee (); 

        public  the override  void Request () 
        { 
            adaptee.SpecificRequest ();    // This method can be invoked Request actually calls into the upper surface SpecificRequest method, 
        } 
    } 

}
View Code

17.3 When to use an adapter pattern

Use an existing class, but if it's the interface, that is, its methods and your request is not the same, you should consider Adapter mode? Right, two classes have done the same thing or similar, but have different interfaces when to use it, and because classes share the same interface, unified client code can call the same interface on the line, so you can be more simple, more direct and more compact,

17.4 Basketball translation adapter

namespace adapter mode 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            Player B = new new the Forwards ( " Shane " ); 
            b.Attack (); 

            Player m = new new Guards ( " McGrady " ); 
            m .Attack (); 

            // Player YM = new new Center ( "Yao Ming"); 

            Player YM = new new Translator ( " Yao Ming " );  
            ym.Attack ();
            ym.Defense ();
 
            Console.Read (); 
        } 
    } 

    // abstract class basketball player, 
    abstract  class Player 
    { 
        protected  String name; 

        public Player ( String name) 
        { 
            the this .name = name; 
        } 

        // attack, 
        public  abstract  void Attack (); 

        // defense, 
        public  abstract  void defense (); 
    } 

    // forward classes, 
    class the forwards: Player 
    { 
        public the forwards ( String name) 
            : Base (name) 
        { 

        } 

        public  the override  void Attack () 
        { 
            Console.WriteLine ( " forward attack {0} " , name); 
        } 

        public  the override  void Defense () 
        { 
            Console.WriteLine ( " forward defense {0} " , name); 
        } 
    } 

    // center class, 
    class Center: Player 
    { 
        public Center ( String name) 
            : Base (name)
        { 

        } 

        Public  the override  void Attack () 
        { 
            Console.WriteLine ( " center attack {0} " , name); 
        } 

        public  the override  void Defense () 
        { 
            Console.WriteLine ( " center defense {0} " , name); 
        } 
    } 

    / / back type, 
    class guards: Player 
    { 
        public guards ( String name) 
            : Base (name) 
        { 

        }

        public  the override  void Attack () 
        { 
            Console.WriteLine ( " guard attack {0} " , name); 
        } 

        public  the override  void Defense () 
        { 
            Console.WriteLine ( " guard defense {0} " , name); 
        } 
    } 

    // foreign center type, 
    class ForeignCenter 
    { 
        Private  String name; 

        public  String the name 
        { 
            GET { return name;}
             SET {name =value;} 
        } 

        public  void attack () 
        { 
            Console.WriteLine ( " foreign center attack {0} " , name); 
        } 

        public  void Defense () 
        { 
            Console.WriteLine ( " foreign defense center {0} " , name); 
        } 
    } 

    // translator class, adapter, 
    class translator: Player 
    { 
        Private ForeignCenter wjzf = new new ForeignCenter (); 

        public translator ( String name) 
            : Base  (name)
        { 
            Wjzf.Name = name; 
        } 

        public  the override  void Attack () 
        { 
            wjzf attack (); 
        } 

        public  the override  void Defense () 
        { 
            wjzf Defense (); 
        } 
    } 

}
View Code

17.5 Adapter pattern of .NET applications

For example, there is a class library has achieved very important in .NET adapter that adapter between the DataAdapter, DataAdapter and DataSet and used as a data source for retrieving and saving data,

17.6 Bian Que medicine

Guess you like

Origin www.cnblogs.com/huangxuQaQ/p/11289203.html