1.3 Abstract Factory Mode

Source: Internet
Author: User
1.3 Abstract Factory Mode
Abstract Factory mode provides an interface to the client, so that the client creates product objects in multiple product families without specifying the specific product type. This is the intention of the abstract factory model.
Each mode is a solution to certain problems. Abstract Factory models face the problem of multi-product hierarchical system design.
Before learning the specific example of an abstract factory, we should understand two important concepts: Product Family and product level.
Product Family: it refers to a family of products with different product levels and related functions. Such as amd cpu and ADM chip motherboard, constitute a family. Intel's CPU and Intel chip motherboard form another family. Both families come from two product levels: CPU and motherboard. A hierarchical structure consists of products with the same structure, as shown below:
Understanding the structure of this product is the key to understanding the abstract factory model, so I will spend no time to draw this diagram. If you cannot understand the meaning of this image, you cannot distinguish between the factory method mode and the abstract factory mode.
It can be seen that each factory in the abstract factory model creates a family of products, not one or one group. Groups can be combined at will! In fact, the two are just a little different. Now, you probably have understood the meaning of the abstract factory model. If you look at the example, the truth will be revealed to the rest of the world! 1.3.1 Implementation of abstract factory model in farm 1.3.1.1 backgroundSmart farmers always make their estate more and more valuable, and "Farm" continues to expand production after a simple factory model and factory model. Today, we are facing new developments. An important task is to introduce Plastic Greenhouses to plant tropical and subtropical fruits and vegetables in greenhouses, to meet market demands and gain greater benefits. 1.3.1.2 Product role DiagramAfter analysis, the product role is analyzed to obtain
1.3.1.3 System DesignAfter analysis, the so-called gardeners are actually factory roles, while vegetables and fruits are product roles. The abstract factory model is used in the farm. The system design is as follows:
1.3.1.4 implementation source code1.3.1.4.1 Abstract Factory: gardener. javapackage com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: gardener. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 22:55:23
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: Abstract Factory role: factory Interface
*/
Public interface gardener {public fruit createfruit (string name); Public veggie createveggie (string name );
} 1.3.1.4.2 abstract Fruit Product: fruit. javapackage com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: fruit. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 22:54:15
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: Abstract Product role: fruit Interface
*/
Public interface fruit {
} 1.3.1.4.3 abstract Vegetable Product: Veggie. Java
Package com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: Veggie. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 22:56:22
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: Abstract Product role: Vegetable Interface
*/
Public interface veggie {
} 1.3.1.4.4 tropical fruit: tropicalfruit. Java
Package com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: tropicalfruit. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 22:57:08
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: specific product role: tropical fruit
*/
Public class tropicalfruit implements fruit {
Private string name; Public tropicalfruit (string name ){
System. Out. println ("tropical fruit-" + name );
} Public String getname (){
Return name;
} Public void setname (string name ){
This. Name = Name;
} 1.3.1.4.5 tropical vegetables: tropicalveggie. Java
Package com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: tropicalveggie. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 22:58:03
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: specific product role: Tropical vegetables
*/
Public class tropicalveggie implements veggie {
Private string name; Public tropicalveggie (string name ){
System. Out. println ("tropical fruit-" + name );
} Public String getname (){
Return name;
} Public void setname (string name ){
This. Name = Name;
} 1.3.1.4.6 subtropical fruit: northernfruit. Java
Package com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: northernfruit. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 22:58:55
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: specific product roles: subtropical Fruits
*/
Public class northernfruit implements fruit {
Private string name; Public northernfruit (string name ){
System. Out. println ("the sub-region factory created for you: Sub-Region fruit-" + name );
} Public String getname (){
Return name;
} Public void setname (string name ){
This. Name = Name;
} 1.3.1.4.7 subtropical vegetables: northernveggie. Java
Package com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: northernveggie. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 22:59:36
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: specific product role: subtropical vegetables
*/
Public class northernveggie implements veggie {
Private string name; Public northernveggie (string name ){
System. Out. println ("created by the subtropical factory for you: subtropical vegetables-" + name );
} Public String getname (){
Return name;
} Public void setname (string name ){
This. Name = Name;
} 1.3.1.4.8 tropical plant: tropicalgardener. Java
/**
* Created by intellij idea.
* Filename: tropicalgardener. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 23:01:49
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: specific factory role: Tropical Factory
*/
Public class tropicalgardener implements gardener {
Public fruit createfruit (string name ){
Return new tropicalfruit (name );
} Public veggie createveggie (string name ){
Return new tropicalveggie (name );
}
} 1.3.1.4.9 subtropical plant: northerngardener. Java
Package com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: northerngardener. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 23:00:31
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: specific factory role: subtropical Factory
*/
Public class northerngardener implements gardener {
Public fruit createfruit (string name ){
Return new northernfruit (name );
} Public veggie createveggie (string name ){
Return new northernveggie (name );
}
} 1.3.1.4.10 test class (client): testapp. Java
Package com. lavasoft. patterns. abstractfactory. ybms ;/**
* Created by intellij idea.
* Filename: testapp. Java
* User: lavasoft
* Date: 2006-12-5
* Time: 23:03:22
* Reading Notes in Java and patterns (by Dr. Min Hong)
* Factory model -- Abstract Factory model -- General Model (farm Application)
* Readme: Test class (client)
*/
Public class testapp {
Private void test (){
Veggie TV, NV;
Fruit TF, NF;
Tropicalgardener Tg = new tropicalgardener ();
Northerngardener ng = new northerngardener (); TV = Tg. createveggie ("tropical leaf ");
NV = Ng. createveggie ("Northeast beet ");
TF = Tg. createfruit ("Hainan coconut ");
NF = Ng. createfruit ("Sydney ");
}
Public static void main (string ARGs []) {
Testapp test = new testapp ();
Test. Test ();
}
} 1.3.1.4.11 test run result
The tropical factory has created: tropical fruit-tropical leaf
The subtropical factory has created: subtropical vegetables-Northeast sugar beet
The tropical factory has created: tropical fruit-Hainan coconut
The subtropical factory has created: subtropical fruit-Sydney process finished with exit code 0
After reading the design drawing and source code, the principle is very clear. This mode is very flexible and the monkey gave it to you to see how you can play it! Hahahaha .... 1.3.1.5 nvwa makes things
The story of nvwa's rope creation is suitable for example here. nvwa's rope is divided by yin and yang, while products are divided by people and animals. The abstract factory model is used in the simulation system design of female's creation of everything. The system design diagram is as follows:
After reading this design drawing, we can refer to the farm example to describe how to implement it easily.

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.