Design pattern is the essence of software development. Learning design patterns well is a required course for every developer. At present, there are many books on design patterns, among which there are better Chinese translations of gof, but they are not very suitable for beginners. Another is Java and pattern, which is suitable for beginners. It is strongly recommended here. However, the disadvantage of this book is that it is too cumbersome in some places. In many places, you only need to briefly describe it, but it is very expensive, which makes the book very thick and looks laborious. It is described in Java, which makes it difficult for some people who only know C. I am. net advocates, in order to read this book I read some Java books, I feel that Java is more diverse than the book. net is much better, and many books are of high quality. Maybe it is the reason why Java is mature now. For convenience. net fans learn the design mode. Here, I will take the learning notes from the book "Java and mode" and re-describe it in C #, hoping to help beginners. In fact, the design model is not a profound theory. I personally think it is not like what some people say: "I have never written 0.1 million. Code Do not talk about design patterns. "As long as you study and practice with your heart, you can fully master the design patterns. The simple factory mode is the class creation mode, also called the static factory method mode. A factory class determines which product class instance to create based on the input parameters. Generally, three roles are involved (for example ):
Factory : This role is at the core of the factory method model and contains the business logic closely related to the application. The factory class creates a product object under direct calls from the client. It is often implemented by a specific class.
Abstract Product role : The class that assumes this role is the parent class of the object created by the factory method mode, or the interfaces they share. It is generally implemented by an interface or abstract class.
Specific product roles : Any object created in the factory method mode is an instance of this role, implemented by a specific class.
Advantages and disadvantages of simple factory Mode : The core of the model is the factory class. This class is responsible for product creation, and the client can remove the responsibility for product creation, which separates responsibility. However, because the factory class integrates the creation logic of all products, if it fails to work properly, it will have a great impact on the system. To add a new product, you must modify the source code of the factory role.
The following describes the implementation of the pattern by using the gardener as an example: Fruit fruit interface, which specifies some common characteristics of fruit. The apple fruit class is derived from the fruit interface. The strawberry class is derived from the fruit interface. The fruitgardener gardener class is responsible for the creation of strawberries and apples. When the client wants to create a fruit (Apple or strawberry object), call the factory method of the gardener class to create it. The UML diagram is as follows: the code is as follows:
Fruit. CS Namespace simple_factory {public interface fruit {// growth void grow (); // harvest void harvest (); // planting void plant ();}}
Apple. CS Namespace simple_factory {public class Apple: fruit {public Apple () {}# region fruit member public void grow () {console. writeline ("Apple is growing ....... ");} public void harvest () {console. writeline ("Apple is harvesting ....... ");} public void plant () {console. writeline ("Apple is planting ....... ") ;}# endregion }}
strawberry. CS Namespace simple_factory {public class strawberry: fruit {public strawberry () {}# region fruit member public void grow () {console. writeline ("Strawberry is growing ....... ");} public void harvest () {console. writeline ("Strawberry is harvesting ....... ");} public void plant () {console. writeline ("Strawberry is planting ....... ") ;}# endregion }}
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 = fruitgardener. factory ("apple"); // creat Apple afruit. grow (); afruit. harvest (); afruit. plant (); afruit = fruitgardener. factory ("strawberry"); // creat strawberry afruit. grow (); afruit. harvest (); afruit. plant () ;}} output: Apple is growing ....... apple is harvesting ....... apple is planting ....... strawberry is growing ....... strawberry is harvesting ....... strawberry is planting .......
From: http://www.java-asp.net/aspnet/200509/t_19581.html