Design Pattern Abstract Factory

advertisement
Design Pattern Abstract Factory
Step 0.
Create a new console C# project.
Step 1.
Below the class Main, let’s declare the new class which will be the abstract
factory. Let’s name it AbstractFactory, as a pattern.
Step 2.
Declare two abstract methods for creating monsters and guns for them.
Here we create abstract methods, which will be used by concrete fabrics while
crating the concrete products.
Method CreateMonster will interact with the method CreateGun, because each
monster must have a gun. Moreover, there are two types of monsters:
SuperMonster and WeakMonster. Each of them has a special type of guns:
SuperMonsters fight with grenade launcher, WeakMonsters fight with pistol.
At first, these methods will be underlined by the red line, because there are no such
abstract classes AbstractGun and AbstractMonster which we wrote as types of the
results of these methods.
Step 3.
So, we must declare two abstract classes AbstractGun and AbstractMonster. Write
them below the abstract factory class.
You see now, that methods CreateGun() and CreateMonster() in abstract factory
class are not underlined now.
If you run project now, nothing will be shown, and, indeed, nothing will happen.
But, there mustn’t be errors, check your project.
Step 4.
Write in AbstractMonster class implementation the declaration of the method
Interact with argument of AbstractGun type.
So, now any monster created by any factory will have an interaction with a gun.
Step 5.
Now we can code implementation of concrete classes of two types of guns (pistol
and grenade) and two types of monsters (weak monster and super monster) in the
system.
Note, that these classes are the inheritors of the relative abstract classes.
Step 6.
In order to show that Weak monster has exactly pistol and Super monster has
exactly grenade, let’s print on console their names and guns. To do that we must
override method Interact inherited from the class AbstractMonster.
Step 7.
We remember, that we must have two factories: one – for creating weak monsters
with pistol, another – for creating super monsters with grenade. So, we must write
two concrete factories, which will be used in the main application. The concrete
factory ConcreteFactoryWeakLevel differs from the abstract factory, so that it has
overridden methods CreateGun and CreateMonster, which calls the constructor of
the relative classes Pistol and WeakMonster.
Step 8.
We do the same with concrete factory for hard level: it has overridden methods
CreateGun and CreateMonster, which calls the constructor of the relative classes
Grenade and SuperMonster.
Step 9.
Now we can create a mediator between the abstract factory and the main
application, so that the main application won’t even know how monsters are
created. To do that, let’s add a new class Client. Class Client will have one
attribute – factory. The main application will choose which type of monsters does
it need at any moment, and will send the request to the client to return the
necessary monsters together with their guns. Also, Client will create the private
objects ClientAbstractGun and ClientAbstractMonster. These private objects are
valued in the constructor of the client by specific methods CreateGun and
CreateMonster. The last will return either Pistol and Weak monster or Grenade and
Super monster through the concrete factories.
We put a method Run into the Client class, which will call Interact from the
relative monster.
The class Client helps as to instantiate the main application from the
implementation of concrete products creation. At the same time, it gives us the
ability to use the factory many times. If we need to change something in behavior
or structure of those objects, it will be implemented only inside the factory, the
main application will still just calling the creation of the needed objects.
Step 10.
Now the programmer can create two variables factory1 and factory2 in the main
void. They will be the objects of the weak level and the hard level. These objects
(concrete factories will be given as parameters to the class Client, which will return
instances of the relative type as it was described at the previous step,
Step 11.
Run the project.
You see that two monsters are created; each of them has a relative gun.
In main application, we just call the concrete factories for different levels of a
game.
The pattern is implemented correctly.
Download