Java Common design pattern explanation (i)---Factory mode __java

Source: Internet
Author: User
Tags stub

Principles of Design Patterns: Programming for interfaces

the role of the factory model:

A. In the design of an application, create or unify the creation of objects in one place (spring)

B. You can add objects directly without changing the application, while also facilitating the maintenance of objects.

type of factory pattern:

A. Simple Factory

B. Factory methods

C. Abstract Factory

Wedge:

Say the Northeast boss home has three cars, Mercedes-Benz, BMW, Blue Lamborghinis, hired an experienced driver, different occasions he will tell the driver to open a different car entertainment.

Simple factory:

Abstruact:

package com.product.abstruct;

Public interface Car {

Public Void Drive ()///car is used for driving, so we need an engine.
}

Implements

package com.product.implement;

import Com.product.abstruct.Car;

             public class Audi Implements car {

                  public void Drive () {//BMW He's also a car, not a horse, but an engine.
                           //TODO auto-generated Method Stub
                           system.out.println ("Yes,today wo are driving Audi .... ");
                    }
               }
            
             &NBSP package com.product.implement;

import Com.product.abstruct.Car;

Public class Benz implements car {

             public Void Drive () {// Audi, he's a car, not a Dior, but an engine.
                      //TODO auto-generated Method stub
                       System.out.println ("Yes,today wo are driving Benz ....") ");
                      system.out.println ("Boss, some according to your instructions ...");
                    }

                }

to the Lamborghini of the province = =

Factory class:

Factory class is a management class, is the direct head of the car, this person is the boss of the driver;

Package com.product.factory;
Import Com.product.abstruct.Car;
Import Com.product.implement.Audi;
Import Com.product.implement.Benz;

public class Driver {
public static car Drivecar (String carname) throws exception{
if (Carname.equalsignorecase ("Benz")) {
return new Benz ();
}
if (Carname.equalsignorecase ("BMW")) {
return new BWM ();
}
Return to New Audi ();
}
}

One day the boss is going to England to watch Liverpool's home match against Aston Villa, he needs to drive a BMW to the airport, so he calls the driver and asks him to pick him up 2 o'clock in the afternoon BMW.

package com.product.test;
Import Com.product.abstruct.Car;
Import Com.product.factory.Driver;

public class Test {
public static void Main (string[] args) throws exception{
Tell the driver which car wo drive Todaty wo'll to Amsterdam
Car cars = Driver.drivecar ("Benz");/Let the driver come and pick up
Mrs Wang Go
Car.drive ()//One key start
}

                       }


Factory Method:

as the boss's business grows bigger, more and more money, so he has bought a few sports cars----Ferrari, Porsche, Jaguar three driving, as the car two increased, all the cars have to have an old driver management, annual inspection, insurance, ticket, car wash, the old driver a person is really unbearable, And the boss will tell the old driver to drive a different style of car from Monday to Saturday. At this time the old driver had to remember the day the boss needed to drive a car out, and then to the boss suggested: for each car to hire a driver, each car has a dedicated personnel responsible for the need to go out when I say, I will send the corresponding personnel to pick up the boss, the boss did not hesitate to say: so do ...

abstract Car class:

package com.product.car.abstruct;

Public interface Car {

Public Void Drive ();
}

Audi Car:

package com.product.car.implement;
Import Com.product.car.abstruct.Car;

Public class Audidriver implements car {

Public Void Drive () {

SYSTEM.OUT.PRINTLN ("Want to be an official four rings");

}
}

Benz Car:

package com.product.car.implement;
Import Com.product.car.abstruct.Car;

Public class Audidriver implements car {

Public Void Drive () {

SYSTEM.OUT.PRINTLN ("Want to be an official four rings");

}
}

= = = Omit other car = = =

car chief (old driver)

package Com.product.car.manager;
Import Com.product.car.abstruct.Car;

Public interface Carmanager {

Public car Drivecar ();

}

the newly recruited Audi driver:

package Com.product.car.managerImpl;
Import Com.product.car.abstruct.Car;
Import Com.product.car.implement.AudiDriver;
Import Com.product.car.manager.CarManager;

Public class Audimanager implements Carmanager {

Public car Drivecar () {

return new Audidriver ();

}
}

newly recruited Mercedes-Benz drivers:

package Com.product.car.managerImpl;
Import Com.product.car.abstruct.Car;
Import Com.product.car.implement.BenzDriver;
Import Com.product.car.manager.CarManager;

                       public class Benzmanager implements Carmanager {
 
                             public Car Drivecar () {
  
                                    return new Benzdriver ();
  
                            }
                      }

one day the boss wants to go to Liverpool:

package com.product.test;
Import Com.product.car.abstruct.Car;
&n

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.