日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

简单工厂模式,抽象工厂模式,反射工厂模式的代码总结

發(fā)布時間:2025/6/15 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
工廠模式也用了不少,特別是MS的petshop中對數(shù)據(jù)庫的訪問,通過工廠模式可以達到自由切換SQL 和 Oracle 數(shù)據(jù)庫。近來也在看設(shè)計模式的書,發(fā)現(xiàn)工廠模式還是有不少的擴展。結(jié)合書中的知識和我自己理解,從代碼的角度來比較工廠模式各變種。

1:簡單工廠模式:其作用是實例化對象而不需要客戶了解這個對象屬于那個具體的子類

using?System;
using?System.Collections;

public?class?MyClass
{
????public?static?void?Main()
????{
????????//通過參數(shù)來實例化子類。
????????IVehicle?vehicle?=?FactoryVehicle.CreateVehicle("car");
????????vehicle.go();
????????Console.ReadLine();????
????}

????
}


public?class?FactoryVehicle
{
????public?static?IVehicle?CreateVehicle(string?VehicleName)
????{
????????switch(VehicleName.ToLower())
????????{
????????????case?"car":
????????????return?new?Car();
????????????case?"boat":
????????????return?new?Boat();
????????????default:
????????????return?new?Car();
????????????
????????}

????}


}

public?interface?IVehicle
{
????void?go();
}

public?class?Car:IVehicle
{
????public?void?go()
????{
????????Console.WriteLine("car");
????}

}

public?class?Boat:IVehicle
{
????public?void?go()
????{
????????Console.WriteLine("boat");
????}

}

2:抽象工廠:即將工廠方法也抽象出來,由具體的子類來實例化工廠。產(chǎn)品創(chuàng)建部分和簡單工廠模式相同。
using?System;
using?System.Collections;

public?class?MyClass
{
????public?static?void?Main()
????{
????????//通過定義的工廠來實例化。弊端是當產(chǎn)品很多時需要增加很多的工廠。代碼重復。
????????FactoryVehicle?factory?=?new?FactoryCar();
????????IVehicle?vehicle?=?factory.CreateVehicle();
????????vehicle.go();
????????Console.ReadLine();????
????}

????
}



public?interface?FactoryVehicle
{
?????IVehicle?CreateVehicle();

}


public?class?FactoryCar:FactoryVehicle
{
????public?IVehicle?CreateVehicle()
????{
????????return?new?Car();
????}

}


public?class?FactoryBoat:FactoryVehicle
{
????public?IVehicle?CreateVehicle()
????{
????????return?new?Boat();
????}

}


public?interface?IVehicle
{
????void?go();
}


public?class?Car:IVehicle
{
????public?void?go()
????{
????????Console.WriteLine("car");
????}

}


public?class?Boat:IVehicle
{
????public?void?go()
????{
????????Console.WriteLine("boat");
????}

}
3:反射工廠模式: 其實就是通過反射的方式來獲得具體實例化是那個類。
using?System;
using?System.Collections;
using?System.Reflection;

public?class?MyClass
{
????public?static?void?Main()
????{
????????//使用反射的方法獲得實例化的類
????????IVehicle?vechicle?=?ReflectFactory.CreateVehicleByReflect("Car");
????????vechicle.go();
????????Console.ReadLine();????
????????
????}

????
}


public?class?ReflectFactory
{
????public?static?IVehicle?CreateVehicleByReflect(string?typeName)
????{
????????Type?type?=?Type.GetType(typeName);
????????ConstructorInfo??ci?=?type.GetConstructor(System.Type.EmptyTypes);;
????????return?(IVehicle)ci.Invoke(null);
????}

}

public?class?FactoryBoat:FactoryVehicle
{
????public?IVehicle?CreateVehicle()
????{
????????return?new?Boat();
????}

}


public?class?FactoryCar:FactoryVehicle
{
????public?IVehicle?CreateVehicle()
????{
????????return?new?Car();
????}

}


public?interface?FactoryVehicle
{
????IVehicle?CreateVehicle();

}

public?interface?IVehicle
{
????void?go();
}


public?class?Car:IVehicle
{
????public?void?go()
????{
????????Console.WriteLine("car");
????}

}


public?class?Boat:IVehicle
{
????public?void?go()
????{
????????Console.WriteLine("boat");
????}

}

總結(jié)

以上是生活随笔為你收集整理的简单工厂模式,抽象工厂模式,反射工厂模式的代码总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。