3wPsO Δημοσ. 8 Ιανουαρίου 2015 Δημοσ. 8 Ιανουαρίου 2015 Γεια σας, Ηθελα να ρωτησω γιατι καποιος να χρησιμοποιησει Singleton method , Factory method και Abstract factory ? Ψαχνω αρκετη ωρα στο ιντερνετ την απαντηση αλλα ο καθενας λεει τα δικα του και ακρη δεν εβγαλα( μαλλον ειμαι και χαζος ) αλλα δεν καταλαβαινω γιατι να τα χρησιμοποιησεις..
tr3quart1sta Δημοσ. 8 Ιανουαρίου 2015 Δημοσ. 8 Ιανουαρίου 2015 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. DefinitionProvides 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 Connect parallel class hierarchies. A class wants its subclasses to specify the object. A class cannot anticipate its subclasses, which must be created. A family of objects needs to be separated by using shared interface. The code needs to deal with interface, not implemented classes. Hide concrete classes from the client. Factory methods can be parameterized. The returned object may be either abstract or concrete object. Providing hooks for subclasses is more flexible than creating objects directly. Follow naming conventions to help other developers to recognize the code structure. Related patterns include Abstract Factory , which is a layer higher than a factory method. Template method, which defines a skeleton of an algorithm to defer some steps to subclasses or avoid subclasses Prototype, which creates a new object by copying an instance, so it reduces subclasses. Singleton, which makes a returned factory method unique. 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. DefinitionProvides 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. DefinitionOne instance of a class or one value accessible globally in an application. Where to use & benefits Ensure unique instance by defining class final to prevent cloning. May be extensible by the subclass by defining subclass final. Make a method or a variable public or/and static. Access to the instance by the way you provided. Well control the instantiation of a class. Define one value shared by all instances by making it static. Related patterns include Abstract factory, which is often used to return unique objects. Builder, which is used to construct a complex object, whereas a singleton is used to create a globally accessible object. Prototype, which is used to copy an object, or create an object from its prototype, whereas a singleton is used to ensure that only one prototype is guaranteed. 1
ZAKKWYLDE Δημοσ. 11 Ιανουαρίου 2015 Δημοσ. 11 Ιανουαρίου 2015 Πολύ πολύ χονδρικά. Το 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 κλάση. Αυτά είναι πολύ χονδρικά που σου λέω αλλά ελπίζω να πήρες μιά ιδέα.
3wPsO Δημοσ. 15 Ιανουαρίου 2015 Μέλος Δημοσ. 15 Ιανουαρίου 2015 Πολύ πολύ χονδρικά. Το 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 ?
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα