Design mode C # Description - Simple Factory Model

zhaozj2021-02-16  41

Foreword: The design model is one of the essence of the field of software development. Learning a well-design model is a compulsory course for every developer. There are many books on design patterns, which is better to have the Chinese translation of GOF, but it is not suitable for beginners. Another is "Java and Mode", which is suitable for beginners, which is highly recommended here. But the deficiencies of this book are too cumbersome, and many places must only be briefly explained, but large pens, make the books thick, look labor. And it is described in Java, which makes someone who only understands C # will not start. I am a .NET supporter, in order to see this book I deliberately read some Java books, I feel that Java is much better than .NET is much better than .NET, and many books are high. Maybe now Java has been mature why. In order to facilitate the design model of the .NET fan learning, I learned that I learned "Java and Mode", and re-describe it with C # language, I hope to help beginners.

In fact, the design model is not a deep theory. Personally think that it is not like some people say "did not write 100,000 code, don't talk about design mode", as long as learning and practice is fully capable.

Simple factory model is a class creation mode and is called static factory method mode. It is an example of which product class is created by a factory class based on incoming parameters. It is generally related to three roles (as shown below):

Factory Class: The role is the core of the factory method model, which contains business logic related to the application. The factory class creates a product object under the direct call of the client, which is often implemented by a specific class.

Abstract Product Role: The class that serves this role is the parent class of the object created by the factory method mode, or the interface they share. Generally implemented by an interface or abstract class.

Specific product role: any pair created by factory method mode

The like is all instances of this role, implemented by specific classes.

Simple factory model advantages and disadvantages:

The core of the mode is factory class, which is responsible for the creation of the product, and the client can exempt the responsibility of the product creation, which achieves the split of responsibility. However, because the factory class has concentrated on all product creation logic, if it does not work properly, it will have a big impact on the system. If you add new products, you must modify the source code of the factory role.

Taking the garden-planted fruits to discuss the specific implementation of this model:

Fruit Fruit Interface, Some common features of fruits

Apple Apple derived from Fruit interface

Strawberry Strawberry Class Delivery from Fruit Interface

Fruitgardener gardener is responsible for strawberry and apple creation.

When the Client wants to create a fruit (Apple or Strawberry Object), call the gardener Factory method to create: UML map is as follows:

code show as below:

FRUIT.CS

Namespace Simple_Factory

{

Public Interface Fruit, INTERFAIT

{

// Growth

Void growth ();

//reward

Void Harvest ();

// plant

Void Plant ();

}

}

Apple.cs

Namespace Simple_Factory

{

Public Class Apple: Fruit

{

Public apple ()

{

}

#Region Fruit member

Public void greow ()

{

Console.writeline ("Apple Is Growing ...";

}

Public void harvest ()

{

Console.WriteLine ("Apple Is Harvesting .......");

}

Public void plant ()

{

Console.writeLine ("Apple Is Planting ...";

#ndregion

}

}

Strawberry.cs

Namespace Simple_Factory

{

Public Class Strawberry: fruit

{

Public strawberry ()

{

}

#Region Fruit member

Public void greow ()

{

Console.writeline ("strawberry is growing ...");

}

Public void harvest ()

{

Console.writeline ("strawberry is harvesting ...");

}

Public void plant ()

{

Console.writeline ("strawberry is planting ...");

}

#ndregion

}

}

Fruitgardener.cs

Namespace Simple_Factory

{

Public Class Fruitgardener

{

// static factory method

Public Static Fruit Factory (String Which)

{

IF (Which.equals ("apple"))

{

Return new apple ();

}

Else IF (Which.equals ("strawberry"))

{

Return new strawberry ();

}

Else

{

Return NULL;

}

}

}

}

Client.cs

Using system;

Namespace Simple_Factory

{

Class Client

{

[Stathread]

Static void main (string [] args)

{

Fruit AFRUIT = Fruitgardner.Factory ("apple"); // Creat Apple

Afruit.grow ();

Afruit.harvest ();

Afruit.plant ();

Afruit = fruitgardner.Factory ("strawberry"); // Creat Strawberry

Afruit.grow ();

Afruit.harvest ();

Afruit.plant ();

}

}

}

The output is as follows:

Apple is groing .......

Apple is harvesting .......

Apple is planting .......

Strawberry is groing .......

Strawberry is harvesting .......

Strawberry is planting .......

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

New Post(0)