Design Pattern DP -- Abstract Factory

Source: Internet
Author: User

Design Mode DP (3) Abstract Factory features: The system is independent of the creation, combination, and use of product classes to provide corresponding interfaces for external users, instead of implementing a class system, one of the products of multiple different systems is configured to consider the enhancement of the relevance between different product classes to the factory method (different factories create instances of different products) code: ## top-level abstract factory class, create Abstract public interface imo-actfactory {ICat createCat (); IDog createDog ();} # The following defines two specific animal factories (BlackAnimalFactory and WhiteAnimalFactory) public class BlackAnimalFactory implements imo-actfactory {public ICat createCat () {return new BlackCat ();} public IDog createDog () {return n Ew BlackDog () ;}} public class WhiteAnimalFactory implements imo-actfactory {public ICat createCat () {return new WhiteCat ();} public IDog createDog () {return new WhiteDog ();}} # define the product interface. Here is the cat and dog interface public interface ICat {void eat ();} public interface IDog {void eat ();} ### define the specific implementation of product class interfaces in different forms (black and white). Here is the Black Cat, Black Dog and white Cat, white dog public class BlackCat implements Cat {public eat () {System. out. println ("the black cat is eat Ting !! ") ;}} Public class BlackDog implements Dog {public eat () {System. out. println (" the black dog is eatting !! ") ;}} Public class WhiteCat implements Cat {public eat () {System. out. println (" the white cat is eatting !! ") ;}} Public class WhiteDog implements Dog {public eat () {System. out. println (" the white dog is eatting !! ") ;}} Call: public static void main (String arg []) {IFactory if1 = new BlackAnimalFactory (); ICat ic1 = if1.createCat (); ic1.eat (); IDog id1 = if1.createDog (); id1.eat (); IFactory if2 = new WhiteAnimalFactory (); ICat ic2 = if2.createCat (); ic2.eat (); IDog id2 = if2.createDog (); id2.eat ();} result: the black cat is eatting !! The black dog is eatting !! The white cat is eatting !! The white dog is eatting !!

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.