Abstract Factory Pattern

실리콘·2023년 2월 15일
0

Intent

Provide an interface for creating families of related or dependent objects, w/o specifiying their concrete classes.

Also Known As

Kit

Motivation

To enable easy changing of the look, feel of an application. Also clients should stay independent of the prevailing look and feel. WidgetFactory is the topmost abstract class. There will be concrete subclass of WidgetFactory for each look-and-feel standard. ex. MotifWidgetFactory, which returns a Motif scroll bar. PMWidgetFactory, returning a scroll bar for Presentaion Manager.
Clients have no idea of WidgetFactory interface. Only use instance of the concrete subclasses, but adhere to interface defined by the abstract class WidgetFactory.

Applicability

  • A system should be independent of how its products are created, composed, and represented.
  • A system should be configured with one of multiple families of products.
  • A family of related product objects is designed to be used together, and you need to enforce this constraint.
  • You want to provide a class library of products, and want to reveal interfaces only, not implementation

Structure

Participants

  • AbstractFactory (WidgetFactory)
    - declares an interface for operations that create abstract product objects.
  • ConcreteFactory (MotifWidgetFactory, PMWidgetFactory)
    - implements the operations to create concrete product objects.
  • AbstractProduct (Window, ScrollBar)
    - declares an interface for a type of product object.
  • ConcreteProduct (MotifWindow, MotifScrollBar)
    - defines a product object to be created by the corresponding concrete factory.
    - implements the AbstractProduct interface.
  • Client
    - Only use interfaces defined by AbstratFactory and AbstractProduct

Collaborations

  • Normally a single instance of a ConcreteFactory class is created at run-time. Of course. its a factory. Creates one kind of product. If client wants different product, use another factory.
  • AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

Consequences

Benefits & Liabilities
1. It isolates concrete classes. Product class names are isolated in the implementation of the concrete factory. not in client code.
2. It makes exchangin product families easy. The class of concrete factory appears only once in an application, where it's instantiated. So no duplicate code change when changin concrete factory to use.
3. Promotes consistency among products. AbstractFactory enforcement.
4. Supporting new kinds of products is difficult. Fixed interface. What if new product requires extended interface? hard. Lots of code to change. But one possible solution in Implementaion section.

Implementation

Some useful techniques for implementing the Abstrat Factory pattern.
1. Factories as singletons.
2. Creating the products. See Factory Method pattern for each product. This overrides I think the constructor() for product classes?
If many product families are possible, refer to Prototype pattern. A javascript like pattern I think.
Pure Prototype apporach
Concrete factory has protype of a product. Factory clones the prototype, and creates the product. Eliminates the need for a new concrete factory calss for each new product family.
Prototype variant
Possible in languages that treat classes as first-class objects. Define a new factory by initializing an instance of concrete factory with classes of products, not by subclassing. This approach takes advantage of language characteristics, whereas the pure Prototype-based approach is language-independent.
3. Defining extensible factories. AbstractFactory usually defines a different operation for each kind of product it can produce. The kinds of products are encoded in the operation signatures. Adding a new kind of product requires changin the AbstractFactory interface and all the classes that depend on it.

  • A more flexible, but less safe design is to add a parameter to operations that create objects. This parameter specified the kind of object to be created. Can be class identifier, integer, whatever. Then AbstractFactory would only need a single make() method with a parmeter indicating the kind of object to create. This is the technique used in the Prototype- and the class-based abstract factories discuessed earlier.
  • This variation is easier to use in a dynamically typed language (js, python).
  • A inherent problem: All products are returned to the client with the same abstract interface as given by the return type. The client will not be able to differentiate or mkae safe assumptions about the class of a product. If clients need to perform subclass-specific operations, they won't be accessible through the abstract interface. Could do a downcast, but not always feasible or safe, as downcast can fail. This is a classic trade-off for a highly flexible and extensible interface.

Sample Code

