Step by step -- Abstract Factory Model

Source: Internet
Author: User

In my opinion, the design patterns are gradually evolved through a large amount of practical experience and summaries from the predecessors. Including the classic GOF23 mode. Most of us are learning models from now on. Once learned, it may not be actually used in projects. Of course, this depends on the complexity of the business and whether a certain mode is required to solve this problem. It's not a pattern for the sake of the pattern. It takes a long time, and the pattern will be a little forgotten.

I want to take some notes for myself to give myself more design inspiration. Sometimes you may not be able to get started with projects, but when you look at some open source code and find that they are using a certain pattern, you will feel that they have a special flavor.

Subject:Abstract Factory Model

Intent: provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

Looking at the above sentence is really confusing. Next we will explain it step by step through an example.

Everyone knows that to assemble a Computer Host, You Need To: CPU, hard disk, motherboard, optical drive, power supply, and chassis. As for whether the host we finally assembled is high-configuration or low-configuration, you don't need to worry about it at this time. The Code is as follows:

Of course, they are all abstract products at this time, because I don't know how big the hard disk is and what brand it is. CPU is AMD, Inter, and so on. The Code is as follows:

Abstract class ComputerFactory
{
Public abstract CPU CreateCPU (); // CPU public abstract HardDisk CreateHardDisk (); // hard disk public abstract MotherBoard CreateMotherBoard (); // main board public abstract CDROM CreateCDROM (); // optical drive public abstract Power CreatePower (); // Power supply public abstract MachineBox CreateMachineBox (); // Chassis

}

Abstract class CPU
{
//
}

Abstract class HardDisk
{
//
}

Abstract class MotherBoard
{
//
}

Abstract class CDROM
{
//
}

Abstract class Power
{
//
}

Abstract class MachineBox
{
//

}

Next we need to combine the above parts. Become a working host

Class WorkMachine

{
Private ComputerFactory computer = null;
Public WorkMachine (ComputerFactory computer)
{
This. computer = computer;
}

// Install the CPU, hard disk, motherboard, optical drive, power supply, chassis, and Fan
// At this time, these classes will interact. If the CPU is to be installed on the motherboard. The optical drive should be fixed on the chassis and so on.
Public void BuildProduct ()
{
CPU cpu = computer. CreateCPU ();
MotherBoard board = computer. CreateMotherBoard ();
// Board. Opreator (cpu );
// And so on.
}
}

Have you found that everything in the WorkMachine class is abstracted and does not depend on a specific class.

Now we have completed most of the work.

Next, let's take a look at what kind of host we need to configure.

If you want a high-configuration host, you only need to implement the following abstract classes:

// High-configuration host Factory

Class HeightComputerFactory: ComputerFactory
{
Public override CDROM CreateCDROM ()
{
Return new DVD_9ROM ();
}
Public override CPU CreateCDROM ()
{
Return new InterCore4 ();
}
Public override HardDisk CreateCDROM ()
{
Return new HeightHardDisk ();
}
Public override MotherBoard CreateCDROM ()
{
Return new HeightMotherBoard ();
}
Public override Power CreateCDROM ()
{
Return new HeightPower ();
}
Public override MachineBox CreateCDROM ()
{
Return new HeightMachineBox ();
}
}

Class InterCore4: CPU // a quad-core CPU
{
//
}

Class HeightHardDisk: HardDisk
{
//
}

Class HeightMotherBoard: MotherBoard
{
//
}

Class DVD_9ROM: CDROM // dual Recorder
{
//
}

Class HeightPower: Power // high-performance Power Supply
{
//
}

Class HeightMachineBox: MachineBox
{
//
}

Finally, we call the code on the client as follows: class APP

{
Public static void Main ()
{
WorkMachine worn = new WorkMachine (new HeightComputerFactory (); // if you think there is a dependency here, you can use the configuration file to complete it. I don't think it is necessary to complete it.
Worn. BuildProduct ();
}
}

If I want to configure a learning host, we only need to implement these abstract classes in the same way as above, but I have not changed the WorkMachine class.

 

Finally, let's look back at its intention: to provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

The series mentioned above are: ComputerFactory class. At the same time, all the methods in it are also abstract classes with no specific implementation.

Of course, the premise of Abstract Public factory is that when the objects in ComputerFactory are stable, for example, I add an abstract class to the ComputerFactory class, then it will not be used.

 

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.