Creation Mode (II): abstractfactory (Abstract Factory)

Source: Internet
Author: User

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 }

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.