What is abstract factory, learn again.
1 Abstract Factory2 Overview3 provides an interface to create a series of related or interdependent objects without specifying their specific classes. 4 5 Applicability61a system is independent of the creation, composition, and presentation of its products. 7 82. When a system is to be configured by one of several product families. 9 Ten3when you want to emphasize the design of a series of related product objects for joint use. One A4when you provide a Product class library, and you want to display only their interfaces instead of implementing them. - participants -1. Abstractfactory the declares an operation interface that creates an abstract product object. - -2. Concretefactory - implement actions to create specific product objects. + -3. Abstractproduct + declares an interface for a class of product objects. A at4. Concreteproduct - define a Product object that will be created by the corresponding specific factory. - implements the Abstractproduct interface. - -5. Client - use only interfaces declared by the Abstractfactory and Abstractproduct classes in
Test class:
1 Public classTest {2 Public Static voidMain (string[] args) {3Ianimalfactory blackanimalfactory =Newblackanimalfactory ();4ICat Blackcat =Blackanimalfactory.createcat ();5 blackcat.eat ();6Idog Blackdog =Blackanimalfactory.createdog ();7 blackdog.eat ();8 9Ianimalfactory whiteanimalfactory =Newwhiteanimalfactory ();TenICat Whitecat =Whiteanimalfactory.createcat (); One whitecat.eat (); AIdog Whitedog =Whiteanimalfactory.createdog (); - whitedog.eat (); - } the -}
1 Public Interface ianimalfactory {23 ICat createcat (); 4 5 idog createdog (); 6 }
1 Public classBlackanimalfactoryImplementsIanimalfactory {2 3 PublicICat Createcat () {4 return NewBlackcat ();5 }6 7 Publicidog Createdog () {8 return NewBlackdog ();9 }Ten One}
1 Public Interface ICat {23 void eat (); 4 }
1 public class Blackcat implements ICat { 2 public void Eat () { 4 System.out.printl N ("The Black Cat is eating!" 5 6 7 }
1 Public class Implements ICat {23 Public void eat () {4 System.out.println ("The White Cat is eating!" ); 5 }67 }
Java design pattern--creation pattern--abstract factory