Pattern Design (2: Abstract Factory Continued)

zhaozj2021-02-11  181

Through the above explanation we can probably understand the concept of Abstract Factory. Let's explain her characteristics in depth. We specifically discuss in the following ways: intention, motivation, applicability, structure, participants, collaboration, effect, implementation

意图: [Provide an interface to create a series of related or interdependent objects without specifying their specific classes] (from "Design Patterns: Elements of Reusable Object-Oriented Software")

Motivation: Consider we produce a car, and she consists of a series of basic products, if we have to produce two models such as: Santana and Passat, in order to ensure portability, we should not hardly encode the program. We define the categories as follows: Carfactory, STNCarfactory, three classes of PstCarfactory. Where carfactory is an abstract class. CARFACTORY implements the actual class interface.

Figure:

Applicability: Abstract Factory is applicable as follows

1. A system is independent of its product creation, combination, and representation.

2. A system is configured by one of multiple product lines. (Such as two kinds of cars)

3. When you want to emphasize the design of a series of related product objects, when you use it. (A variety of components constitute a car)

4. Provide a class library, just want to display the interface

structure:

cooperation:

1. Create a ConmertFactory class instance when running, create products (such as Santana), creating

Users with different classes (StBFactory | PstFactory);

2. Abstractory Factory will delay the product object to the subclass.

effect:

advantage:

1. Separate specific classes: such as: Customers are not directly dealing with products, through abstract classes.

2. Easy to exchange products: If you can replace the product type (Santana | Passat).

3. It is conducive to the consistency of the product: If you produce Passat, we can only use her proprietary parts.

Disadvantages:

`It is difficult to support new types of products: such as: If you produce new products change all the subclasses involved.

achieve:

Class carfactory {

Carfactory () {}

Body makebody () {return new body ();

ENGINEE MAKEENGINEEE () {Return New Enginee ();

}

Class Stnfactory Extends Carfactory {

STNFActory ()}

Body makebody () {return new stnbody ();

ENGINEE MAKEENGINEEE () {Return New Stnenginee ();

}

Class body {

Body () {system.out.println ("Body Concert");

}

Class Enginee {

Enginee () {system.out.println ("Enginee Concert");

}

Class Stnbody Extends Body {

STNBody () {system.out.println ("stnbody concert");}

}

Class Stnenginee Extends Enginee {

Stnenginee () {system.out.println ("stnenginee concert");}}

Class product {

Product () {system.out.println ("Product Concert");

}

Class stnproduct {

STNPRODUCT () {}

Product ProductCreate (STNFactory FAC)

{body dy = fac.makebody ();

Enginee En = fac.makenginee ();

Return new product ();

}

}

Class test {

Public static void main (string [] args)

{STNPRODUCT STNP = New STNPRODUCT ();

STNFACTORY STNF = New STNFActory ();

STNP.PRODUCTCREATE (STNF);

}

}

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

New Post(0)