Follow the example to learn the design pattern-abstract Factory

Source: Internet
Author: User

Abstract factory belongs to the creation design pattern

Design Intent: Provides an interface that can create a series of related or interdependent objects without specifying their specific classes.

Light look at the design intent some abstract, not good understanding, let us take a look at the example class diagram, combined with the class diagram we do a concrete explanation, I believe it will let everyone enlightened. We take the production of automobiles, for example, we produce two series of cars, cars, trucks, each series of cars have engines and tanks.

Iabstrcatfactory: An abstract factory interface that declares methods for creating abstract products.

Carfactory: Car factory implementation class.

Trunkfactory: Truck Factory implementation class.

Here we provide a factory implementation class for each series, remember that this is the core of the abstract factory, there are several series on the creation of a few factory implementation classes, if our series added a sports car, then we need to add a sports car factory implementation class.

Ivehicle: Automotive Product Interface, declares the method of producing automobiles.

Car: Car realization class.

Trunk: Truck implementation class.

Iengine: Engine product interface, declares the method of producing the engine.

Carengine: Car engine implementation class.

Trunkengine: Truck engine implementation class.

Itank: Fuel tank Product Interface, declare the method of producing tank.

Cartank: Car fuel tank products.

Trunktank: Truck fuel tank products.

Each product has an abstract interface, our creation is also for these abstract interfaces, in the client, our creation process is closed, you only to the interface created by WHO, but you do not know how it was created. Let's take a look at the client's code first.

