Design Pattern-Structure Pattern

Source: Internet
Author: User

I. Summary of the structural mode:

 

Ii. Detailed analysis:

  1. (Adapter) Adapter mode Diagram

 

 

Adapter adapter mode: creates an interface to use a class of existing incompatible interfaces. The main application is to add new functions to system maintenance.

Usage process: provides an access interface for the object to be used, so that the target object functions are integrated into the system.

Life example: a typical example is a laptop power supply.

2. Proxy Mode

 

Proxy proxy mode: the proxy object is used to control the reference to the original object, and its interface cannot be changed.

3. decorator decorative pattern Diagram

 

 

 

Public ClassTest

{

Public Static VoidMain (string [] ARGs)

{

Errorlog =NewErrorlog ();

Decoratelog errorlogabve =NewDecoratelog (errorlog );

System.Out. Println ("errorlogabove :");

Errorlogabove. printmessage ();

 

Dubuglog =NewDubuglog ();

Decoratelog dubuglogabve =NewDecoratelog (errorlogabove, dubuglog );

System.Out. Println ("dubuglogabove :");

Dubuglogabove. printmessage ();

 

Inforlog =NewInforlog ();

Decoratelog inforlogabve =NewDecoratelog (dubuglogabove, inforlog );

System.Out. Println ("inforlogabove :");

Inforlogabve. printmessage ();

}

}

 

Abstract ClassLog

{

Public Abstract VoidPrintmessage ();

}

 

ClassDecoratelogExtendsLog

{

 

Log log1;

Log log2;

 

PublicDecoratelog ()

{

}

 

PublicDecoratelog (log log1)

{

This. Log1 = log1;

}

 

PublicDecoratelog (log log1, log log2)

{

This. Log1 = log1;

This. Log2 = log2;

}

 

Public VoidPrintmessage ()

{

If(Null! = Log1)

Log1.printmessage ();

If(Null! = Log2)

Log2.printmessage ();

}

}

 

ClassErrorlogExtendsLog

{

Public VoidPrintmessage ()

{

System.Out. Println ("error information ");

}

}

 

ClassDubuglogExtendsLog

{

Public VoidPrintmessage ()

{

System.Out. Println ("dubuglog information ");

}

}

 

ClassInforlogExtendsLog

{

Public VoidPrintmessage ()

{

System.Out. Println ("inforlog information ");

}

}

Decorator decoration mode: dynamically add some new operations and functions to the target object.

4. Appearance Pattern

 

Facade appearance mode: shields subsystem components from the system to reduce the coupling between the client and the system.

Life example: send a text message to check the phone bill. The applications in the project are databases. (The implementation of the datautil class is facade)

5. Bridge Mode Diagram

 

Bridge Mode: separates the abstraction and implementation of the system, and facilitates the implementation of layered architecture. Reduce the code modifications caused by changes.

PackageCom. Test. bridge;

 

Public ClassTest

{

Public Static VoidMain (string [] ARGs)

{

Abstractroad road1 =NewSpeedway ();

Road1.car =NewCar ();

Road1.run ();

}

}

 

Abstract ClassAbstractcar

{

Public Abstract VoidRun ();

}

 

ClassCarExtendsAbstractcar

{

Public VoidRun ()

{

 

System.Out. Println ("car in ");

}

}

 

ClassBusExtendsAbstractcar

{

Public VoidRun ()

{

 

System.Out. Println ("Bus in ");

}

}

 

Abstract ClassAbstractroad

{

Abstractcar car;

 

Public Abstract VoidRun ();

}

 

ClassSpeedwayExtendsAbstractroad

{

Public VoidRun ()

{

 

Car. Run ();

System.Out. Println ("driving on highway ");

}

}

 

ClassStreetExtendsAbstractroad

{

Public VoidRun ()

{

 

Car. Run ();

System.Out. Println ("driving on a street in a city ");

}

}

Living instance: device switch

6. Combined Mode Diagram

 

Composite combination mode: ensures consistency of object usage.

In terms of structure and decorate, the two modes are usually implemented together.

PackageCom. Test. composite;

 

ImportSun. Reflect. generics. reflectiveobjects. notimplementedexception;

ImportJava. util .*;

 

Public ClassTest

{

Public Static VoidMain (string [] ARGs)

{

// Construct the root node

Composite rootcomponent =NewComposite ("root ");

 

// Add two leaves, that is, child parts

Leaf leaf1 =NewLeaf ("leaf1 ");

Leaf leaf2 =NewLeaf ("leaf2 ");

 

Rootcomponent. Add (leaf1 );

Rootcomponent. Add (leaf2 );

 

// Traverse Components

Rootcomponent. eachchild ();

}

}

 

Abstract ClassComponent

{

String name;

 

PublicString getname ()

{

ReturnName;

}

 

Public VoidSetname (string name)

{

This. Name = Name;

}

 

// Add part

Public Abstract VoidAdd (Component component );

 

// Delete part

Public Abstract VoidRemove (Component component );

 

// Traverse all child parts

Public Abstract VoidEachchild ();

}

 

ClassLeafExtendsComponent

{

PublicLeaf ()

{

}

 

PublicLeaf (string name)

{

This. Name = Name;

}

 

// Leaf nodes cannot be added.

Public VoidAdd (Component component)

{

Throw NewNotimplementedexception ();

}

 

// The leaf node cannot be deleted because it cannot be added.

Public VoidRemove (Component component)

{

Throw NewNotimplementedexception ();

}

 

// The execution result is displayed because no child node exists on the leaf node.

Public VoidEachchild ()

{

String STR = string.Format(Name + "executed ..");

System.Out. Println (STR );

}

}

 

ClassCompositeExtendsComponent

{

PublicComposite ()

{

}

 

PublicComposite (string name)

{

This. Name = Name;

}

 

// The component used to save the combination

List <component> mylist =NewArraylist <component> ();

 

// Add a node and add a part

Public VoidAdd (Component component)

{

Mylist. Add (component );

}

 

// Delete a node to delete a part

Public VoidRemove (Component component)

{

Mylist. Remove (component );

}

 

// Traverse subnodes

Public VoidEachchild ()

{

String STR = string.Format(Name + "executed ..");

System.Out. Println (STR );

For(Component C: mylist)

{

C. eachchild ();

}

 

}

}

 

 

7. Metadata Mode

 

Share mode: Use the sharing technology to effectively support a large number of fine-grained objects

 

 

Design Pattern-Structure Pattern

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.