The core factory category is no longer responsible for the creation of all products. Instead, it submits the specific creation work to sub-classes and becomes an abstract factory role. It is only responsible for providing interfaces that must be implemented by specific factory classes, but do not touch the details of which product class should be instantiated.
Example: Ask mm to go to McDonald's for a hamburger. Different mm has different tastes. It is annoying to remember every one. I usually use the factory method mode, take the mm to the waiter and say "I want a hamburger". What kind of hamburger should I ask the mm to tell the waiter directly.
1 Interface Sample {
2 Public Void Say ();
3 }
4
5 Class Samplea Implements Sample {
6 Public Void Say (){
7 System. Out. println ("samplea ");
8 }
9 }
10
11 Class Sampleb Implements Sample {
12 Public Void Say (){
13 System. Out. println ("sampleb ");
14 }
15 }
16 Abstract Class Factory {
17 Public Abstract Sample create ();
18 }
19
20 Class Factorya Extends Factory {
21 Public Sample create (){
22 Return New Samplea ();
23 }
24 }
25
26 Class Factoryb Extends Factory {
27 Public Sample create (){
28 Return New Sampleb ();
29 }
30 }
31
32 Public Class Test {
33 Public Static Void Main (string [] ARGs ){
34 Factory factorya = New Factorya ();
35 Sample test1 = factorya. Create ();
36 Test1.say ();
37 Factory factoryb = New Factoryb ();
38 Sample Test2 = factoryb. Create ();
39 Test2.say ();
40 }
41 }