Design mode C # implementation (1) - AbstractFactory

zhaozj2021-02-16  53

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. To view the C # implementation of this example in the book, please click here: http://www.9cbs.net/develop/read_article.asp?id=20949 I hope this article can help you will learn or have doubtful people Since the author's ability is limited, if you are not right, please point it, or contact me (wu_jian830@hotmail.com) Thank you!

转载请注明原文地址:https://www.9cbs.com/read-23930.html

New Post(0)