Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

Δημοσ.

Γεια σας, 

 

Ηθελα να ρωτησω γιατι καποιος να χρησιμοποιησει Singleton method , Factory method και Abstract factory ? Ψαχνω αρκετη ωρα στο ιντερνετ την απαντηση αλλα ο καθενας λεει τα δικα του και ακρη δεν εβγαλα( μαλλον ειμαι και χαζος ) αλλα δεν καταλαβαινω γιατι να τα χρησιμοποιησεις..

 

 

Δημοσ.

Factory pattern is one of most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

 

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

 

Definition

Provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be instantiated or called, based on the conditions or parameters given.

Where to use & benefits

 

 

 

Abstract Factory patterns works around a super-factory which creates other factories. This factory is also called as Factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

 

In Abstract Factory pattern an interface is responsible for creating a factory of related objects, without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.

 

Definition

Provides one level of interface higher than the factory pattern. It is used to return one of several factories.

Where to use & benefits
  • Creates families of related or dependent objects like Kit.
  • Provides a class library of products, exposing interface not implementation.
  • Needs to isolate concrete classes from their super classes.
  • A system needs independent of how its products are created, composed, and represented.
  • Try to enforce a constraint.
  • An alternative to Facade to hide platform-specific classes
  • Easily extensible to a system or a family
  • Related patterns include
    • Factory method, which is often implemented with an abstract factory.
    • Singleton, which is often implemented with an abstract factory.
    • Prototype, which is often implemented with an abstract factory.
    • façade, which is often used with an abstract factory by providing an interface for creating implementing class.

 

 

 

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best way to create an object.

 

This pattern involves a single class which is responsible to creates own object while making sure that only single object get created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

 

Definition

One instance of a class or one value accessible globally in an application.

Where to use & benefits

  • Like 1
Δημοσ.

Πολύ πολύ χονδρικά. 

 

Το Singleton είναι όταν θέλουμε να έχουμε μόνο μία instance ενός Object. Για παράδειγμα θέλεις να γράφεις ένα Log file, δεν υπάρχει λόγος κάθε φορά να κάνεις ένα new Object().

 

Το Factory method είναι απλά μια οποιαδήποτε μέθοδος η οποία χρησιμοποιείται για να κατασκευάσει ένα Object. Κάτι ανάλογο του Constructor μόνο που μια factory method μπορεί να την έχεις σε διαφορετική κλάση (μαζί με άλλα διάφορα πλεονεκτήματα).

 

Τέλος το Abstract Factory ουσιαστικά μας δίνει τη δυνατότα να επιλέγουμε το Implementation της κάθε φορά. Π.χ Θέλουμε έναν Parser ο οποίος να κάνει parse xml, html ή json. Οπότε έχεις τρία διαφορετικά implementations. Οπότε μπορείς να κάνεις:

ParserFactory factory = new XmlParserFactory();
Parser parser1 = factory.createParser();

factory = new JsonParserFactory();
Parser parser2 = factory.createParser();

factory = new HtmlParserFactory();
Parser parser3 = factory.createParser();

H abstract factory είναι η ParserFactory. Μεθαύριο αποφασίζω να κάνω μια WhateverFormatParserFactory(); και θα μπορώ να τη διαχειρίζομαι απο τη Abstract κλάση.

 

Αυτά είναι πολύ χονδρικά που σου λέω αλλά ελπίζω να πήρες μιά ιδέα.

Δημοσ.

Πολύ πολύ χονδρικά. 

 

Το Singleton είναι όταν θέλουμε να έχουμε μόνο μία instance ενός Object. Για παράδειγμα θέλεις να γράφεις ένα Log file, δεν υπάρχει λόγος κάθε φορά να κάνεις ένα new Object().

 

Το Factory method είναι απλά μια οποιαδήποτε μέθοδος η οποία χρησιμοποιείται για να κατασκευάσει ένα Object. Κάτι ανάλογο του Constructor μόνο που μια factory method μπορεί να την έχεις σε διαφορετική κλάση (μαζί με άλλα διάφορα πλεονεκτήματα).

 

Τέλος το Abstract Factory ουσιαστικά μας δίνει τη δυνατότα να επιλέγουμε το Implementation της κάθε φορά. Π.χ Θέλουμε έναν Parser ο οποίος να κάνει parse xml, html ή json. Οπότε έχεις τρία διαφορετικά implementations. Οπότε μπορείς να κάνεις:

ParserFactory factory = new XmlParserFactory();
Parser parser1 = factory.createParser();

factory = new JsonParserFactory();
Parser parser2 = factory.createParser();

factory = new HtmlParserFactory();
Parser parser3 = factory.createParser();

H abstract factory είναι η ParserFactory. Μεθαύριο αποφασίζω να κάνω μια WhateverFormatParserFactory(); και θα μπορώ να τη διαχειρίζομαι απο τη Abstract κλάση.

 

Αυτά είναι πολύ χονδρικά που σου λέω αλλά ελπίζω να πήρες μιά ιδέα.

Μηπως εχεις κανενα real life παραδειγμα για το singleton και το factory method ? 

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...