Abstract Factory mode, abstract factory Mode

Source: Internet
Author: User

Abstract Factory mode (conversion): Abstract Factory Mode
This blog is well written and has learned a lot. This article is reposted from http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html

A common example in our daily life is assembling a computer. When assembling a computer, we usually need to select a series of accessories, such as CPU, hard disk, memory, motherboard, power supply, and chassis. For the sake of simplicity, we only need to consider the choice of CPU and motherboard.

In fact, when selecting a CPU, there are a series of problems, such as the brand, model, number of pins, and clock speed. Only by determining these problems can we determine the specific CPU.

Similarly, when selecting a motherboard, there are also a series of problems, such as brand, chipset, integrated chip, bus frequency, and so on. Only these problems are identified can the specific motherboard be determined.

Choosing a different CPU and motherboard is the requirement that each customer puts forward to the installation company when assembling a computer, that is, the installation solution developed by each of us.

Before final determining the installation scheme, we also need to consider the compatibility between various accessories as a whole. For example, CPU and motherboard cannot be assembled if Intel CPU and AMD motherboard are used. Because the number of Intel CPU pins is not compatible with the CPU plug-in provided by the AMD motherboard, that is to say, if Intel's CPU is used, it cannot be inserted into the AMD motherboard, so the installation solution is holistic, the selected accessories are associated.

For the installation engineer, he only knows how to assemble a computer and the relevant accessories, but the customer has to say what kind of accessories to use. That is to say, the installation engineer is only responsible for assembly, and the customer is responsible for selecting the specific accessories required for assembly. Therefore, when the installation engineers assemble computers for different customers, they only need to obtain the corresponding accessories according to the customer's installation scheme and then assemble the computers.

Simple factory solution

Considering the customer's functions, You need to select the CPU and motherboard you need, and then tell the installation engineer their choice, and then wait for the installation engineer to assemble the computer.

For installation engineers, they only know the interfaces of CPU and motherboard, but do not know the specific implementation. Obviously, they can use the simple factory mode or factory method mode. For simplicity, a simple factory is used here. The customer informs the installation engineer of his or her choice, and then the installation engineer obtains the corresponding instance object through the corresponding factory. 1 public interface Cpu {2 public void calculate (); 3}

