Abstract Factory Reading Notes
intention:
Configure an excuse for creating a related or interdependent object without specifying their specific classes.
Alias:
Kit
understanding:
Abstract Factory is a creation pattern that provides a valid method for our creation of objects. We don't have to directly New objects but can configure an interface for creating objects, which defines how to create objects. We also know that the object created by the abstract factory is a series or a family. The biggest feature of this mode is to hand over the specific creation of the task to his subclavab is the specific class, so we will create the target time delay to its subclass. We know that design mode is prepared for developers who have certain object-oriented developers in design mode (GOF). Therefore, we should all know the difference between the class and types. In the current programming language, the interface is the most abstract data structure, so we will define the AbstractFactoy in our abstract factory into an interface is also very natural. What does such a factory can create a family product? That is to say, this type of product has the same parent class or a parent interface. Here I don't want to repeat the goflings on collaboration and results between these objects, etc., which can be seen in "Design Software" this book. Below I want to implement this mode with C # for your reference.
structure:
Through our object-oriented knowledge We know that a parent class can identify an object of a subclass, which is also a key to understanding it. We will use an abstract class object to represent a subclass of objects. As shown in the figure above, we now have an interface of IABSTRACTFACTOY. The responsibility of the interface is to create the work of the object, and we have two specific factories ConcreteFactory1 and ConcreteFactory2 They are the specific implementation of the functions in the interface. They implemented these two A method, of course, there may be no two specific factories in a specific application. In the book of GOF, in many cases in many cases, we have no factory abstraction interface, most of which are direct use of specific factories. Here I want to force the structure in the full description so that it is true. Ok, the following is my code, this code shows how we implemented AbstractFactory in C #. I use a WinForm to test the structure.
Implement code:
Using system;
Namespace AbstractFactory_me
{
Public interface abstractFactory {
IABSTRACTPRODUCTA CREATEPRODUCTA ();
IABSTRACTPRODUCTB CREATEPRODUCTB ();
}
Public interface abstractproducta {
String showself ();
String Showother (IABSTRACTPRODUCTB B);
}
Public Class Producta1: IabStractProducta {
Public productA1 () {}
Public string showself () {
Return this.tostring ();
}
Public String Showother (IABSTRACTPRODUCTB B) {
Return b.toString ();
}
}
Public Class Producta2: ibstractProducta {
Public productA2 () {}
Public string showself () {
Return this.tostring ();
}
Public String Showother (IABSTRACTPRODUCTB B) {
Return b.toString ();
}
}
Public interface abstractproductb {string showself ();
String Showother (IABSTRACTPRODUCTA A);
}
Public Class ProductB1: IABSTRACTPRODUCTB {
Public string showself () {
Return this.tostring ();
}
Public String Showother (IABSTRACTPRODUCTA A) {
Return a.tostring ();
}
}
Public Class ProductB2: IABSTRACTPRODUCTB {
Public string showself () {
Return this.tostring ();
}
Public String Showother (IABSTRACTPRODUCTA A) {
Return a.tostring ();
}
}
Public Class Concretefactory1: IABSTRACTFAACTORY {
Public IabstractProducta CreateProducta () {
Return new producta1 ();
}
Public abstractProductB CreateProductB () {
Return new productb1 ();
}
}
PUBLIC CLASS ConcreteFactory2: ibutstractfactory {
Public IabstractProducta CreateProducta () {
Return new producta2 ();
}
Public abstractProductB CreateProductB () {
Return new productb2 ();
}
}
Public class client {
Public void run () {
IABSTRACTFACTORY FACTORY1 = New ConcreteFactory1 ();
IABSTRACTPRODUCTA A = Factory1.createProducta ();
a.showself ();
IABSTRACTPRODUCTB B = Factory1.createProductB ();
B.Showself ();
B.Showother (a);
}
}
}
We put a RichtextBox1 instance in the WinForm that was tested, and he used to display the structure.
Private Void Form1_Load (Object Sender, System.Eventargs E) {
this.richtextbox1.clear ();
IABSTRACTFACTORY FACTORY1 = New ConcreteFactory1 ();
IABSTRACTPRODUCTA A = Factory1.createProducta ();
IABSTRACTPRODUCTB B = Factory1.createProductB ();
this.RichtextBox1.AppendText (a.showself () "/ n");
this.RichtextBox1.AppendText (B.Showself () "/ n");
THIS.RICHTextBox1.AppendText (B.Showother (a) "/ n");
This.RichtextBox1.AppendText (a.showother (b) "/ n / n / n");
THIS.RICHTextBox1.AppendText (A.gettype (). TOSTRING () "/ n");
THIS.RICHTextBox1.AppendText (B.gettype (). TOSTRING () "/ n");} For the clear instructions, we use Showseelf and Showother's methods in the generated object to display yourself and another object. C # implementation in the book: The example in the GOF book is implemented with C # implementation: use system;
Namespace AbstractFactory_maze {
USING MAZE;
Public interface abstractFactory {
Mazeclass makemaze ();
Wall makewall ();
Room Makeroom (INT N);
Door Makedoor (room liveoom, room otherroom);
}
PUBLIC CLASS MAZEFACTORY: AbstractFactory {
Public mazeclass makemaze () {
Return new mazeclass ();
}
Public wall made makewall () {
Return New Wall ();
}
Public room makeroom (int N) {
Return New Room (n);
}
Public Door Makedoor (Room OnRoom, Room Otherroom) {
Return New Door (OneRoom, Otherroom);
}
}
// this is aclient
Public class mazegame {
Public MazeClass Mazecreate (AbstractFactory Factory) {
MazeClass Amaze = Factory.makemaze ();
Room r1 = factory.makeroom (1);
Room r2 = factory.makeroom (2);
Door adoor = factory.makedoor (R1, R2);
Amaze.Addroom (R1);
Amaze.addroom (r2);
R1.setside (Direction.North, Factory.makewall ());
R1.setside (Direction.east, Adoor);
R1.setside (Direction.South, Factory.makewall ());
R1.setside (Direction.west, Factory.makewall ());
R2.setside (Direction.North, Factory.makewall ());
R2.setside (Direction.east, Factory.makewall ());
R2.setside (Direction.South, Factory.makewall ());
R2.setside (Direction.West, Adoor);
Return Amaze;
}
}
}
Namespace maze {
Using system.collections;
Public class mapsite {
Public Virtual Void Enter () {}
}
Public Enum Direction {North, South, EAST, West}
Public class room: Mapsite {
Public String Print () {
String result = "";
For (int i = 0; i <= 3; i ) {
Switch (i) {
Case (int) Direction.east: {
Result = "EAST IS:" this.getside (direction.east) "Break;
}
Case (int) Direction.North: {
Result = "north is:" this.getside (direction.north) "/ t";
Break;
}
Case (int) Direction.South: {
Result = "South IS:" this.getside (direction.Sout) "/ t";
Break;
}
Case (int) Direction.west: {
Result = "West is:" this.getside (direction.west) "/ t";
Break;
}
}
}
Return Result;
}
Public Room (int N) {
THIS.M_ROOMNUMBER = N;
}
Public mapsite getside (direction dir) {
Return this.m_sides [(int) DIR];
}
Public void setside (direction dir, mapsite maps) {
THIS.M_SIDES [(int) DIR] = Mapsite;
}
Public override void enter () {}
Private mapsite [] m_sides = new mapsite [4];
Int m_roomnumber;
}
Public class wall: Mapsite {
Public wall () {}
Public override void enter () {}
}
PUBLIC CLASS DOOR: MAPSITE {
Public Door (room inroom, room odiot) {}
Public override void enter () {}
Public room {
Get {return this.m_oneroom;
Set {this.m_oneroom = value;
}
PRIVATE ROOM M_ONEROOM;
Public room Otherroom {
Get {return this.m_otherroom;}
Set {this.m_otherroom = value;
}
PRIVATE ROOM M_OTHERROOM;
PRIVATE BOOL M_ISOPEN;
Public bool isopen {
Get {return this.m_isopen;
Set {this.m_isopen = value;
}
}
Public class mazeclass {
Public mazeclass () {}
Public String Print () {
String result = "";
For (int i = 0; i <= this.m_maze.count-1; i ) {
Result = this.Roomnumber (i) .print () "/ n";
}
Return Result;
}
Public void addroom (room room) {
M_maze.add (room);
}
Public room roomnumber (int roomnumber) {
Return (Room) this.m_maze [roomnumber];
PRIVATE ARRAYLIST M_MAZE = New ArrayList ();
}
}
Private Void Form1_Load (Object Sender, System.Eventargs E) {
AbstractFactory Factory = new mazefactory ();
Mazegame Game = new mazegame ();
MazeClass Amaze = Game.mazecreate (Factory);
This.RichtextBox1.AppendText (Amaze.print ());
}
The following is the output result:
North Is: Maze.wall South Is: Maze.Wall East Is: Maze.door West Is: Maze.wall
North Is: Maze.wall South IS: Maze.wall East Is: Maze.wall West Is: Maze.door
I hope this article can help people who are about to learn or have doubts, because the author's ability is limited, if you are not right, please indicate, or contact me (wu_jian830@hotmail.com) Thank you!