Create pattern--Abstract Factory mode

Source: Internet
Author: User

Overview
    Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
Applicability
    1. A system is independent of the creation, composition, and presentation of its products.    2. When a system is to be configured by one of multiple product families.    3. When you want to emphasize the design of a series of related product objects for joint use.    4. When you provide a Product class library and just want to display their interfaces instead of implementing them.
participants
    1.AbstractFactory      declares an operation interface that creates an abstract product object.    2.ConcreteFactory      implements the operation of creating a specific product object.    3.AbstractProduct      declares an interface for a class of product objects.    4.ConcreteProduct      defines a product object that will be created by the corresponding specific factory.      implements the Abstractproduct interface.    5.Client      Use only interfaces declared by the Abstractfactory and Abstractproduct classes

above are some very orthodox explanations, summarizing the above concepts, I can assume that the abstract factory pattern is applicable in the following cases:

example of buying a car:when we buy a car, we don't pay attention to how the car is made up, and the details of the car's assembly, because it's not something we think about (i.e. a
the system does not depend on how product instances are created, combined, and expressed in detail.
Of course there are many kinds of cars, so we have a lot of options (System products are more than one family, the system only consumes one of them).
when it comes to consumption, if we choose a brand, then we must buy the whole car, not just a small part of it; a brand of car
type, we can call it a product family, so the product of the same product family is used together.





Code Description:
engine interface and specific implementation:
/** * Engine interface * @author Linhai Gu * */public interface Motor {public void motors ();

public class Aodimotor implements Motor{private string Name=null;public aodimotor (string name) {This.name=name;} @Overridepublic void Motor () {System.out.println (name+ "engine");}}

public class Baomamotor implements Motor{private string Name=null;public baomamotor (string name) {This.name=name;} @Overridepublic void Motor () {System.out.println (name+ "engine");}}


Tire interface and specific implementation:
/** * Tyre interface * @author Linhai Gu * */public interface Tyre {public void tyre ();

public class Aodityre implements Tyre{private string Name=null;public Aodityre (string name) {This.name=name;} @Overridepublic void Tyre () {System.out.println (name+ "tyre");}

public class Baomatyre implements Tyre{private string Name=null;public Baomatyre (string name) {This.name=name;} @Overridepublic void Tyre () {System.out.println (name+ "tyre");}

Steering wheel interface with specific implementation:
/** * Steering Wheel interface * @author Linhai Gu * */public interface Steeringwhell {public void Steeringwhell ();}

public class Aodisteeringwhell implements Steeringwhell{private string Name=null;public Aodisteeringwhell (string name) {this.name=name;} @Overridepublic void Steeringwhell () {System.out.println (name+ "of the Steering Wheel");}}

public class Baomasteeringwhell implements Steeringwhell{private string Name=null;public Baomasteeringwhell (string Name) {This.name=name;} @Overridepublic void Steeringwhell () {System.out.println (name+ "of the Steering Wheel");}}

Abstract Factory class:
Public interface Abstractfactory {/* * Create motor Object */public motor creatmotor ();/* * Create Tyre object */public tyre  Creattyre (); * * Create Steeringwhell object */public Steeringwhell Creatsteeringwhell ();}

Implementation class:
public class Aodifactory implements abstractfactory{@Overridepublic Motor creatmotor () {//TODO auto-generated method Stu Breturn new Aodimotor ("Audi"); @Overridepublic Tyre Creattyre () {//TODO auto-generated method Stubreturn new Aodityre ("Audi"); @Overridepublic Steeringwhell Creatsteeringwhell () {//TODO auto-generated method Stubreturn new Aodisteeringwhell ("Audi" );}}


public class Baomafactory implements abstractfactory{@Overridepublic Motor creatmotor () {//TODO auto-generated Method St Ubreturn new Baomamotor ("BMW"); @Overridepublic Tyre Creattyre () {//TODO auto-generated method Stubreturn new Baomatyre ("BMW"); @Overridepublic Steeringwhell Creatsteeringwhell () {//TODO auto-generated method Stubreturn new Baomasteeringwhell ("BMW ");}}

Brand manufacturers to make car categories:
</pre><pre name= "code" class= "Java" >public class Carengineer {//car required engine private motor motor=null;// Car needs tires private tyre tyre=null;//car need steering wheel private steeringwhell steeringwhell=null;public void Makecar (Abstractfactory af ) {this.motor=af.creatmotor (); This.tyre=af.creattyre (); This.steeringwhell=af.creatsteeringwhell (); This.motor.motor (); This.tyre.tyre (); This.steeringWhell.steeringWhell ();}}

Test:
public class Test {public static void main (string[] args) {//TODO auto-generated method stub//brand manufacturer Carengineer Carengineer =new Carengineer ();//customer 1 selected brand Abstractfactory aodi=new aodifactory ();//customer 2 selected brand Abstractfactory baoma=new baomafactory ( );//Manufacturer launches SYSTEM.OUT.PRINTLN ("Customer 1:"); Carengineer.makecar (Aodi); System.out.println ("Customer 2:"); Carengineer.makecar (Baoma);}}

Operation Result:




As you can see from the above, if you want to add a new product to the entire product family, you have to modify the abstract factory, which means that you need to modify all the factory implementation classes, so the abstract Factory mode is not easy to expand the new product





Reprint Please specify the Source:http://blog.csdn.net/hai_qing_xu_kong/article/details/42084395Emotional Control _





Create pattern--Abstract Factory mode

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.