1 public class IntelCpu implements Cpu {2/** 3 * Number of CPU pins 4 */5 private int pins = 0; 6 public IntelCpu (int pins) {7 this. pins = pins; 8} 9 @ Override10 public void calculate () {11 // TODO Auto-generated method stub12 System. out. println ("Intel CPU pins:" + pins); 13} 14 15}
1 public class AmdCpu implements Cpu {2/** 3 * Number of CPU pins 4 */5 private int pins = 0; 6 public AmdCpu (int pins) {7 this. pins = pins; 8} 9 @ Override10 public void calculate () {11 // TODO Auto-generated method stub12 System. out. println ("amd cpu pins:" + pins); 13} 14}

Motherboard interface and implementation

1 public interface Mainboard {2     public void installCPU();3 }
1 public class IntelMainboard implements Mainboard {2/** 3 * number of holes in the CPU slot 4 */5 private int cpuHoles = 0; 6/** 7 * constructor, 8 * @ param cpuHoles 9 */10 public IntelMainboard (int cpuHoles) {11 this. cpuHoles = cpuHoles; 12} 13 @ Override14 public void installCPU () {15 // TODO Auto-generated method stub16 System. out. println ("the number of CPU slot holes on the Intel motherboard is:" + cpuHoles); 17} 18 19}
1 public class AmdMainboard implements Mainboard {2/** 3 * number of holes in the CPU slot 4 */5 private int cpuHoles = 0; 6/** 7 * constructor, 8 * @ param cpuHoles 9 */10 public AmdMainboard (int cpuHoles) {11 this. cpuHoles = cpuHoles; 12} 13 @ Override14 public void installCPU () {15 // TODO Auto-generated method stub16 System. out. println ("AMD motherboard CPU slot holes:" + cpuHoles); 17} 18}

CPU and motherboard Factory

 1 public class CpuFactory { 2     public static Cpu createCpu(int type){ 3         Cpu cpu = null; 4         if(type == 1){ 5             cpu = new IntelCpu(755); 6         }else if(type == 2){ 7             cpu = new AmdCpu(938); 8         } 9         return cpu;10     }11 }
 1 public class MainboardFactory { 2     public static Mainboard createMainboard(int type){ 3         Mainboard mainboard = null; 4         if(type == 1){ 5             mainboard = new IntelMainboard(755); 6         }else if(type == 2){ 7             mainboard = new AmdMainboard(938); 8         } 9         return mainboard;10     }11 }

The running results of installation engineers and customers are as follows:

1 public class ComputerEngineer {2/** 3 * defines the CPU required for the Assembly Machine 4 */5 private Cpu cpu = null; 6/** 7 * defines the motherboard required for assembly 8 */9 private Mainboard mainboard = null; 10 public void makeComputer (int cpuType, int mainboard) {11/** 12 * basic steps for machine assembly 13 */14 // 1: first, prepare the required accessories 15 prepareHardwares (cpuType, mainboard); 16 // 2: assemble the machine 17 // 3: test the machine 18 // 4: deliver the customer 19} 20 private void prepareHardwares (int cpuType, int mainboard) {21 // here to be accurate The specific implementation of the standby CPU and the motherboard is as simple as an example. Here we only prepare the two 22 //. However, the installation engineer does not know how to create them. What should I do? 23 24 // directly find the corresponding factory to get 25 this. cpu = CpuFactory. createCpu (cpuType); 26 this. mainboard = MainboardFactory. createMainboard (mainboard); 27 28 // test whether the accessories are easy to use 29 this. cpu. calculate (); 30 this. mainboard. installCPU (); 31} 32}
1 public class Client {2     public static void main(String[]args){3         ComputerEngineer cf = new ComputerEngineer();4         cf.makeComputer(1,1);5     }6 }

The running result is as follows:

The above implementation, although solved through a simple factory method: For installation engineers, only the CPU and motherboard interfaces are known, but not the specific implementation problems.But there is another problem that cannot be solved, that is, these CPU objects and the main board objects are actually related and need to match each other.In the above implementation, this association is not maintained, and the CPU and motherboard are chosen by the customer at will, which is problematic. For example, when the client calls makeComputer, the input parameter is (1, 2) and the running result is as follows:

Observe the above results to see the problem. The customer chose Intel with 755 CPU pins, AMD as the motherboard, and 938 as the CPU Jack on the motherboard, which cannot be assembled at all, this is because the relationship between accessories is not maintained. How can this problem be solved?

Introduce Abstract Factory Model

Each mode is a solution to certain problems. The biggest difference between the abstract factory mode and the factory method mode is that the factory method mode is for a product level structure, while the abstract factory mode needs to face multiple product level structures.

Before learning the specific example of an abstract factory, we should understand two important concepts: Product Family and product level.

A product family is a family of products with functions associated with different product levels. For example, AMD's motherboard, chipset, and CPU constitute a family, while Intel's motherboard, chipset, and CPU constitute a family. Both families come from three product levels: motherboard, chipset, and CPU. A hierarchical structure consists of products with the same structure, as shown below: 1 public interface AbstractFactory {2/** 3 * Create a CPU object 4 * @ return CPU object 5 */6 public Cpu createCpu (); 7/** 8 * Create a motherboard object 9 * @ return motherboard object 10 */11 public Mainboard createMainboard (); 12}

 1 public class IntelFactory implements AbstractFactory { 2  3     @Override 4     public Cpu createCpu() { 5         // TODO Auto-generated method stub 6         return new IntelCpu(755); 7     } 8  9     @Override10     public Mainboard createMainboard() {11         // TODO Auto-generated method stub12         return new IntelMainboard(755);13     }14 15 }
 1 public class AmdFactory implements AbstractFactory { 2  3     @Override 4     public Cpu createCpu() { 5         // TODO Auto-generated method stub 6         return new IntelCpu(938); 7     } 8  9     @Override10     public Mainboard createMainboard() {11         // TODO Auto-generated method stub12         return new IntelMainboard(938);13     }14 15 }

Compared with the preceding implementation, the installation engineer class has the following major changes: from the client side, the CPU and motherboard selection parameters are not input, but directly transmitted to the customer's selected product objects. In this way, the compatibility problem caused by selecting the CPU and motherboard separately is avoided. The customer needs to choose one set, which is a series.

1 public class ComputerEngineer {2/** 3 * defines the CPU required for the Assembly Machine 4 */5 private Cpu cpu = null; 6/** 7 * defines the motherboard required for assembly 8 */9 private Mainboard mainboard = null; 10 public void makeComputer (AbstractFactory af) {11/** 12 * basic steps for machine assembly 13 */14 // 1: first, prepare the required accessories 15 prepareHardwares (af); 16 // 2: assemble machine 17 // 3: Test Machine 18 // 4: deliver customer 19} 20 private void prepareHardwares (AbstractFactory af) {21 // here we are going to prepare the specific implementation of CPU and motherboard, for the sake of simplicity, only the two 22 // Yes, the installation engineer does not know how to create it. What should I do? 23 24 // directly find the corresponding factory to get 25 this. cpu = af. createCpu (); 26 this. mainboard = af. createMainboard (); 27 28 // test whether the accessory is easy to use 29 this. cpu. calculate (); 30 this. mainboard. installCPU (); 31} 32}

Client code:

1 public class Client {2 public static void main (String [] args) {3 // create an installation engineer object 4 ComputerEngineer cf = new ComputerEngineer (); 5 // The customer selects and creates the product object to be used. 6. Export actfactory af = new IntelFactory (); 7. Inform the installation engineer of the selected product and let the installation engineer assemble the computer 8 cf. makeComputer (af); 9} 10}

The abstract factory function is to create an interface for a series of related objects or mutually dependent objects. Be sure to note that the methods in this interface are not arbitrarily stacked, but a series of related or interdependent methods. For example, the motherboard and CPU used in the above example are used to assemble related objects of a computer. Different Installation Schemes represent a specific computer series.

 

 

 

 

 

 

 

 

 

 

 

Since a series of objects defined by the abstract factory are usually related or mutually dependent, these product objects constitute a product family, that is, the abstract factory defines a product family.

This brings great flexibility. When switching product families, you only need to provide different abstract factory implementations. That is to say, a product family is switched as a whole.

Under what circumstances should I use the abstract factory model?

  1. A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.

2. There are more than one product family in this system, and the system only consumes products of one of them.

3. products belonging to the same product family are used together. This constraint must be reflected in the system design.(For example, the Intel motherboard MUST use an Intel CPU or an Intel chipset)

4. The system provides a product library. All products use the same interface, so that the client does not rely on implementation.

Abstract The Origin of the factory Model

The origin or earliest application of the abstract factory model is used to create windows that belong to different operating systems. For example, the command buttons and Text boxes are both Windows. In the Windows environment of the UNIX operating system and Windows environment of the Windows operating system, the two builds have different local implementations, and their details are different.

In each operating system, there is a Windows build family. Here is the product family composed of buttons and Text. Each window component has its own hierarchical structure. An abstract role gives an abstract function description, and a specific sub-class gives specific implementations under different operating systems.

 

 

 

 

 

 

 

We can see that in the above product class diagram, there are two product level structures: the Button level structure and the Text level structure. There are two product families, namely the UNIX product family and the Windows product family. The UNIX product family consists of UNIX buttons and UNIX Text products, while the Windows product family consists of Windows buttons and Windows Text products.

 

 

 

 

 

 

 

 

 

The system needs to create product objects to meet the hierarchical structure of a project. Two specific engineering roles are available: UnixFactory and WindowsFactory. The UnixFactory object is used to create products in the Unix product family, while the WindowsFactory object is used to create products in the Windows product family. This is the application of the abstract factory model. Solutions for the abstract factory model are as follows:

 

 

 

 

 

 

 

 

 

Obviously, a system can only run in a Windows environment of a certain operating system, rather than running on different operating systems at the same time. Therefore, the system can only consume products belonging to the same product family.

In modern applications, the scope of use of the abstract factory model has been greatly expanded, and the system is no longer required to consume only one product family. Therefore, you can ignore the original intentions mentioned above.

Advantages of abstract factory Model
  • Separation interfaces and Implementations

The client uses an abstract factory to create the required objects, and the client does not know the specific implementation. The client is only for product-oriented interface programming. That is to say, the client is decoupled from the specific product implementation.

  • Easy to switch product families

Because a specific factory implementation represents a product family, for example, from the Intel series to the AMD series in the above example, you only need to switch the specific factory.

Disadvantages of abstract factory Model
  • It is not easy to expand new products

If you want to add a new product to the entire product family, you need to modify the abstract factory so that all factory implementation classes can be modified.

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.