1. 程式人生 > >設計模式——9.抽象工廠

設計模式——9.抽象工廠

抽象工廠(Abstract Factory)

Abstract Factory模式簡介:

提供一個建立一系列相關或者相互依賴物件的介面,而無需指定它們具體的類。

Abstract Factory模式結構:

C++程式碼

AbstractProductA抽象類(AbstractProductB抽象類同理):

    
    //file: AbstractProductA.h
    #pragma once
    class AbstractProductA
    {
    public:
        AbstractProductA();
        virtual ~AbstractProductA();
        
        virtual void stepA() = 0;
    };
    
    //file: AbstractProductA.cpp
    #include "pch.h"
    #include "AbstractProductA.h"
    
    AbstractProductA::AbstractProductA() {}
    
    AbstractProductA::~AbstractProductA() {}

ProductA1具體實現類(ProductA2/B1/B2同理):

    //file: ProductA1.h
    #pragma once
    #include "AbstractProductA.h"
    class ProductA1 : public AbstractProductA
    {
    public:
        ProductA1();
        virtual ~ProductA1();
    
        void stepA();
    };
    
    //file: ProductA1.cpp
    #include "pch.h"
    #include "ProductA1.h"
    #include <iostream>
    
    ProductA1::ProductA1() {}
    
    ProductA1::~ProductA1() {}
    
    void ProductA1::stepA()
    {
        std::cout << "Product A1's step !" << std::endl;
    }

AbstractFactory抽象類:


    //file: AbstractFactory.h
    #pragma once
    #include "AbstractProductA.h"
    #include "AbstractProductB.h"
    
    class AbstractFactory
    {
    public:
        AbstractFactory();
        virtual ~AbstractFactory();
    
        virtual AbstractProductA* createProductA() = 0;
        virtual AbstractProductB* createProductB() = 0;
    };

    //file: AbstractFractory.cpp
    #include "pch.h"
    #include "AbstractFactory.h"
    
    AbstractFactory::AbstractFactory() {}
    
    AbstractFactory::~AbstractFactory() {}

ConcreteFactory1具體實現類(ConcreteFactory2同理)


    //file: ConcreteFactory1.h
    #pragma once
    #include "AbstractFactory.h"
    class ConcreteFactory1 : public AbstractFactory
    {
    public:
        ConcreteFactory1();
        virtual ~ConcreteFactory1();
    
        virtual AbstractProductA* createProductA();
        virtual AbstractProductB* createProductB();
    };


    //file: ConcreteFactory1.cpp
    #include "pch.h"
    #include "ConcreteFactory1.h"
    #include "ProductA1.h"
    #include "ProductB1.h"
    
    ConcreteFactory1::ConcreteFactory1() {}
    
    ConcreteFactory1::~ConcreteFactory1() {}
    
    AbstractProductA* ConcreteFactory1::createProductA()
    {
        return new ProductA1();
    }
    
    AbstractProductB* ConcreteFactory1::createProductB()
    {
        return new ProductB1();
    }

客戶端程式碼:


    // AbstractFactoryPattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
    
    #include "pch.h"
    #include <iostream>
    #include "ConcreteFactory1.h"
    #include "ConcreteFactory2.h"
    using namespace std;
    
    int main()
    {
        AbstractFactory* frac = new ConcreteFactory1();
        //AbstractFactory* frac = new ConcreteFactory2();
        AbstractProductA* pa = frac->createProductA();
        AbstractProductB* pb = frac->createProductB();
    
        pa->stepA();
        pb->stepB();
    
        delete frac;
        delete pa;
        delete pb;
    
        char c;
        cin >> c;
        return 0;
    }

C#程式碼

ProductA抽象類以及1系列和2系列的具體實現類:


    public abstract class AbstractProductA
    {
        public abstract void StepA();
    }

    public class ProductA1 : AbstractProductA
    {
        public override void StepA()
        {
            Console.WriteLine("Product A1's Step !");
        }
    }

    public class ProductA2 : AbstractProductA
    {
        public override void StepA()
        {
            Console.WriteLine("Product A2's Step !");
        }
    }

ProductB抽象類以及1系列和2系列的具體實現類:


    public abstract class AbstractProductB
    {
        public abstract void StepB();
    }

    public class ProductB1 : AbstractProductB
    {
        public override void StepB()
        {
            Console.WriteLine("Product B1's Step !");
        }
    }

    public class ProductB2 : AbstractProductB
    {
        public override void StepB()
        {
            Console.WriteLine("Product B2's Step !");
        }
    }

AbstractFractory以及1系列和2系列的ConcreteFractory:


    public abstract class AbstractFactory
    {
        public abstract AbstractProductA CreatProductA();
        public abstract AbstractProductB CreatProductB();
    }

    public class ConcreteFactory1 : AbstractFactory
    {
        public override AbstractProductA CreatProductA()
        {
            return new ProductA1();
        }

        public override AbstractProductB CreatProductB()
        {
            return new ProductB1();
        }
    }

    public class ConcreteFactory2 : AbstractFactory
    {
        public override AbstractProductA CreatProductA()
        {
            return new ProductA2();
        }

        public override AbstractProductB CreatProductB()
        {
            return new ProductB2();
        }
    }

客戶端程式碼::


    AbstractFactory frac = new ConcreteFactory1();
    //AbstractFactory frac = new ConcreteFactory2();

    AbstractProductA pa = frac.CreatProductA();
    AbstractProductB pb = frac.CreatProductB();
    pa.StepA();
    pb.StepB();

REF

書籍:

設計模式與遊戲開發、大話設計模式

GitHub:

https://github.com/me115/design_patterns