Design Mode: Abstract Factory Mode

Source: Internet
Author: User

Design Mode: Abstract Factory Mode

 

 

The generation of objects consumes system resources, so how to efficiently produce, manage, and operate objects has always been a topic worth discussing. The Creational model is related to the establishment of objects, the pattern under this classification provides some guiding principles and design directions. The list below all belongs to the Creational model.

  • Simple Factory mode Abstract Factory mode Builder mode Factory Method mode Prototype mode Singleton mode Registry of Singleton Mode Abstract Factory Model

    We talked about the simple factory model above, where we divide movies into action movies and love movies, so there are cheap and expensive movies. At this time, the audience asked me to watch only cheap action movies, some watch expensive movies, so taking photos of our simple factory is not enough.

    What should we do? The audience requirements become more complex, so we only need to make the factory more complex. The IFoxFilmFactory is divided into two departments: the Department that produces expensive movies (ExpensiveFilmFactory ), department that produces cheap movies (economy filmfactory ). in this case, the classification of movies is also complicated. action movies (IActionMovie) have ExpensiveActionMovie, while economic actionmovie and ILoveMovie have ExpensiveLoveMovie ), there are cheap (economical lovemovie ).

    The following class diagram

    IActionMovie interface-action movies

     

    public interface IActionMovie {    public void playAction() ;}

     

    ILoveMovie interface-Love Film

    public interface ILoveMovie {    public void playLove() ;}

     

    ExpensiveActionMovie-expensive action movies

    Public class ExpensiveActionMovie implements IActionMovie {@ Override public void playAction () {System. out. println (expensive-Hum haha !); }}

     

    Economical actionmovie-cheap action movies

    Public class economy micactionmovie implements IActionMovie {@ Override public void playAction () {System. out. println (cheap-Hum haha !); }}

     

    ExpensiveLoveMovie class-expensive love movies

    Public class ExpensiveLoveMovie implements ILoveMovie {@ Override public void playLove () {System. out. println (expensive-love Tiger Oil !); }}

     

    Economic miclovemovie-cheap love films

    Public class economical miclovemovie implements ILoveMovie {@ Override public void playLove () {System. out. println (cheap-love Tiger Oil !); }}

     

    IFoxFilmFactory interface -- total factory of Fox

    public interface IFoxFilmFactory {    public IActionMovie getActionMovie() ;    public ILoveMovie getLoveMovie() ;}

     

    ExpensiveFilmFactory class-Department for producing expensive movies

    public class ExpensiveFilmFactory implements IFoxFilmFactory {    @Override    public IActionMovie getActionMovie() {        return new ExpensiveActionMovie();    }    @Override    public ILoveMovie getLoveMovie() {        return new ExpensiveLoveMovie();    }}

     

    Economy filmfactory-Department for producing cheap movies

    public class EconomicFilmFactory implements IFoxFilmFactory {    @Override    public IActionMovie getActionMovie() {        return new EconomicActionMovie();    }    @Override    public ILoveMovie getLoveMovie() {        return new EconomicLoveMovie();    }}

     

    AudienceClient class-audience

    Public class AudienceClient {public static void main (String [] args) {IFoxFilmFactory fox1 = new Economy micfilmfactory (); fox1.getActionMovie (). playAction (); fox1.getLoveMovie (). playLove (); IFoxFilmFactory fox2 = new ExpensiveFilmFactory (); fox2.getActionMovie (). playAction (); fox2.getLoveMovie (). playLove ();/* print * cheap-Hum haha! * Cheap-love tiger oil! * Expensive-hum, haha! * Expensive-love tiger oil! */}}

     

    16:50:20

    Brave, Happy, Thanksgiving!


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.