設計模式---工廠系列

1.靜態工廠java

調用對象的方法生成一個對象,每次調用都是同一個對象,只要對象裏控制了產生對象的方法,就叫靜態工廠。(car類裏面只有一個靜態car對象稱爲單例,若是是一list的car,就是多例)spring

示例代碼:app

測試類
public class Test {
public static void main(String[] args) {
Car c = Car.getInstance();
Car c2 = Car.getInstance();
System.out.println(c==c2);
c.run();
}
}
//輸出結果
true
car hohoho ....

car類ide

public class Car {
private Car() {}; //private方法修飾構造方法,不能被調用
private static Car car = new Car();
public static Car getInstance() { //靜態方法產生car對象,只有一個對象
return car;
}
public void run() {
System.out.println("car hohoho ....");
}
}

2.簡單工廠(使用抽象類方法,生產對象和生產過程本身控制)工具

關係圖:測試

實現代碼:對象

代碼段1:接口

//調用程序
public class Main {
public static void main(String[] args) {
Factory f = new CarFactory();// = new PlaneFactory();
movable m = f.create();
m.run();
}
}

代碼段2://交通工具類get

//共性接口
public interface movable {
void run();
}
//實現接口的car
public class Car implements movable{
public void run() {
System.out.println("car......");
}
}
//實現接口的plane
public class plane implements movable{
@Override
public void run() {
System.out.println("plane......");
}
}

代碼段3://生產交通工具的工廠(VehicleFactory)產品

//類接口
public interface VehicleFactory {
movable create();
}
//生產car的工廠
public class CarFactory implements VehicleFactory {
@Override
public movable create() {
return new Car();
}
}
//生產plane的工廠
public class PlaneFactory implements VehicleFactory {
@Override
public movable create() {
return new plane();
}
}

3.抽象工廠(工廠生產一系列產品)

關係圖:

代碼:

 

 

//調用類
public class Main {
public static void main(String[] args) {
AbstractFactory df = new DefaultFactory();// = new magicFactory();
df.createfood().printname();
df.createVehicle().run();
df.createweapon().shoot();
}
}
//運行結果
apple ....
car ....
ak47 .....
//抽象工廠類
public abstract class AbstractFactory {
public abstract vehicle createVehicle();
public abstract weapon createweapon();
public abstract Food createfood();
}
//工廠一
public class DefaultFactory extends AbstractFactory{
@Override
public vehicle createVehicle() {
return new car();
}
@Override
public Food createfood() {
return new Apple();
}
@Override
public weapon createweapon() {
return new Ak47();
}
}
//工廠2
public class MagicFactory extends AbstractFactory{
@Override
public vehicle createVehicle() {
return new bangzi();
}
@Override
public Food createfood() {
return new mushroom();
}
@Override
public weapon createweapon() {
return new Magicstick();
}
}
//產品抽象類和產品
1 weapon
public abstract class weapon {
public abstract void shoot();
}
11
public class Ak47 extends weapon{
@Override
public void shoot() {
System.out.println("ak47 .....");
}
}
12
public class Magicstick extends weapon{
@Override
public void shoot() {
System.out.println("magicstick ....");
}
}
2 Food
public abstract class Food {
public abstract void printname();
}
21
public class Apple extends Food{
@Override
public void printname() {
System.out.println("apple ....");
}
}
22
public class mushroom extends Food {
@Override
public void printname() {
System.out.println("mushroom ....");
}
}
3 vehicle
public abstract class vehicle {
public abstract void run();
}
31
public class car extends vehicle{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("car ....");
}
}
32
public class bangzi extends vehicle{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("bangzi ....");
}
}

總結

抽象類工廠解決了嗎實現改變工廠改變一下類產品的問題,可是產品固定。

簡單工廠解決了產品和生產過程的工廠本身控制,產品多樣。

兩種思路是不可調和的,可是能夠實現合理安排,例如spring的beanFactory。

相關文章
相關標籤/搜索