Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Readme.md

Abstract Factory

Definitions

"It's a creational design pattern that lets you produce families of related objects without specifying their concrete classes."

"A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products""

Notes:

Pros

  1. You can be sure that the products you’re getting from a factory are compatible with each other.
  2. avoid coupling between products and client code (calling "new" everywhere).
  3. Single Responsibility Principle. Products are created in their own factories.
  4. Open/Closed Principle. You can introduce new variants of products without breaking existing client code.

Cons

  1. The code may become more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.

When to use it:

  1. "Use the Abstract Factory when your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products—they might be unknown beforehand or you simply want to allow for future extensibility."

Structure

  1. Abstract Products: these are interfaces for a set of related products, or a product family. In this case, UI components.
        // Product A
        public interface IInput
        {
            KeyCode Jump { get; }
            KeyCode Attack { get; }
            KeyCode Block { get; }
            KeyCode PowerUp { get; }
        }

        // Product B
        public interface IWindow
        {
            string Name { get; }
        }

        // Product C
        public interface IButton
        {
            string Name { get; }
        }
  1. Concrete Products: are various implementations of abstract products, grouped by variants.
        //Concrete Product A1
        public class XboxInput : IInput
        {
            public KeyCode Jump => KeyCode.JoystickButton0;
            public KeyCode Attack => KeyCode.JoystickButton1;
            public KeyCode Block => KeyCode.JoystickButton2;
            public KeyCode PowerUp => KeyCode.JoystickButton3;
        }
        
        //Concrete Product A2
        public class Ps4Input : IInput
        {
            public KeyCode Jump => KeyCode.JoystickButton3;
            public KeyCode Attack => KeyCode.JoystickButton2;
            public KeyCode Block => KeyCode.JoystickButton1;
            public KeyCode PowerUp => KeyCode.JoystickButton0;
        }
        
        // ... more itens
  1. Abstract Factory: is an interface which declares methodsto create each of the abstracted products.
        public interface IUiFactorySystem
        {
            IInput CreateInputXbox();
            IButton CreateButtonXbox();
            IWindow CreateWindowXbox();

            IInput CreateInputPs4();
            IButton CreateButtonPs4();
            IWindow CreateWindowPs4();
        }
  1. Concrete Factories: implement the creation methods of the abstract factory using the Factory Method Pattern.
        //Base concrete factory
        public class Ps4XboxBaseFactory<TPs4, TXbox, TBase> : IPs4XboxBaseFactory<TPs4, TXbox, TBase>
            where TPs4 : TBase, new()
            where TXbox : TBase, new()
        {
            public TPs4 CreatePs4()
            {
                return new TPs4();
            }

            public TXbox CreateXbox()
            {
                return new TXbox();
            }

            public TBase CreateSystem(RuntimePlatform platform)
            {
                if (platform != RuntimePlatform.PS4 && platform != RuntimePlatform.XboxOne)
                    Debug.LogError("Platform not supported: " + platform);

                if (platform == RuntimePlatform.PS4)
                    return CreatePs4();

                return CreateXbox();
            }
        }

        //Create a factory for the Input System
        public class InputSystemFactory : Ps4XboxBaseFactory<Ps4Input, XboxInput, IInput>
        {
        }

        //Create a factory for the Window System
        public class WindowSystemFactory : Ps4XboxBaseFactory<Ps4Window, XboxWindow, IWindow>
        {
        }

        //Create a factory for the Button System
        public class ButtonSystemFactory : Ps4XboxBaseFactory<Ps4Button, XboxButton, IButton>
        {
        }
  1. Concrete Abstract Factory: Factory that brings all the elements together.
        public class UiFactorySystem : IUiFactorySystem
        {
            readonly ButtonSystemFactory buttonFactory = new ButtonSystemFactory();
            readonly InputSystemFactory inputFactory = new InputSystemFactory();
            readonly WindowSystemFactory windowFactory = new WindowSystemFactory();
            
            //... implementation of the methods
        }

References:

  1. Youtube Derek Banas
  2. Youtube Christopher Okhravi
  3. Github Qian Mo
  4. Article Source Making
  5. Article Guru