コード例 #1
0
        // Use this for initialization
        void Start()
        {
            Debug.Log("第一种方式:由子类产生");
            Factory factory1 = new ConcreteCreatorProductA();

            factory1.FactoryMethod();
            Factory factory2 = new ConcreteCreatorProductB();

            factory2.FactoryMethod();

            Debug.Log("\n ---------------------------------- \n");

            Debug.Log("第二种方式:在FactoryMethod 增加参数");
            ConcreteCreator_MethodType creator_methodType = new ConcreteCreator_MethodType();
            Product product_MethodType = creator_methodType.FactoryMethod(2);

            Debug.Log("\n ---------------------------------- \n");

            Debug.Log("第三种方式:Creator 泛型类");
            Creator_GenericClass <ConcreteProductB> creator_GenericClass = new Creator_GenericClass <ConcreteProductB>();

            creator_GenericClass.FactoryMethod();

            Debug.Log("\n ---------------------------------- \n");

            Debug.Log("第四种方式:FactoryMethod 泛型方法");
            ConcreteCreator_GenericMethod creator_GenericMethod = new ConcreteCreator_GenericMethod();

            creator_GenericMethod.FactoryMethod <ConcreteProductB>();
        }
コード例 #2
0
    void UnitTest()
    {
        // 產品
        Product theProduct = null;

        // 工廠界面
        Creator theCreator = null;

        // 設定為負責ProduceA的工廠
        theCreator = new ConcreteCreatorProductA();
        theProduct = theCreator.FactoryMethod();

        // 設定為負責ProduceB的工廠
        theCreator = new ConcreteCreatorProductB();
        theProduct = theCreator.FactoryMethod();

        // 工廠界面
        Creator_MethodType theCreatorMethodType = new ConcreteCreator_MethodType();

        // 取得兩個產品
        theProduct = theCreatorMethodType.FactoryMethod(1);
        theProduct = theCreatorMethodType.FactoryMethod(2);

        // 使用Generic Method
        ConcreteCreator_GenericMethod theCreatorGM = new ConcreteCreator_GenericMethod();

        theProduct = theCreatorGM.FactoryMethod <ConcreteProductA>();
        theProduct = theCreatorGM.FactoryMethod <ConcreteProductB>();

        // 使用Generic Class
        // 負責ProduceA的工廠
        Creator_GenericClass <ConcreteProductA> Creator_ProductA = new Creator_GenericClass <ConcreteProductA>();

        theProduct = Creator_ProductA.FactoryMethod();

        // 負責ProduceA的工廠
        Creator_GenericClass <ConcreteProductB> Creator_ProductB = new Creator_GenericClass <ConcreteProductB>();

        theProduct = Creator_ProductB.FactoryMethod();
    }
コード例 #3
0
    void UnitTest()
    {
        // 产品
        Product theProduct = null;

        // 工厂界面
        Creator theCreator = null;

        // 设定为负责ProduceA的工厂
        theCreator = new ConcreteCreatorProductA();
        theProduct = theCreator.FactoryMethod();

        // 设定为负责ProduceB的工厂
        theCreator = new ConcreteCreatorProductB();
        theProduct = theCreator.FactoryMethod();

        // 工厂界面
        Creator_MethodType theCreatorMethodType = new ConcreteCreator_MethodType();

        // 取得两个产品
        theProduct = theCreatorMethodType.FactoryMethod(1);
        theProduct = theCreatorMethodType.FactoryMethod(2);

        // 使用Generic Method
        ConcreteCreator_GenericMethod theCreatorGM = new ConcreteCreator_GenericMethod();

        theProduct = theCreatorGM.FactoryMethod <ConcreteProductA>();
        theProduct = theCreatorGM.FactoryMethod <ConcreteProductB>();

        // 使用Generic Class
        // 负责ProduceA的工厂
        Creator_GenericClass <ConcreteProductA> Creator_ProductA = new Creator_GenericClass <ConcreteProductA>();

        theProduct = Creator_ProductA.FactoryMethod();

        // 负责ProduceA的工厂
        Creator_GenericClass <ConcreteProductB> Creator_ProductB = new Creator_GenericClass <ConcreteProductB>();

        theProduct = Creator_ProductB.FactoryMethod();
    }
コード例 #4
0
    public static void UnitTest()
    {
        System.Console.WriteLine("Basic Factory Method");

        Product theProduct = null;

        Creator theCreator = null;

        theCreator = new ConcreteCreatorProductA();
        theProduct = theCreator.FactoryMethod();

        theCreator = new ConcreteCreatorProductB();
        theProduct = theCreator.FactoryMethod();

        System.Console.WriteLine("\nMethod_type Factory Method");

        ConcreteCreator_MethodType theCreatorMethodType = new ConcreteCreator_MethodType();

        theProduct = theCreatorMethodType.FactoryMethod(1);
        theProduct = theCreatorMethodType.FactoryMethod(2);

        System.Console.WriteLine("\nGeneric Class Factory Method");

        Creator_GenericClass <ConcreteProductA> Creator_ProductA = new Creator_GenericClass <ConcreteProductA>();

        theProduct = Creator_ProductA.FactoryMethod();

        Creator_GenericClass <ConcreteProductB> Creator_ProductB = new Creator_GenericClass <ConcreteProductB>();

        theProduct = Creator_ProductB.FactoryMethod();

        System.Console.WriteLine("\nGeneric Method Factory Method");
        ConcreteCreator_GenericMethod theCreatorGM = new ConcreteCreator_GenericMethod();

        theProduct = theCreatorGM.FactoryMethod <ConcreteProductA>();
        theProduct = theCreatorGM.FactoryMethod <ConcreteProductB>();
    }
コード例 #5
0
	void UnitTest () {

		// 產品
		Product theProduct = null;

		// 工廠界面
		Creator theCreator = null;

		// 設定為負責ProduceA的工廠
		theCreator = new ConcreteCreatorProductA();
		theProduct = theCreator.FactoryMethod();

		// 設定為負責ProduceB的工廠
		theCreator = new ConcreteCreatorProductB();
		theProduct = theCreator.FactoryMethod();

		// 工廠界面
		Creator_MethodType theCreatorMethodType = new ConcreteCreator_MethodType();

		// 取得兩個產品
		theProduct = theCreatorMethodType.FactoryMethod(1);
		theProduct = theCreatorMethodType.FactoryMethod(2);

		// 使用Generic Method
		ConcreteCreator_GenericMethod theCreatorGM = new ConcreteCreator_GenericMethod();
		theProduct = theCreatorGM.FactoryMethod<ConcreteProductA>();
		theProduct = theCreatorGM.FactoryMethod<ConcreteProductB>();

		// 使用Generic Class
		// 負責ProduceA的工廠
		Creator_GenericClass<ConcreteProductA> Creator_ProductA = new Creator_GenericClass<ConcreteProductA>();
		theProduct = Creator_ProductA.FactoryMethod();

		// 負責ProduceA的工廠
		Creator_GenericClass<ConcreteProductB> Creator_ProductB = new Creator_GenericClass<ConcreteProductB>();
		theProduct = Creator_ProductB.FactoryMethod();
	}