Model-engineering implementation and expansion (Design Mode C #) Factory method-self-test reference answer

Source: Internet
Author: User
Transferred from: Model-engineering implementation and expansion (Design Mode C)

Http://www.cnblogs.com/callwangxiang/

 

 

Modify the one completed at the end of this chapter that is suitable for engineering applications.Factory typeTo support constructors with Parameters

 

Analysis:

1. In line with the OCP principle, we do not modify the original factory <t>, but extend it through inheritance.

2. continue to use the activator provided by. NET Framework to provide support for constructors with parameters.

3. to distinguish it from the method signature of the original factory <t>, We must explicitly declareObject []Is used as the constructor parameter.

 

Reference answer

 

1. To avoid ambiguity, first modify the referenced namespace

Using System;
Using   F = Marvellousworks. practicalpattern. factorymethod;
Using Microsoft. visualstudio. testtools. unittesting;

2. define new factory interfaces and specific factory types

Interface Ifacloud: F. ifacloud
{
# Region Factory method
Ttarget create < Ttarget > ( Object [] Parametes );
Ttarget create < Ttarget > ( String Name, Object [] Parametes );
# Endregion
}

Class Factory: F. Factory, ifacloud
{
Public Ttarget create < Ttarget > ( Object [] Parametes)

{
Return(Ttarget) activator. createinstance (registry [Typeof(Ttarget)], parametes );
}

Public ttarget create ttarget > ( string name, Object [] parametes)
{< br> return (ttarget) activator. createinstance (registry [ typeof (ttarget), name], parametes);
}< BR >}

 

3. Write unit test verification

[Testmethod]
Public   Void Createinstance ()
{
VaR Factory =   New Factory ()
. Registertype < Ifruit, Apple > ()
. Registertype < Ifruit, orange > ( " O " )
. Registertype < Ivehicle, bicycle > ()
. Registertype < Ivehicle, bicycle > ( " A " )
. Registertype < Ivehicle, train > ( " B " )
. Registertype < Ivehicle, car > ( " C " )
. Registertype < Ientry, entrywithname > ()
. Registertype < Ientry, entrywithname > ( " N " )
. Registertype < Ientry, entrywithnameandageandtitle > ( " Nat " );

# RegionNo parameter type for the constructor

Assert. isinstanceoftype (factory. Create<Ifruit>(),Typeof(Apple ));
Assert. isinstanceoftype (factory. Create<Ifruit>("O"),Typeof(Orange ));

Assert. isinstanceoftype (factory. Create < Ivehicle > (), Typeof (Bicycle ));
Assert. isinstanceoftype (factory. Create < Ivehicle > ( " A " ), Typeof (Bicycle ));
Assert. isinstanceoftype (factory. Create < Ivehicle > ( " B " ), Typeof (Train ));
Assert. isinstanceoftype (factory. Create < Ivehicle > ( " C " ), Typeof (CAR ));

# Endregion

# RegionType of the constructor with Parameters

//Convert to the new extended interface form
VaR F=(Ifacloud) factory;

// Use extended features
VaR E1 = F. Create < Ientry > ( New   Object [] { " Joe " });
Assert. isinstanceoftype (E1, Typeof (Entrywithname ));
Assert. areequal < String > ( " Joe " , (Entrywithname) E1). Name );

VaR E2 = F. Create < Ientry > ( " Nat " , New   Object [] { " Joe " , 20 });
Assert. isinstanceoftype (E2, Typeof (Entrywithnameandageandtitle ));
Assert. areequal < String > ( " Joe " , (Entrywithname) E2). Name );
Assert. areequal < Int > ( 20 , (Entrywithnameandageandtitle) E2). Age );
Assert. areequal < String > (Entrywithnameandageandtitle. defaulttitle, (entrywithnameandageandtitle) E2). Title );

# Endregion
}

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.