Kody źródłowe/Fabryka abstrakcyjna (wzorzec projektowy)

Z Wikibooks, biblioteki wolnych podręczników.
Przejdź do nawigacji Przejdź do wyszukiwania
Fabryka abstrakcyjna (wzorzec projektowy)
Kod źródłowy
Kody źródłowe programów stosujących wzorzec projektowy fabryki abstrakcyjnej
Wikipedia
Zobacz w Wikipedii hasło Fabryka abstrakcyjna


Java[edytuj]

/* Przykład GUIFactory */

abstract class GUIFactory {
    public static GUIFactory getFactory() {
        int sys = readFromConfigFile("OS_TYPE");
        if (sys == 0) {
            return new WinFactory();
        } else {
            return new OSXFactory();
        }
    }

    public abstract Button createButton();
}


class WinFactory extends GUIFactory {
    public Button createButton() {
        return new WinButton();
    }
}


class OSXFactory extends GUIFactory {
    public Button createButton() {
        return new OSXButton();
    }
}


 
abstract class Button {
    public abstract void paint();
}


class WinButton extends Button {
    public void paint() {
        System.out.println("Przycisk WinButton");
    }
}

 
class OSXButton extends Button {
    public void paint() {
        System.out.println("Przycisk OSXButton");
    }
}

 
public class Application {
    public static void main(String[] args) {
        GUIFactory factory = GUIFactory.getFactory();
        Button button = factory.createButton();
        button.paint();
    }
    // Wyświetlony zostanie tekst:
    //   "Przycisk WinButton"
    // lub:
    //   "Przycisk OSXButton"
}


C#[edytuj]

using System;
using System.Collections.Generic;

namespace TheAbstractFactoryPattern
{
    enum OS_TYPE
    {
        Windows,
        OsX
    }

    // Przykład GUIFactory 

    abstract class GUIFactory
    {
        /// <summary>
        /// getFactory returns concrete Factory,
        ///instead parameter abstract Factory can get OS_Type from outer method
        /// </summary>
        /// <param name="type">Operating System</param>
        /// <returns></returns>
        public static GUIFactory getFactory(OS_TYPE type)
        {
            switch (type)
            {
                case OS_TYPE.Windows:
                    return new WinFactory();

                case OS_TYPE.OsX:
                    return new OSXFactory();
                default:
                    throw new NotImplementedException();
            }
        }

        public abstract Button createButton();
    }


    class WinFactory : GUIFactory
    {
        public override Button createButton()
        {
            return new WinButton();
        }
    }


    class OSXFactory : GUIFactory
    {
        public override Button createButton()
        {
            return new OSXButton();
        }
    }

    abstract class Button
    {
        public abstract void paint();
    }

    class WinButton : Button
    {
        public override void paint()
        {
            Console.WriteLine("Przycisk WinButton");
        }
    }

    class OSXButton : Button
    {
        public override void paint()
        {
            Console.WriteLine("Przycisk OSXButton");
        }
    }


    public class Application
    {
        public static void Main(String[] args)
        {
            GUIFactory factory = GUIFactory.getFactory(OS_TYPE.Windows);
            Button button = factory.createButton();
            button.paint();
            Console.ReadLine();
        }
        // Wyświetlony zostanie tekst:
        //   "Przycisk WinButton"
        // lub:
        //   "Przycisk OSXButton"
    }
}


C++[edytuj]

// source: factory.hpp
#ifndef GUI_FACTORY_HPP_
#define GUI_FACTORY_HPP_

enum OSType {
    OS_Windows,
    OS_OSX,
    OS_Unknown
};

class Button;

class GUIFactory {
public:
    static GUIFactory* getFactory(OSType type);
    virtual Button* createButton() = 0;
};

class OSXFactory : public GUIFactory {
public:
    virtual Button* createButton();
};

class WindowsFactory : public GUIFactory {
public:
    virtual Button* createButton();
};

class Button {
public:
    virtual void paint() = 0;
};

class OSXButton : public Button {
public:
    virtual void paint();
};

class WindowsButton : public Button {
public:
    virtual void paint();
};

#endif  // GUI_FACTORY_HPP_

// source: factory.cpp
// #include "factory.hpp"
#include <exception>

class NotImplementedException : public std::exception {};

GUIFactory* GUIFactory::getFactory(OSType type){
    switch (type) {
        case OS_Windows:
            return new WindowsFactory();
        case OS_OSX:
            return new OSXFactory();
        default:
            throw NotImplementedException();
    }
}

Button* OSXFactory::createButton() {
    return new OSXButton();
}

Button* WindowsFactory::createButton() {
    return new WindowsButton();
}

#include <iostream>

void OSXButton::paint() {
    std::cout << "OSX Button" << std::endl;
}

void WindowsButton::paint() {
    std::cout << "Windows Button" << std::endl;
}

// source: main.cpp
// #include "factory.hpp"
using namespace std;

int main(int ac, char *av[]) {
    GUIFactory * factory = GUIFactory::getFactory(OS_Windows);
    Button * button = factory->createButton();

    button->paint();

    delete button;
    delete factory;
}