Java classic 23 design modes: creative mode (1)

Source: Internet
Author: User

Java classic 23 design modes: creative mode (1)

The design pattern is called the Internal Skill of the programmer. I have read a majority of it before, but I have summarized it myself. So I will summarize it here. It is worth mentioning that the design pattern is not specific to Java. Because Android is always used, Java is used as the carrier here. There are 23 classic design modes, which can be divided into three major types: Creation Mode (5), structure mode (7), and behavior mode (11 ), 5 + 7 + 11 = 23. all searches on the Internet are rejected. Here is only a personal record. This article records the Factory Method, Abstract Factory, and Singleton modes in the creation mode, and strives to be thorough.

I. Factory method

The factory method involves four concepts. For a general understanding, we first use an online example. Men take their women to KFC for dinner. Men first say they want a hamburger. Specifically, let MM say that they want a hamburger. I personally think this example is not very appropriate and may cause misunderstandings. The simplest example is that there are many factories, each of which can produce specific products. For a clear understanding, we will introduce it in conjunction with the Java design pattern (crazy Java Alliance edition.

1. Product is a Product and an interface.

Public interface Work { 

Void doWork ();
}

2. ConcreteProduct: specific product. That is, the preceding work interface is implemented, but different tasks are performed. The same thing is of course the concept of a class.

Public class StudentWork implements Work {
Public void doWork (){
System. out. println ("Student * homework! ");
}
}

Public class TeacherWork implements Work {
Public void doWork (){
System. out. println ("the instructor approves the assignment! ");
}
}

Two specific products are defined here. A student's job is homework, and a teacher's job is to approve homework.

3. Creator: the Creator. Corresponding to the above Product, Creator aims to get the Product and get the Product.

Public interface IWorkFactory {
Work getWork ();
}

4. ConcreteCreator: The specific creator. If the Creator obtains Work, all specific products meet the requirements. Each product is encapsulated in a factory.

Public class StudentWorkFactory implements IWorkFactory {


Public Work getWork (){
Return new StudentWork ();
}


}


Public class TeacherWorkFactory implements IWorkFactory {


Public Work getWork (){
Return new TeacherWork ();
}


}

The test code is as follows:

Public class Test {


Public static void main (String [] args ){
IWorkFactory studentWorkFactory = new StudentWorkFactory ();
StudentWorkFactory. getWork (). doWork ();

IWorkFactory teacherWorkFactory = new TeacherWorkFactory ();
TeacherWorkFactory. getWork (). doWork ();
}

}
Applicability:

1. When a class does not know the class of the object it must create. 


2. When a class wants its subclass to specify the object it creates.

3. If you want to delegate the responsibility of creating an object to one of multiple help classes and want to localize the information of which help subclass is the proxy.

Ii. Abstract Factory

With the above example, we can understand the abstract factory very well. For example, if you go to the store to buy things, the store is Factory. If you want to say something, the clerk will give you something.

In combination with the above example, define a WorkFactory to implement the IWorkFactory interface, pass in a parameter in the WorkFactory, determine whether it is a new StudentWork or TeacherWork Based on the parameter, and then return. This is the abstract factory. This is the simplest and straightforward understanding. For clarity, the following is a complex example.

1. AbstractProduct abstract Product

Public interface ICat {
Void eat ();
}


Public interface IDog {


Void eat ();
}

2. Specific Products of ConcreteProduct

Public class BlackCat implements ICat {
Public void eat (){
System. out. println ("The black cat is eating! ");
}
}


Public class WhiteCat implements ICat {


Public void eat (){
Sy * tem. out. prin * ln ("The w * ite cat is eating! *);
}
}

Public class BlackDog implements IDog {


Public void eat (){
System. out. println ("The black dog is eating ");
}


}


Public class WhiteDog implements IDog {


Public void eat (){
System. out. println ("The white dog is eat * ng! ");
}


}

3. AbstractFactory Abstract Factory and interface.

Public interface IAnimalFactory {


ICat createCat ();

IDog createDog ();
}

4,

ConcreteFactory: implements the above interface.

Public class BlackAnimalFactory implements IAnimalFactory {


Public ICat createCat (){
Return new BlackCat ();
}


Public IDog createDog (){
Return new BlackDog ();
}


}


Public class WhiteAnimalFactory imp * ements IAnimalFactory {


Public ICat createCat (){
Return new WhiteCat ();
}


Public IDog createDog (){
Return new WhiteDog ();
}


}

Test code:

Public static void main (String [] args ){
IAnimalFactory blackAnimalFactory = new BlackAnimalFactory ();
ICat blackCat = blackAnimalFactory. createCat ();
BlackCat. eat ();
IDog blackDog = blackAnimalFactory. createDog ();
BlackDog. eat ();

IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory ();
ICat whiteCat = whiteAnimalFactory. createCat ();
WhiteCat. eat ();
IDog whiteDog = whiteAnimalFactory. createDog ();
WhiteDog. eat ();
}

Note: we can see that this complex example uses interfaces to express the factory's creation of different products based on different needs. A simple example is to transmit parameters to determine different products.

Usage:

1. A system must be independent of the creation, combination, and representation of its products.
2. A system must be configured by one of multiple product generations.
3. When you want to emphasize the design of a series of related product objects for joint use *
4. When you provide a product library, you only want to display their interfaces instead of implementations.

Iii. Singleton Mode

This mode is widely used. For more information, see the blog post. I will not repeat it.

Key points:

1 is to use double locks for the sake of thread call security and synchronization locks.

2. In addition to this classic Singleton, there are other Singleton modes.


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.