|
1 | | -Notes: |
2 | | - |
3 | | -Pros |
4 | | -1. You can be sure that the products you’re getting from a factory are compatible with each other. |
5 | | -2. avoid coupling between products and client code (calling "new" everywhere). |
6 | | -3. Single Responsibility Principle. Products are created in their own factories. |
7 | | -4. Open/Closed Principle. You can introduce new variants of products without breaking existing client code. |
8 | | - |
9 | | -Cons |
10 | | -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. |
11 | | - |
12 | 1 | When to use it: |
13 | | -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." |
14 | | - |
15 | | -### Structure |
16 | | - |
17 | | -1. [Abstract Products](https://github.com/ycarowr/DesignPatterns/blob/master/Assets/Creational/AbstractFactory/UiSystemPlatformsFactory/Scripts/UiSystemPlatformsFactoryUsage.Products.cs): these are interfaces for a set of related products, or a product family. In this case, UI components. |
18 | | -``` |
19 | | - // Product A |
20 | | - public interface IInput |
21 | | - { |
22 | | - KeyCode Jump { get; } |
23 | | - KeyCode Attack { get; } |
24 | | - KeyCode Block { get; } |
25 | | - KeyCode PowerUp { get; } |
26 | | - } |
27 | | -
|
28 | | - // Product B |
29 | | - public interface IWindow |
30 | | - { |
31 | | - string Name { get; } |
32 | | - } |
33 | | -
|
34 | | - // Product C |
35 | | - public interface IButton |
36 | | - { |
37 | | - string Name { get; } |
38 | | - } |
39 | | -``` |
40 | | -2. [Concrete Products](https://github.com/ycarowr/DesignPatterns/blob/master/Assets/Creational/AbstractFactory/UiSystemPlatformsFactory/Scripts/UiSystemPlatformsFactoryUsage.ConcreteProducts.cs): are various implementations of abstract products, grouped by variants. |
41 | | -``` |
42 | | - //Concrete Product A1 |
43 | | - public class XboxInput : IInput |
44 | | - { |
45 | | - public KeyCode Jump => KeyCode.JoystickButton0; |
46 | | - public KeyCode Attack => KeyCode.JoystickButton1; |
47 | | - public KeyCode Block => KeyCode.JoystickButton2; |
48 | | - public KeyCode PowerUp => KeyCode.JoystickButton3; |
49 | | - } |
50 | | - |
51 | | - //Concrete Product A2 |
52 | | - public class Ps4Input : IInput |
53 | | - { |
54 | | - public KeyCode Jump => KeyCode.JoystickButton3; |
55 | | - public KeyCode Attack => KeyCode.JoystickButton2; |
56 | | - public KeyCode Block => KeyCode.JoystickButton1; |
57 | | - public KeyCode PowerUp => KeyCode.JoystickButton0; |
58 | | - } |
59 | | - |
60 | | - // ... more itens |
61 | | -``` |
62 | | - |
63 | | -3. [Abstract Factory](https://github.com/ycarowr/DesignPatterns/blob/master/Assets/Creational/AbstractFactory/UiSystemPlatformsFactory/Scripts/UiSystemPlatformsFactoryUsage.UiFactorySystem.cs): is an interface which declares methodsto create each of the abstracted products. |
64 | | -``` |
65 | | - public interface IUiFactorySystem |
66 | | - { |
67 | | - IInput CreateInputXbox(); |
68 | | - IButton CreateButtonXbox(); |
69 | | - IWindow CreateWindowXbox(); |
70 | | -
|
71 | | - IInput CreateInputPs4(); |
72 | | - IButton CreateButtonPs4(); |
73 | | - IWindow CreateWindowPs4(); |
74 | | - } |
75 | | -``` |
76 | | -4. [Concrete Factories](https://github.com/ycarowr/DesignPatterns/blob/master/Assets/Creational/AbstractFactory/UiSystemPlatformsFactory/Scripts/UiSystemPlatformsFactoryUsage.Factories.cs): implement the creation methods of the abstract factory using the Factory Method Pattern. |
77 | | -``` |
78 | | - //Base concrete factory |
79 | | - public class Ps4XboxBaseFactory<TPs4, TXbox, TBase> : IPs4XboxBaseFactory<TPs4, TXbox, TBase> |
80 | | - where TPs4 : TBase, new() |
81 | | - where TXbox : TBase, new() |
82 | | - { |
83 | | - public TPs4 CreatePs4() |
84 | | - { |
85 | | - return new TPs4(); |
86 | | - } |
87 | | -
|
88 | | - public TXbox CreateXbox() |
89 | | - { |
90 | | - return new TXbox(); |
91 | | - } |
92 | | -
|
93 | | - public TBase CreateSystem(RuntimePlatform platform) |
94 | | - { |
95 | | - if (platform != RuntimePlatform.PS4 && platform != RuntimePlatform.XboxOne) |
96 | | - Debug.LogError("Platform not supported: " + platform); |
97 | | -
|
98 | | - if (platform == RuntimePlatform.PS4) |
99 | | - return CreatePs4(); |
100 | | -
|
101 | | - return CreateXbox(); |
102 | | - } |
103 | | - } |
104 | | -
|
105 | | - //Create a factory for the Input System |
106 | | - public class InputSystemFactory : Ps4XboxBaseFactory<Ps4Input, XboxInput, IInput> |
107 | | - { |
108 | | - } |
109 | | -
|
110 | | - //Create a factory for the Window System |
111 | | - public class WindowSystemFactory : Ps4XboxBaseFactory<Ps4Window, XboxWindow, IWindow> |
112 | | - { |
113 | | - } |
114 | | -
|
115 | | - //Create a factory for the Button System |
116 | | - public class ButtonSystemFactory : Ps4XboxBaseFactory<Ps4Button, XboxButton, IButton> |
117 | | - { |
118 | | - } |
119 | | -``` |
120 | | - |
121 | | -5. [Concrete Abstract Factory]``` |
| 2 | +1. When you have a family of algorithms that do the same thing, but in a different way. |
| 3 | +2. When you want decouple the client usage from the actual implementation. |
| 4 | +3. Is a good alternative for a inheritance because it brings more flexibility horizontally in the Inheritage Tree. |
| 5 | +4. Capture the abstraction into an interface, and the implementation into concrete classes. |
122 | 6 |
|
123 | 7 | References: |
124 | | -1. Youtube [Derek Banas](https://www.youtube.com/watch?v=xbjAsdAK4xQ&list=PLF206E906175C7E07&index=6) |
125 | | -2. Youtube [Christopher Okhravi](https://www.youtube.com/watch?v=v-GiuMmsXj4&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc&index=5) |
126 | | -3. Github [Qian Mo](https://github.com/QianMo/Unity-Design-Pattern/tree/master/Assets/Creational%20Patterns/Abstract%20Factory%20Pattern) |
127 | | -4. Article [Source Making](https://sourcemaking.com/design_patterns/abstract_factory) |
128 | | -5. Article [Guru](https://refactoring.guru/design-patterns/abstract-factory) |
129 | | - |
| 8 | +1. Youtube [Christopher Okhravi](https://www.youtube.com/watch?v=v9ejT8FO-7I&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc&index=1) |
| 9 | +2. Github [Qian Mo](https://github.com/QianMo/Unity-Design-Pattern/tree/master/Assets/Behavioral%20Patterns/Strategy%20Pattern) |
| 10 | +3. Article [Source Making](https://sourcemaking.com/design_patterns/strategy) |
| 11 | +4. Article [Guru](https://refactoring.guru/design-patterns/strategy) |
130 | 12 |
|
131 | | -Agorithms From: |
| 13 | +Sorting Agorithms in the example are taken from: |
132 | 14 | - https://programm.top/en/c-sharp/algorithm/array-sort/ |
133 | | -- http://anh.cs.luc.edu/170/notes/CSharpHtml/sorting.html |
| 15 | +- http://anh.cs.luc.edu/170/notes/CSharpHtml/sorting.html |
0 commit comments