示例#1
0
        static void Main(string[] args)
        {
            TargetClass target = new AdapterClass();

            // 用 适配器类 的实例调用的是 某一个不相关的类的某个方法 ->
            // 一般是一个现有的类,接口不是很符合要求,用适配器去进行扩展
            // 说白了就是一个类封装了一个另一个无关类的实例
            target.Request();
        }
示例#2
0
        public static void Main(string[] args)
        {
            // Whip up the target
            AdapteeClass target = new AdapteeClass();

            // Whip up the adapter for the target
            ISomeInterface clientToTargetAdapter = new AdapterClass(target);

            // Whip up the client that was not able to inject the target class
            // before but now can thanks to the clientToTarget_adapter.
            ClassThatWantsToUseAdapteeClass client =
                new ClassThatWantsToUseAdapteeClass(clientToTargetAdapter);

            client.TheThingThatYouHaveAlwaysDoneButNowWantToDoWithAdaptee();

            // voila, and you didn't change the code for your Client class or
            // your AdapteeClass class (if you were even able to in the first
            // place).

            // Example where we prepare for a future version of the Adaptee
            // where the method signature of a method we are using in that
            // library is going to change. So we change to the new method
            // signature prematurely using an Adapter.

            // Already have an AdapteeClass

            // Whip up the adapter that swaps the method signature
            ISomeInterfaceWithArgs methodSigAdapter =
                new MethodSigAdapterClass(target);

            ClassThatWantsToUseNewArgumentOrder client2 =
                new ClassThatWantsToUseNewArgumentOrder(methodSigAdapter);

            // Calling with new syntax
            client2.TheMethodYouWantToCallButWithFutureSyntax(5, "hi");
        }