The creation pattern of design pattern perception

Source: Internet
Author: User

Summary of the creation pattern:

The Earth (customer) needs a nature (product), the nature needs to have a lot of animals (product features), many plants (product features). Nature uses a combination of singleton mode and abstract Factory mode. Animals and plants are created using a simple factory method pattern. To create an animal, for example: In a simple factory encounter the same "person", Directly using the prototype pattern cloning, the law is created by the builder mode.

Public Animal

{

Private String mtype = null;

Public Animal Clone ()

{

...

}

Public String GetType ()

{

return mtype;

}

}

Public Humananimal extends Animal

{

public static Builder ()

{

Public Build Add (String part)

{

}

Public Humananimal Build ()

{

}

}

}

Public animalfactory

{

Private Animal mlastanimal = null;

Public Animal Create (String type)

{

if (Type.equals (Mlastanimal.gettype))

{

return Mlastanimal.clone ();

}

Else

{

Animal Temp=null;

if (type.eaual ("person"))

{

temp = new Humananimal.builder (). Add ("Hand"). Add ("header"). Add ("Foot"). Add ("torso"). Build ();

}

...//other animals slightly

Mlastanimal = temp;

return temp;

}

}

}

Private Class Nature

{

Private list<animal> manimals= new arraylist<animal> ();

Private list<plant> mplants = new arraylist<plant> ();

private static Nature minstance = null;

Private Nature ()

{

Manimals.add (animalfactory.create ("person"));

Manimals.add (animalfactory.create ("person"));

Manimals.add (Animalfactory.create ("Dog"));

Manimals.add (Animalfactory.create ("Grass carp"));

...

Mplants.add (Plantfactory.create ("Horn Flower"));

Mplants.add (Plantfactory.create ("Peach Tree"));

...

}

public static Nature getinstance ()

{

if (minstance = = null)

{

Minstance = new Nature ();

}

return minstance;

}

Public list<animal> getanimals ()

{

return manimals;

}

Public list<plant> getplants ()

{

return mplants;

}

}

The creation pattern of design pattern perception

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.