Structural Patterns Structural model
1. Adapter mode (Adaptor)
Purpose: Converts a class/interface to another form that the client expects. Adapters allow classes to work together, otherwise they will not work because of incompatible interfaces. By adding an interface to encapsulate an existing subclass, the client is programming to the interface, thus hiding the specific subclass.
Suppose we have a MediaPlayer interface and an entity class Audioplayerthat implements the MediaPlayer interface. By default,Audioplayer can play audio files in mp3 format.
We also have another interface Advancedmediaplayer and an entity class that implements the Advancedmediaplayer interface. This class can play files in VLC and MP4 format.
To have Audioplayer play audio files in other formats. Need an adapter, this adapter implements the Mediapayer interface, in this adapter class, with the Advancedmediaplayer interface, instantiate the implementation of its two classes,
PublicClass MediaadapterImplementsMediaPlayer {Advancedmediaplayer advancedmusicplayer;PublicMediaadapter (String audiotype) {if (Audiotype.equalsignorecase ("VLC"new Vlcplayer ();} else if (Audiotype.equalsignorecase ("mp4" new Mp4player () ; }} @Override public void Play (String audiotype, String fileName) {if (Audiotype.equalsignorecase ("VLC" else if (Audiotype.equalsignorecase ("mp4" ) {Advancedmusicplayer.playmp4 (FileName)}}}
It then provides support for playing other file formats in the entity classes that normally implement the MediaPlayer interface.
1PublicClass AudioplayerImplementsMediaPlayer {2Mediaadapter Mediaadapter;34@Override5PublicvoidPlay (String audiotype, String fileName) {67//Play built-in support for MP3 music files8if (Audiotype.equalsignorecase ("MP3")){9 System.out.println ("Playing mp3 file. Name: "+FileName);10}11//Mediaadapter provides support for playing other file formats12Elseif (Audiotype.equalsignorecase ("VLC") 13 | | audiotype.equalsignorecase ("mp4" 14 mediaadapter = new Mediaadapter (Audiotype); 15 Mediaadapter.play (Audiotype, fileName); 16 }17 Else{18 System.out.println ("Invalid media.") +19 audiotype + "format not supported" ); 20 }21 } 22}
Adorner mode (Decorator)
Decorator mode is a design pattern that is designed to address the following problems: Adding features of different sides to an object. In this design pattern, we construct subclasses for each attribute, which is added to the object through the delegation mechanism.
Adorner vs. inheritance
? Adorners compose features at run time, and inheritance composes features at compile time.
? Adorners consist of multiple collaborative objects; Inheritance produces an object of a definite type.
? Multiple decorations can be mixed and matched, and multiple inheritance is conceptually difficult.
Appearance mode (facade pattern)
Hides the complexity of the system and provides the client with an interface that the client can access the system. This type of design pattern is a structured pattern that adds an interface to an existing system to hide the complexity of the system.
Create an interface.
Shape.java
PublicInterfaceShape{void draw();}
Create an entity class that implements the interface.
Rectangle.java
public class rectangle implements shape { @Override public void Draw () { system.. Println}} /span>
Square.java
public class square< Span class= "PLN" > implements shape { @Override public void Draw () { system.. Println ( "Square::d Raw ()" }} /span>
Circle.java
public class Circle implements Shape { @Override Public void draw() { System. Out. println("Circle::d Raw ()"); }}< /c14>
Create a skin class.
Shapemaker.java
Public Class Shapemaker { Private ShapeCircle; Private ShapeRectangle; Private ShapeSquare; Public Shapemaker() {Circle= New Circle();Rectangle= New Rectangle();Square= New Square(); } Public void Drawcircle () { Circle. Draw} public void Drawrectangle () { Rectangle. Draw} public void Drawsquare () { Square. Draw}} /span>
Use this skin class to draw various types of shapes.
Facadepatterndemo.java
Public Class Facadepatterndemo { Public Static voidMain (string[] Args { shapemaker Shapemaker = new shapemaker (); Shapemaker. Shapemaker. Shapemaker.}}
/span>
Java design mode (i)