Use the code to briefly describe the design patterns of Singleton, abstract factory, bridge, and composite.

Source: Internet
Author: User

Use the code to briefly describe the design patterns of Singleton, abstract factory, bridge, and composite.
Singleton:

Public Class

{

Private Static a instance;

Private Static ()

{

Instance = ......

}

Public static a instance ()

{

Return instance;

}

}

By calling Class A. instance (), you can obtain the shared instance at the unique application level of the class, that is, Singleton.

Abstract Factory

Public Class B

{

}

Public Class C

{

}

Public class B1: B

{

}

..........

Public abstract class factory

{

Public abstract B create ();

Public abstract C create (string name );

}

Public class subfactory1: Factory

{

Public B create ()

{

Return new B1;

}

Public C create ()

{

Return new C1;

}

}

Public class subfactory2: Factory

{

Public B create ()

{

Return new B2;

}

Public C create ()

{

Return new C2;

}

}

You can customize production methods and return product specifications through abstract classes. The inherited child classes are used to produce products that meet the factory characteristics of this child class. However, the products must meet the specifications customized by the abstract class factory.

Composite

Public Class

{

Public void play ()

{

.....

}

}

Public Class B

{

Public void sleep ()

{

.....

}

}

 

Public Class C

{

Private a a1 = NULL;

Private B b1 = NULL;

Public C ()

{

A1 = new ();

B1 = new B ();

}

Public void play ()

{

A1. play ();

}

Public void sleep ()

{

B1. sleep ();

}

 

}

In fact, C encapsulates the methods provided by Class A and Class B, that is, it integrates the functions of both. The final interface is usually composed of components.

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.