in C++ in books.
if you want dynamic language example, there is also SmallTalk example. But you have to know the C++ example first to understand.

Known Uses

Old cases.. Interviews? ET++?

implement using Factory Method, or Prototype
A concrete factory is often a Singleton.

profile
software engineer

31개의 댓글

comment-user-thumbnail
2024년 11월 16일

Great posting, moved in front plus saved as a favorite your web blog. I actually can’t put it off to read simple things extra out of you actually. Bijoux fabriqués au Québec

답글 달기
comment-user-thumbnail
2024년 11월 17일

It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait reading. I am impressed with your work and skill. Thanks. how get my ip address

답글 달기
comment-user-thumbnail
2024년 11월 17일

A beats might be fantastic. You possess numerous especially capable actors. I just aspire most people the right from victory.Best iptv

답글 달기
comment-user-thumbnail
2024년 11월 18일

They can be a pretty decent helpful hints most definitely to make sure you many a new comers to blogosphere, transient and additionally specific information… Regards just for stating this tool. Recommended look at page. iptv smarters pro

답글 달기
comment-user-thumbnail
2024년 11월 18일

I am definitely enjoying your website. You definitely have some great insight and great stories. bathroom rentals las vegas

답글 달기
comment-user-thumbnail
2024년 11월 20일

Document thrilled along with the researching you will designed to makes precise put up impressive. Delightful adventure! iptv uk

답글 달기
comment-user-thumbnail
2024년 11월 21일

The Debox method facilitates emotional healing by encouraging individuals to confront and release pent-up emotions, as outlined at https://debox.Co/debox-digital-book/. Practitioners achieve relief from emotional baggage through specific exercises detailed in our digital book, promoting mental wellness. The method incorporates breathing techniques to help users navigate through emotional turmoil, offering a step-by-step guide for effective emotional release. Success stories, such as users overcoming years of anxiety within weeks, underscore the method's efficacy. Essential oils are recommended as part of the process for their calming properties, enhancing the healing experience. Meditation plays a crucial role in the Debox method, aiding in the detachment from negative emotions. Our digital book provides a structured plan that users have found manageable, dedicating a few minutes daily to the exercises. Feedback highlights the importance of consistency in practice for optimal results, with many reporting significant improvements within a month. The Debox method's integration into daily routines has shown to significantly reduce stress levels, with users typically noticing changes in as little as two weeks. Support is available through our website, offering guidance for those navigating the healing journey with the Debox method. how to heal with Debox Method

답글 달기
comment-user-thumbnail
2024년 11월 25일

Thanks for your time designed for advertisment a great very good guide! I noticed your internet-site ideal for a must have. It has splendid and even effective content articles. Compete the best get the job done! full match replay

답글 달기
comment-user-thumbnail
2024년 11월 26일

In fact dependable, fantastic, fact-filled advice in this case. A items Have let down, and that also clearly is valid in this case to boot. Most people consistently can make for a worthwhile read through. How can you show So i am floored?: )#) Cultivate the nice content pieces. 대부사이트

답글 달기
comment-user-thumbnail
2024년 11월 30일

This specific is a great article My spouse and i witnessed due to talk about the idea. It is definitely precisely what I want to to view expect throughout potential you can proceed pertaining to expressing a real exceptional article. situs slot gacor

답글 달기
comment-user-thumbnail
2024년 12월 2일

You understand your projects stand out of the crowd. There is something unique about them. It seems to me all of them are brilliant. 국외문자

답글 달기
comment-user-thumbnail
2024년 12월 3일

Decent put up, her the most fascinating blog page which are in this case, cultivate monetary management give good results, could be spine. slot88 login

답글 달기
comment-user-thumbnail
2024년 12월 4일

This is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here! 프라그마틱 슬롯 먹튀

답글 달기
comment-user-thumbnail
2024년 12월 5일

This kind of is a wonderful submit My partner and i noticed as a result of discuss that. It is just what I desired to find out desire inside upcoming you may keep on regarding revealing this kind of outstanding submit. alexistogel

