What is the difference between the factory model and the simple factory. Not much nonsense. we can clearly see the first example.
Advantage: the factory mode makes up for the violation of the open-closed principle in the simple factory mode, and maintains the advantages of the encapsulated object creation process.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading. tasks;
Namespace designmodel
{
Public interface Factory
{
JS createjs ();
}
Public class JS
{
Public int NUMA {Get; set ;}
Public int numb {Get; set ;}
Public Virtual int getresult ()
{
Return 0;
}
}
Public class Add1: JS
{
Public override int getresult ()
{
Return NUMA + numb;
}
}
Public class sub1: JS
{
Public override int getresult ()
{
Return NUMA-numb;
}
}
Public class addfactory: Factory
{
Public JS createjs ()
{
Return new Add1 ();
}
}
Public class subfactory: Factory
{
Public JS createjs ()
{
Return new sub1 ();
}
}
}
Client call:
Factory factory = new addfactory ();
JS = factory. createjs ();
JS. NUMA = 1;
JS. Numb = 2;
Console. writeline (JS. getresult ());
Factory F = new subfactory ();
Js j = f. createjs ();
J. NUMA = 9;
J. Numb = 0;
Console. writeline (J. getresult ());
Console. Readline ();