The structural model of Java Classic 23 Design Patterns (I.) __java

Source: Internet
Author: User

The structure pattern includes 7 kinds: Adapter mode, bridge mode, combination mode, decoration mode, appearance mode, enjoy element mode, Agent mode.

This article mainly introduces adapter mode and bridging mode. First, adapter mode (Adapter)

Adapter mode is actually very simple, like a mobile phone charger, the mobile phone needs 5V, and the outlet is 220V. Therefore need the charger to swing on OK. Another example, one can speak Chinese and a can only speak English can not communicate, then the middle of a translation. All communication through translation, translation to speak English, you can complete a single communication. The example in Link 1 is very vivid. To sum up, there is a common interface (called target), and if all goes well, everyone is fine and does not need to be fit. If an interface is not a common standard interface, you need to reset the variables of a non-standard interface when you implement target, and then use the function provided by target to call the function of the object of the nonstandard interface in the function body, just like deceitful act. Here is a detailed description:

1, Target

Public interface Target {
void Adapteemethod ();
void Adaptermethod ();
}

2, Adaptee
public class Adaptee {
public void Adapteemethod () {
Syste*.out.p*intln ("Adaptee method!");
}
}

This is the class of non-standard interfaces that are described in the above article.

3, Adapter

Public clas* Adapter implement* Target {
Private Adap*ee adaptee;
Public Adapter (Adaptee adaptee) {
this.adapte* = Adaptee;
}
public void Adapteemethod () {
Adaptee.adapteemethod ();
}
public void Adaptermethod () {
*ystem.out.println ("Adapter method!");
}
You can see in the adapter, new a adaptee variable, and then call adaptee.*** in the function in the generic standard interface target.

Test code:

public class Test {
public static void Main (string[] args) {
Target target = new Adapter (new Adaptee ());
Target.adapteemethod ();
Target.adaptermethod ();
}
}

Applicability:

1. You want to make an already existing class, and its interface does not meet your needs.
2. You want to create a reusable class that can be used with other unrelated classes or unforeseeable classes (that is, the * interface
Classes that may not necessarily be compatible) work together.
3. (For object adapter only) you want to use some subclasses that already exist, but it's not possible to do it for each
Subclasses to match their interfaces. The object adapter can fit its parent class interface.
Bridging Mode (bridge)

Bridging mode is designed to separate the abstraction and implementation parts, and if a transaction has multiple dimensions, it needs to be decoupled using bridging mode. For example, there are two kinds of roads and highways. There are two kinds of cars and big trucks. Such 22 combinations have four kinds of situations, in order to describe clearly what vehicles need to design four classes on what road. If the bridging mode is simple, the essence of bridging mode is to maintain an object of another class in one of the classes. For example, when I define "road", I design a variable car (this is a base class whose subclasses are cars and trucks). By inheriting this imaginary class of "road", we get two concrete roads: general highways and highways. Inherit virtual class car, get two specific cars: cars and big trucks. So when I'm describing things, I can describe things clearly by car C = New highway, then by assigning it to cars and big trucks in C.

If it is more complicated, the above example has only two dimensions: Road and car, plus one dimension: Man. Of course people have men and women. So for the logic of the concept and reality consistent, you can define the virtual class of people, define a member variable car. When you define a virtual class car, you add a member variable way. This assumes that things have a, B, C, D ... Several dimensions, just add a member variable of virtual class B in the definition of a. Add a C's member variable when defining B, and so on, this is the bridging mode. Can see the link in the example is written in C + +, and so I have time to write in Java. Here are some examples of men and women wearing horse clips and trousers, typically two dimensions:

1.Abstraction abstract class people, maintaining a pointer to a Implementor type object, which refers to the back of the clothing virtual class

public abstract class Person {
private clothing clothing;
Pr*vate String type;
Public clothing getclothing () {
return clothing;
}
publi* void Setclothing () {
this.clothing = *lothingfactory.getclothing ();
}
public void SetType (String type) {
T*is.type = type;
}
Public String GetType () {
return this.ty*e;
}
public abstract void dress ();
}

2, Refinedabstraction: Expand the interface defined by abstraction.

Male human
public class Mans extends *erson {
Public Mans () {
SetType ("man");
}
public void dress () {
Clothing clothing = get*lothing ();
Clothing.persondresscloth (this);
}
}

Woman class:

public class Lady extends person {


Public Lady () {
settyp* ("Woman");
}

public void dress () {
Cloth*ng clothing = getclothing ();
C*othing.persondresscloth (this);
}
}

3, Implementor
Defines an interface for the implementation class that does not necessarily have to be exactly the same as the Ab*traction interface. In fact, the two interfaces can be completely different.
Generally speaking, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on these basic operations.

Public abstract class Clothing {
public abstract void Persondressc*oth (*erson person);
}

4, the specific concreteimplemento*

public class Jackt extends clothing {


public void Persondresscloth (person person) {
System.out.println (Person.gettype () + "Wear vest");
}
}
public class Trouser extends clothing {


public void Persondresscloth (person person) {
System.ou*.println (*erson.gettype () + "wear trousers");
}
}

Test code:

public class Test {public
    static void Main (string[] args) {The person man
        = The new Man ();
        Lady of the person = The New Lady ();
        Clothing jacket = new Jacket ();
        Clothing trouser = new trouser ();
        Jacket.persondresscloth (mans);
        Trouser.persondresscloth (mans);
        Jacket.persondresscloth (lady);
        Trouser.persondresscloth (Lady);
    }


Haha, then looked for the next, http://blog.csdn.net/jason0539/article/details/22568865 has the above people, road, car Examples of Java written out, you can refer to. But in the article, the user has the road, the road has the car construction thought is not very recommended, in addition is not using the interface feeling still a little disorderly, is not clear.

Another is that this person, car, road example in the construction is one-way, this is better, new out of a person, and then set what road. And the example of dress is a two-way coupling, in clothing need to pass people virtual class, fortunately, the coupling is virtual class. In fact, can also be seen as one-way coupling, in the pepple in fact defined the dress method, through the creation of the previous model to obtain the desired clothing type is OK.

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.