Abstract factory pattern -Abstract Factory

 First, the definition

 It offers to create a series of related or dependent objects interface, without specifying their concrete classes.

Second, the structure

 

Three, Demo

Clothing products

/ ** 
 * clothing 
 * / 
public  interface Clothes { 

    void PrintUsage (); 

} 

/ ** 
 * Jacket 
 * / 
public  class JackClothes the implements Clothes { 
    
    @Override 
    public  void PrintUsage () { 
        System.out.println ( "I am a jacket wear tide I " ); 
    } 
    
} 

/ ** 
 * T-shirt 
 * / 
public  class TShirtClothes the implements Clothes { 

    @Override 
    public  void PrintUsage () { 
        System.out.println ( " I T-shirt, random ");
    }

}

Footwear

/ ** 
 * footwear 
 * / 
public  interface Shoe { 
    
    void describeSelf (); 
} 

/ ** 
 * shoes (particularly footwear) 
 * / 
public  class RunningShoe the implements Shoe { 

    @Override 
    public  void describeSelf () { 
        the System.out .println ( "I running shoes, as their salts with I" ); 
    } 

} 

/ ** 
 * basketball shoes (particularly footwear) 
 * / 
public  class BasketballShoe the implements shoe { 
    
    @Override 
    public  void describeSelf () { 
        the System.out .println ("I'm a basketball shoes, I wear to play basketball hanging burst" );     
    } 
    
}

factory

/ ** 
 * abstract factory 
 * / 
public  interface Factory's { 

    / ** 
     * both the production of footwear 
     * / 
    Shoe produceShoe (as BRAND Brand); 

    / ** 
     * can produce clothing goods 
     * / 
    Clothes produceClothes (as BRAND Brand); 
} 

/ ** 
 * Li Ning factory, production of shoes series, also produces apparel products 
 * / 
public  class LiningFactory the implements factory's { 

    / ** 
     * production of footwear products 
     * / 
    @Override 
    public shoe produceShoe (BRAND Brand) {
         IF (BRAND.BASKETBALL_SHOE == Brand) { 
            System.out.println ( "--- --- Li Ning basketball shoes factory");
             Return  new new BasketballShoe (); 
        } 

        IF (BRAND.RUNNING_SHOE == Brand) { 
            System.out.println ( "--- plant Ning shoes ---" );
             return  new new RunningShoe (); 
        } 

        return  null ; 
    } 

    / ** 
     * production of apparel products 
     * / 
    @Override 
    public Clothes produceClothes (BRAND Brand) {
         IF (BRAND.JACK_CLOTHES == Brand) { 
            System.out.println ( "Li Ning factory jacket --- ---" );
             return  new new  JackClothes ();
        }

        IF (BRAND.TSHIRT_CLOTHES == Brand) { 
            System.out.println ( "T-shirt factory Ning --- ---" );
             return  new new TShirtClothes (); 
        } 

        return  null ; 
    } 
} 

/ ** 
 * the Nike plant, production of footwear products, also produces apparel products 
 * / 
public  class NikeFactory the implements Factory's { 

    / ** 
     * production of shoes products 
     * / 
    @Override 
    public shoe produceShoe (BRAND Brand) {
         IF (BRAND.RUNNING_SHOE == Brand) { 
            System. out.println ("--- the Nike basketball shoe factory ---");
             Return  new new RunningShoe (); 
        } 

        IF (BRAND.BASKETBALL_SHOE == Brand) { 
            System.out.println ( "--- the Nike shoes factory ---" );
             return  new new BasketballShoe (); 
        } 

        return  null ; 
    } 

    / ** 
     * production of apparel products 
     * / 
    @Override 
    public Clothes produceClothes (BRAND Brand) {
         IF (BRAND.JACK_CLOTHES == Brand) { 
            System.out.println ( "--- Nike factory jacket ---" );
             return  new new JackClothes ();
        } 

        IF (BRAND.TSHIRT_CLOTHES == Brand) { 
            System.out.println ( "T-shirt --- the Nike factory ---" );
             return  new new TShirtClothes (); 
        } 

        return  null ; 
    } 

}

Client code

public  class Client { 

    public  static  void main (String [] args) {
         // Nike factory to produce shoes series (basketball shoes, running shoes) and clothing products (jackets, T-shirts) 
        Factory's nikeFactory = new new NikeFactory ();
         // production of footwear products 
        shoe nikeBasketballShoe = nikeFactory.produceShoe (BRAND.BASKETBALL_SHOE); // basketball shoes 
        shoe nikeRunningshoe = nikeFactory.produceShoe (BRAND.RUNNING_SHOE); // running shoes 
        nikeBasketballShoe.describeSelf (); 
        nikeRunningshoe.describeSelf (); 
        // production clothing products 
        Clothes nikeJack = nikeFactory.produceClothes (BRAND.JACK_CLOTHES); //Jacket 
        Clothes nikeTshit = nikeFactory.produceClothes (BRAND.TSHIRT_CLOTHES); // T-Shirts 
        nikeJack.printUsage (); 
        nikeTshit.printUsage (); 

        System.out.println ( "============= =============== " ); 

        // Li Ning factories to produce shoes series (basketball shoes, running shoes) and clothing products (jackets, T-shirts) 
        factory's liningFactory = new new liningFactory () ;
         // production of footwear products 
        shoe liningBasketballShoe = liningFactory.produceShoe (BRAND.BASKETBALL_SHOE); 
        shoe liningRunningShoe = liningFactory.produceShoe (BRAND.RUNNING_SHOE);  
        liningBasketballShoe.describeSelf ();
        liningRunningShoe.describeSelf (); 
        // production of apparel products
        Clothes liningJack = liningFactory.produceClothes(BRAND.JACK_CLOTHES);
        Clothes liningTshirt = liningFactory.produceClothes(BRAND.TSHIRT_CLOTHES);
        liningJack.printUsage();
        liningTshirt.printUsage();
    }
}

 

 

Guess you like

Origin www.cnblogs.com/rouqinglangzi/p/11122316.html