现在的位置: 首页 > 综合 > 正文

Structural patterns_Java design patterns

2017年12月24日 ⁄ 综合 ⁄ 共 2655字 ⁄ 字号 评论关闭

Structural patterns prescribe the organization of classes and objects. These patterns are concerned with how classes inherit from each other or how they are composed from other classes.

Common structural patterns include Adapter, Proxy, and Decorator patterns. These patterns are similar in that they introduce a level of indirection between a client class and a class it wants to use. Their intents are different, however. Adapter uses indirection
to modify the interface of a class to make it easier for a client class to use it. Decorator uses indirection to add behavior to a class, without unduly affecting the client class. Proxy uses indirection to transparently provide a stand-in for another class.

The Adapter pattern

The Adapter pattern is typically used to allow the reuse of a class that is similar, but not the same, as the class the client class would like to see. Typically the original class is capable of supporting the behavior the client
class needs, but does not have the interface the client class expects, and it is not possible or practical to alter the original class. Perhaps the source code is not available, or it is used elsewhere and changing the interface is inappropriate.

Here is an example that wraps OldClass so a client class can call it using a method, NewMethod() defined in NewInterface:

  public class OldClassAdapter implements NewInterface {
    private OldClass ref;
    public OldClassAdapter(OldClass oc)
    {
      ref = oc;
    }
      public void NewMethod()
    {
       ref.OldMethod();
    }
  }

The Decorator and Proxy pattern

A Proxy is a direct stand-in for another class, and it typically has the same interface as that class because it implements a common interface or an abstract class. The client object is not aware that it is using a proxy. A Proxy
is used when access to the class the client would like to use must be mediated in a way that is apparent to the client -- because it requires restricted access or is a remote process, for example.

Decorator, like Proxy, is also a stand-in for another class, and it also has the same interface as that class, usually because it is a subclass. The intent is different, however. The purpose of the Decorator pattern is to extend the functionality of the original
class in a way that is transparent to the client class.

Examples of the Decorator pattern in the Java API are found in the classes for processing input and output streams.BufferedReader(), for example, makes reading text from a file convenient and efficient:

BufferedReader in = new BufferedReader(new FileReader("file.txt"));

The Composite pattern

The Composite pattern prescribes recursive composition for complex objects. The intent is to allow all component objects to be treated in a consistent manner. All objects, simple and complex, that participate in this pattern derive from a common abstract component
class that defines common behavior.

Forcing relationships into a part-whole hierarchy in this way minimizes the types of objects that our system (or client subsystem) needs to manage. A client of a paint program, for example, could ask a line to draw itself in the same way it would ask any other
object, including a composite object.

抱歉!评论已关闭.