Translation Tipatterns - Connect different types (Connecting DiffERENT TYPES)

xiaoxiao2021-03-06  103

Connect different types (Connecting Differen)

Adapter

Adaper accepts a type and generates an interface for other types. When you have a class in your hand, you need it is another class (and you need That), you can solve the problem with Adapter. The only thing that needs to be done is to generate the class you need, and there are many ways to complete this match.

//: Adapter: SimpleAdapter.java

// "Object Adapter" from Gof Diagram

Package adapter;

Import junit.framework. *;

Class target {

Public void request () {}

}

Class adaptee {

Public void specificrequest () {

System.out.println ("Adaptee: SpecificRequest");

}

}

Class Adapter Extends Target {

Private adaptee adaptee;

Public Adapter (Adaptee a) {

Adaptee = a;

}

PUBLIC VOID Request () {

Adaptee.specificRequest ();

}

}

Public class SimpleAdapter Extends Testcase {

Adaptee a = new adaptee ();

Target T = New Adapter (A);

Public void test () {

T.REQUEST ();

}

Public static void main (string args []) {

Junit.textui.teStrunner.run (SimpleAdapter.class);

}

} ///: ~

//: Adapter: AdapterVariations.java

// Variations on the adapter pattern.

Package adapter;

Import junit.framework. *;

Class whatihave {

Public void g () {}

Public void h () {}

}

Interface whatiwant {

Void f ();

}

Class SurrogateAdapter Implements WHATIWANT {

WHATIHAVE WHATIHAVE;

Public SurrogateAdapter (Whatihave Wih) {

WHATIHAVE = WiH;

}

Public void f () {

//Mplement Behavior Using

//Methods in whatihave:

Whatihave.g ();

Whatihave.h ();

}

}

Class whatiuse {

Public void op (WHAT WIW) {

Wiw.f ();

}

}

// Approach 2: Build Adapter USE INTOOP ():

Class whatiuse2 extends whatiuse {

Public void op (WHATIHAVE WIH) {

New SurrogateAdapter (WiH) .f ();

}

}

// approach 3: Build Adapter Into Whatihave:

Class Whatihave2 Extends WhatihaveImplements WhatiWant {

Public void f () {

g ();

h ();

}

}

// Approach 4: Use an inner class:

Class whatihave3 extends whatihave {

Private class inneraDapter imports whatiwant {

Public void f () {

g ();

h ();

}

}

Public whatiwant whatiwant () {

Return new inneradapter ();

}

}

Public Class Adaptervariations Extends Testcase {

Whatiuse whatiuse = new whatiuse ();

Whatihave WHATIHAVE = New Whatihave ();

Whatwant Adapt = New SurrogateAdapter (WHATIHAVE);

What2 WhatIuse2 = new whatiuse2 ();

Whatihave2 Whatihave2 = new whatihave2 ();

Whatihave3 Whatihave3 = new whatihave3 ();

Public void test () {

WhatiUse.op (Adapt);

// approach 2:

Whatiuse2.op (will);

// approach 3:

WhatiUse.op (whatihave2);

// approach 4:

WhatiUse.op (whereihave3.whatiwant ());

}

Public static void main (string args []) {

JUnit.textui.testrunner.run (AdapterVariations.class);

}

} ///: ~

I want to take the liberty to borrow the term "proxy", because in "design mode", they insist that one agent (Proxy) must have the same interface as the object it agents. However, if the two words are used together, "Proxy Adapter" seems to be more reasonable.

Bridge (Bridge)

In the process of studying Bridge mode, I found that it is almost the worst mode of GOF description. I started this conclusion that when I read the "Design Patterns Explained" of Alan Shalloway's "Design Patterns Explained". At a meeting, I discussed this issue with two participants, which wrote an article about design patterns and did a speech on design patterns (including Bridge mode). These two discussions make me a completely different view of the structure of Bridge mode. With these doubts, I re-dried the work of GOF and realized that the two participants' views did not match the GOF. I have also found that the description of Bridge mode is really bad, and the picture of the Bridge mode universal structure is not used, and it is to describe the picture of the BRIDGE mode actual example. As long as you look at the picture, I understand how Bridge mode is. When you review Bridge mode, you will find that an important role is that you can often act as a set of intended structures for writing code. We may choose to use different objects when compile or runtime based on a particular situation, and the purpose of Bridge mode is to structure your code, so that you can easily add new types of front-end (Front-End) objects. These front-end objects are implemented by calling the functionality of the new type of backend (Back-end) objects). For this, modifications can be made without interfering with each other, and the front-end and backend (back-end) can be modified. The front-end-end can have a completely different interface, and is usually the same. Their common is to implement certain functions of itself by using any number of back-end objects. The interface of the backend object is usually different. The only thing between the backend objects must be the same thing is that they have to achieve some similar features - for example, a set of graphics libraries implemented by different ways, or a series of different data storage solutions. The Bridge mode is actually a tool for organizational code, which allows you to add any number of new front-end services, and these front-end services can be implemented by delegate these operations to any number of backend objects. By using Bridge mode, you can avoid the number of explosions of the number of classes brought by (class). But don't forget that a series of changes processed by the Bridge mode usually occur at the coding phase: When you need to process more and more options (options), Bridge mode allows you to keep your code. . The purpose of this example is to illustrate the structure of the Bridge mode (it implements the above picture):

//: Bridge: Bridgestructure.java

// a Demonstration of the structure and operation

// of the bridge pattern.

Package bridge;

Import junit.framework. *;