Package Com.factory.abstrcatfactory;import Com.factory.abstrcatfactory.factory.carfactory;import Com.factory.abstrcatfactory.factory.iabstrcatfactory;import com.factory.abstrcatfactory.factory.TrunkFactory; Import Com.factory.abstrcatfactory.model.engine;import Com.factory.abstrcatfactory.model.tank;import Com.factory.abstrcatfactory.model.vehicle;import Com.factory.abstrcatfactory.product.iengine;import Com.factory.abstrcatfactory.product.itank;import Com.factory.abstrcatfactory.product.ivehicle;public class Client {/** Abstract Factory * There are a few abstract series of products there are several factories, each factory is responsible for the transcend product of this series * each system product has a number of sub-products, each sub-product has an interface class * @author Gaoxu * @param args */public static void Main (string[] args) {//Trolley abstract object creation iabstrcatfactory carfactory = new Carfactory ();//Trolley engine iengine Enginec = Carfactory.createengineobject (); Engine carengine= enginec.createengine ();//trolley fuel tank Itank TANKC = Carfactory.createtankobject (); Tank Cattank = Tankc.createtank (); Ivehicle Vehiclec = Carfactory.createvehicleobject ();//created car vehicle car = Vehiclec.createvehiclE (Carengine, Cattank);////////////////////////////////////////////////////////////the creation of a truck abstract object iabstrcatfactory Trunkfactory = new Trunkfactory ();//truck engine Iengine enginet = Trunkfactory.createengineobject (); Engine trunkengine = Enginet.createengine ();//truck fuel tank Itank Tankt = Trunkfactory.createtankobject (); Tank Trunktank = Tankt.createtank (); Ivehicle Vehiclet = Carfactory.createvehicleobject ();//created truck vehicle trunk = Vehiclet.createvehicle (Trunkengine, Trunktank);}}

Look for the code, we do not see the specific product generation class, can only see the different methods of creation, so the abstract factory is faced with abstract product production, the car is an abstract product concept, it is from the car, truck abstraction, and we now want to achieve their abstract basis of the various products, It would be appropriate to use an abstract factory in this way, and we can see that the engine and the tank are all two abstract bases, cars and trucks.

And if abstract products add an abstract basis, for example: sports cars, then we just need to add a section of the client to the sports car-related code, and add a system of factory implementation and product implementation class can be, completely will not affect the other series of code.

Two systems, two factory implementations.

Package Com.factory.abstrcatfactory.factory;import Com.factory.abstrcatfactory.product.iengine;import Com.factory.abstrcatfactory.product.itank;import com.factory.abstrcatfactory.product.ivehicle;/** * @author Gaoxu * Practice the truth!  */public interface Iabstrcatfactory {/** * @author Gaoxu * @return */public itank createtankobject ();/** * @author Gaoxu * @return */public iengine createengineobject ()/** * @author Gaoxu * @return */public ivehicle createvehicleobject ();

Package Com.factory.abstrcatfactory.factory;import Com.factory.abstrcatfactory.product.car;import Com.factory.abstrcatfactory.product.carengine;import Com.factory.abstrcatfactory.product.cartank;import Com.factory.abstrcatfactory.product.iengine;import Com.factory.abstrcatfactory.product.itank;import com.factory.abstrcatfactory.product.ivehicle;/** Car Factory Implementation * @author Gaoxu * Practice the truth! */public class Carfactory implements iabstrcatfactory{@Overridepublic Itank Createtankobject () {return new Cartank ();} @Overridepublic iengine Createengineobject () {return new Carengine ();} @Overridepublic ivehicle Createvehicleobject () {return new Car ();}}

Package Com.factory.abstrcatfactory.factory;import Com.factory.abstrcatfactory.product.iengine;import Com.factory.abstrcatfactory.product.itank;import Com.factory.abstrcatfactory.product.ivehicle;import Com.factory.abstrcatfactory.product.trunk;import Com.factory.abstrcatfactory.product.trunkengine;import com.factory.abstrcatfactory.product.trunktank;/** Truck Factory Implementation * @author Gaoxu * Practice the truth! */public class Trunkfactory implements iabstrcatfactory{@Overridepublic Itank Createtankobject () {return new  Trunktank ();} @Overridepublic iengine Createengineobject () {return new Trunkengine ();} @Overridepublic ivehicle Createvehicleobject () {return new Trunk ();}}

Automotive products, engine products, fuel tank products

The relationship between them is a kind of aggregation.


Package Com.factory.abstrcatfactory.product;import Com.factory.abstrcatfactory.model.engine;import Com.factory.abstrcatfactory.model.tank;import com.factory.abstrcatfactory.model.vehicle;/** Automotive Product Interface * @author Gaoxu * Practice the truth! */public interface Ivehicle {public Vehicle createvehicle (Engine engine,tank Tank);}

Package Com.factory.abstrcatfactory.product;import Com.factory.abstrcatfactory.model.engine;import Com.factory.abstrcatfactory.model.tank;import com.factory.abstrcatfactory.model.vehicle;/** Car Product realization * @author Gaoxu * Practice the truth! */public class Car implements ivehicle{@Overridepublic Vehicle createvehicle (Engine engine,tank Tank) {Vehicle Car = new V Ehicle (); Car.setengine (engine); Car.settank (tank); return car;}}

Package Com.factory.abstrcatfactory.product;import Com.factory.abstrcatfactory.model.engine;import Com.factory.abstrcatfactory.model.tank;import com.factory.abstrcatfactory.model.vehicle;/** Truck Product Implementation * @author Gaoxu * Practice the truth! */public class Trunk implements ivehicle{@Overridepublic Vehicle createvehicle (engine engine, Tank Tank) {Vehicle Trunk = New Vehicle (); Trunk.setengine (engine); Trunk.settank (tank); return trunk;}}
Package Com.factory.abstrcatfactory.product;import com.factory.abstrcatfactory.model.engine;/** Transmitter Product Interface * @author Gaoxu * Practice the truth! */public interface Iengine {public Engine createengine ();}

Package Com.factory.abstrcatfactory.product;import com.factory.abstrcatfactory.model.engine;/** Trolley Transmitter Product Implementation * @author Gaoxu * Practice the truth! */public class Carengine implements iengine{@Overridepublic Engine createengine () {//TODO auto-generated method Stubretu RN null;}}

Package Com.factory.abstrcatfactory.product;import com.factory.abstrcatfactory.model.engine;/** Truck engine product Implementation * @author Gaoxu * Practice the truth! */public class Trunkengine implements iengine{@Overridepublic Engine createengine () {//TODO auto-generated method Stubre Turn null;}}

Package Com.factory.abstrcatfactory.product;import com.factory.abstrcatfactory.model.tank;/** Tank Product Interface * @author Gaoxu * Practice the truth! */public interface Itank {public Tank createtank ();}

Package Com.factory.abstrcatfactory.product;import com.factory.abstrcatfactory.model.tank;/** Trolley Fuel tank Product Implementation * @author Gaoxu * Practice the truth! */public class Cartank implements itank{@Overridepublic Tank Createtank () {return null;}}

Package Com.factory.abstrcatfactory.product;import com.factory.abstrcatfactory.model.tank;/** Truck Fuel tank product Implementation * @author Gaoxu * Practice the truth! */public class Trunktank implements itank{@Overridepublic Tank Createtank () {//TODO auto-generated method Stubreturn null ;}}

With an example of a production car, we can clearly recognize the flexibility of the abstract factory model, and all products are essentially non-coupled when created, and we only need to combine them based on the aggregation relationship between them.

Summarize the advantages and disadvantages of the abstract factory:

1: Decoupling and encapsulation are the greatest advantages of the abstract factory.

2: Flexibility to respond to the increasing demands of the product line.

Disadvantages:

1: The code workload is too large for each additional product line.

From this we can also understand that the application of design patterns does not solve all problems perfectly, so it depends on our specific focus.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Follow the example to learn the design pattern-abstract Factory

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.