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.