Class abstraction {

PRIVATE IMPLEMENTATION IMPLEMENTATION;

Public Abstract (Implementation IMP) {

IMPLEMENTATION = IMP;

}

// Abstract Used by the various front-end

// Objects in Order TO IMPLEMENT THEIR

// DiffERENT INTERFCES.

Public void service1 () {

// Implement this feature use some

// Combination of back-End Implementation: Implementation.facility1 ();

Implementation.facility2 ();

}

Public void service2 () {

// Implement this feature use some osther

// Combination Of Back-End Implementation:

Implementation.facility2 ();

Implementation.facility3 ();

}

Public void service3 () {

// Implement this feature use some osther

// Combination Of Back-End Implementation:

Implementation.facility11 ();

Implementation.facility2 ();

Implementation.facility4 ();

}

// for use by Subclasses:

protected importation getimplementation () {

Return IMplement;

}

}

Class clientService1 extends abstraction {

Public ClientService11 (Implementation IMP) {Super (IMP);

Public void servicea () {

Service1 ();

Service2 ();

}

Public void serviceb () {

Service3 ();

}

}

Class clientService2 extends abstraction {

Public clientService2 (Implementation IMP) {Super (IMP);

Public void servicec () {

Service2 ();

Service3 ();

}

Public void serviced () {

Service1 ();

Service3 ();

}

Public void servicee () {

GetImplementation (). facility3 ();

}

}

Interface implementation {

// the commit importation provided by the common

// back-end objects, Each in Their Own Way.

Void facility1 ();

Void facility2 ();

Void facility3 ();

Void facility4 ();

}

Class Library1 {

Public void method1 () {

System.out.println ("Library1.Method1 ()");

}

Public void method2 () {

System.out.println ("Library1.Method2 ()");

}

}

Class Library2 {

Public void Operation1 () {

System.out.println ("Library2.Operation1 ()");

}

Public void Operation2 () {

System.out.println ("Library2.Operation2 ()");

}

Public void Operation3 () {

System.out.println ("Library2.Operation3 ()");

}

}

Class Implementation1 Implements Implementation {// Each Facility Delegates to a Different Library

// in Order to Fulfill The Obligations.

PRIVATE LIBRARY1 DELEGATE = New Library1 ();

Public void facility11 () {

System.out.println ("Implementation1.Facility1");

Delegate.Method1 ();

}

Public void facility2 () {

System.out.println ("Implementation1.Facility2);

Delegate.Method2 ();

}

Public void facility3 () {

System.out.println ("Implementation1.Facility3);

Delegate.Method2 ();

Delegate.Method1 ();

}

Public void facility4 () {

System.out.println ("Implementation1.Facility4);

Delegate.Method1 ();

}

}

Class Implementation2 Implements IMPLEMENTATION {

PRIVATE LIBRARY2 DELEGATE = New library2 ();

Public void facility11 () {

System.out.println ("Implementation2.facility1");

Delegate.Operation1 ();

}

Public void facility2 () {

System.out.println ("Implementation2.facility2");

Delegate.Operation2 ();

}

Public void facility3 () {

System.out.println ("Implementation2.Facility3);

Delegate.Operation3 ();

}

Public void facility4 () {

System.out.println ("Implementation2.Facility4);

Delegate.Operation1 ();

}

}

Public Class BridgeStructure Extends Testcase {

Public void test1 () {

// here, The Implementation is determined by

// The Client At Creation Time:

ClientService1 CS1 =

New clientService1 (New Implementation1 ());

CS1.Servicea ();

Cs1.serviceb ();

}

Public void test2 () {

ClientService1 CS1 =

New clientService1 (New Implementation2 ());

CS1.Servicea ();

Cs1.serviceb ();

}

Public void test3 () {

ClientService2 CS2 =

New clientService2 (New Implementation1 ());

CS2.Servicec ();

Cs2.serviced ();

Cs2.servicee ();

}

Public void test4 () {ClientService2 CS2 =

New clientService2 (New Implementation2 ());

CS2.Servicec ();

Cs2.serviced ();

Cs2.servicee ();

}

Public static void main (String [] args) {

Junit.textui.teStrunner.Run (BridgeStructure.class);

}

} ///: ~

Front-End Base Class provides the operation required to implement Front-End Derived Class as required by the back-end base class (declared) by the back-end base class. Thus Since, any backend derived class can be used to implement the operations required for the front end class. Note that Bridge is completed step-by-step, each step is provided with an abstraction layer. In the above example, Implementation is defined as an interface to emphasize that all actual functions are implemented through the backend class, while the backend base class does not participate in the implementation. The backend derived class implements the operation defined by the base class by entrusting (this example, Library1 and library2), which usually have a completely different interface, but it provides almost the same Function (this is also an important purpose of Bridge mode). One words, each backend class can be seen as a library (ADAPTER), these libraries or tools implement expected features in different ways.

Exercise

1. Modify BridgeStructure.java, write a FACTROY to (Dynamic) to select (reserve object). 2. Modify BridgeStructure.java, use the delegation, not inherited for the front-end object. What are the benefits and disadvantages of this? 3. Write an example of a Bridge mode, requiring an associated array (Associative Array). Elements are to be removed by a key object (Key Object). In the constructor, a series of "key-value" is initialized in the constructor. When reading data, use the array as a backend as a backend, but when writing Key-Value Pair, the backend implementation is to switch to map.4. With Bridge mode, combine with java.util.collections written a stack (Stack) and queue (Queue). After writing, write a double-ended queue. Add a LinkedList as a backend implementation. This will write steps, you will understand how to use Bridge mode to add front and back-end objects to your code with the smallest case. 5. Write a class with Bridge mode to connect to the Bookkeeping program (along with their interfaces and data formats) and different banks (these banks offer different types of services and interfaces).

table of Contents

转载请注明原文地址:https://www.9cbs.com/read-126820.html

New Post(0)