답글 달기
comment-user-thumbnail
2024년 12월 7일

Compete the great get the job done, As i browse small amount of content articles in this particular websites and even It looks like that your choice of word wide web web log is without a doubt realistic important and allows gotten forums in fabulous tips. parlay

답글 달기
comment-user-thumbnail
2024년 12월 9일

I got too much interesting stuff on your blog. I guess I am not the only one having all the enjoyment here! Keep up the good work.slot online

답글 달기
comment-user-thumbnail
2024년 12월 11일

Bandar Toto Macau is a well-known gambling platform that offers a wide range of betting options, including sports, casino games, and lottery. Popular in Macau, it attracts both local and international players with its exciting atmosphere and diverse gaming experiences, making it a top choice for entertainment and betting enthusiasts. olxtoto

답글 달기
comment-user-thumbnail
2024년 12월 14일

With thanks to get furnishing recently available posts in connection with the dilemma, I actually look ahead to learn extra. 문자인증대행

답글 달기
comment-user-thumbnail
2024년 12월 14일

Code promo 1xBet 2025 : Utilisez le code — 1X200FOX pour profiter d'une offre VIP de 100% jusqu'à 130€ sur les paris sportifs ou de 1950€ + 150 tours gratuits. Renseignez le code promo lors de la création de votre compte et obtenez un bonus sur les paris sportifs jusqu'à $130. Sur le casino vous pourrez bénéficier d'un bonus allant jusqu'à $1950 + 150 tours gratuits.code promo 1xbet senegal

답글 달기
comment-user-thumbnail
2024년 12월 15일

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. vviavi slot

답글 달기
comment-user-thumbnail
2024년 12월 15일

Apply the 1WinMax500 code for a 500% bonus up to $1025 at 1Win. Receive 70 free spins instantly with no verification needed, plus exciting offers like lotteries and extra free spins every day. 1win casino app

답글 달기
comment-user-thumbnail
2024년 12월 16일

New to 1xBet? Use the 1XBONO25 code and get a 100% bonus up to $130. Start your adventure with extra funds for both sports betting and casino games, making the most of your welcome offer. 1xbet argentina

답글 달기
comment-user-thumbnail
2024년 12월 17일

169Cuan offers a fantastic online gambling experience with sports betting, online slots, and live casino games. Enjoy smooth gameplay and thrilling challenges, with high win potential and rewards on every bet you place. link alternatif 169cuan

답글 달기
comment-user-thumbnail
2024년 12월 17일

Take advantage of Melbet's "MEGA200" promo code for 2025. Get a welcome bonus of up to 200€ based on your first deposit and enjoy an exclusive 130% bonus to maximize your betting potential from the start. code promo melbet cameroun

답글 달기
comment-user-thumbnail
2024년 12월 19일

Frequent goes to here i will discuss the obvious way to appreciate it on your attempt, which often means that Now i am viewing the internet site day-to-day, in search of completely new, useful facts. Quite a few, with thanks! 트립닷컴 항공권 할인코드

답글 달기
comment-user-thumbnail
2024년 12월 22일

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. nx303

답글 달기
comment-user-thumbnail
2024년 12월 23일

This is such a great resource that you are providing and you give it away for free. ยูฟ่าเบท

답글 달기
comment-user-thumbnail
2024년 12월 24일

Many thanks for that publish as well as excellent ideas.. actually We additionally believe that effort is actually the most crucial facet of obtaining achievement. 슬롯사이트

답글 달기
comment-user-thumbnail
6일 전

It's which means that amazing together with inspiring. We really enjoy any designs together with whoever should get it all during the post shall be grinning. colatogel login

답글 달기

Good place, walked into the future together with bookmarked as their favorite your websites. That i can’t procrastinate to read the paper even more with everyone. 부산 출장마사지

답글 달기

When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. slot777

답글 달기