Design mode C # description - factory method mode Mutou23 [original]

xiaoxiao2021-03-06  181

? Design mode C # description - factory method mode

Factory method mode is a class creation mode, called virtual construct sub-model or polymorphic factory model. Its intention is to define a factory interface that creates a product object, delays the actual creation work into the subclass. Disadvantages of simple factory model:

Because the factory class has a logic of all products, if it is not possible to work, it will have a big impact on the system. If you add new products, you must modify the source code of the factory role. The advantages of factory method model:

In the factory method mode, the core factory class is no longer responsible for all products creation, but will give the created work to the subclass. This core class is a role of an abstract factory, only responsible for giving interfaces that must be implemented by specific factory subclasses without contacting which product class instantiated details. This allows the factory method model to allow the system to introduce new products without modifying the specific plant role, making it superiority of simple factory models.

?

The specific implementation of this mode is discussed below:

Abstract Factory Role (CREATOR): Started with the core of factory method model, which provides an interface that creating an object's factory class. ConcentecReetecreator: Responsible for the creation of specific product objects to implement the interface specified by Creator. Abstract Product Role (Product): The super-type of the object to be created in the factory method mode, which specifies the interface that the product should have. ConcreteProduct: Realize the interface specified by Product. The sample code is as follows: CREATOR: PUBLIC Interface Creator

???? {

??????????Product factory (); // factory method

????}

? Concretecreator1:

Class Concretecreator1: CREATOR

???? {

???????? public product factory () // factory method

???????? {

????????????? Return New ConcreteProduct1 ();

????????}

????}

? Concretecreator2:

???? Class Concretecreator2: CREATOR

???? {

???????? public product factory () // factory method

???????? {

????????????? Return New ConcreteProduct2 ();

????????}

????}

? Products: PRODUCT:

Public Interface Product

???? {

????????

????}

? ConcreteProduct1:

Class ConcreteProduct1: Product

???? {

???????? public concreteproduct1 ()

???????? {

????????????? console.writeline ("Creat ConcreteProduct1");

????????}

??????????

????}

? ConcreteProduct2:

???? Class ConcreteProduct2: Product

???? {

???????? public concreteproduct2 ()

???????? {

????????????? console.writeline ("Creat ConcreteProduct2");

????????}

??????????

????}

? Client:

Class Client

???? {

???????? private static creator creator1, creator2; ???????? private statino

???????? [statHread]

???????? static void

Main

(String [] ARGS)

???????? {

????????????? creator1 = new concretecreator1 ();

??????????????product1 = CREATOR1.FACTORY ();

????????????? creator2 = new concretecreator2 ();

????????????? product2 = CREATOR2.FACTORY ();

????????}

????}

?

Author Blog:

http://blog.9cbs.neet/mutou23/

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

New Post(0)