Previous | Home | Next |
The creational design pattern is the part of the design pattern by help of this pattern we can create the class creation and object creation pattern . the class pattern is use inheritance in instantiation process and the object creation pattern get the job done for this use to delegation. the creational pattern is used to define the best possible way to the instantiate of the object.
There are five type of the creational pattern, which is listed here:
- Abstract Factory Pattern
- Builder Pattern
- Factory Method Pattern
- Singleton Pattern
- Prototype Pattern
Abstract Factory is Provide one level of interface which is higher than the factory pattern. and this is used for return more than one factories.
Use of the Abstract Factory
- Create one ar more families of related or dependent objects.
- It is Provide products of a class library, and exposing to interface not implementation.
- Needs to isolate concrete classes from their super classes.
- A system needs independent of how its product are created, composed, and represented.
- Try to enforce a constraint.
- An alternative to Facade to hide platform-specific classes.
- Easily extensible to a family.
- 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.
- Facade, which is often used with an abstract factory by providing an interface for creating implementing class.
Example
package r4r; class Information {} interface ConnectionFactory { Loc getLocalConnection(); Rem getRemoteConnection(); } interface Loc { Information[] loadDB(String filename); } interface Rem extends Loc{ void connect2WWW(String url); } class LMode implements Loc { public Information[] loadDB(String name) { System.out.print("Loading the data from a local database "); return null; } } class RMode implements Rem { public void connect2WWW(String url) { System.out.println("Connect to a remote database "); } public Information[] loadDB(String name) { System.out.println("Loading data from a remote database "); return null; } } class DManager implements ConnectionFactory { boolean local = false; Information[] data; public Loc getLocalConnection() { return new LMode(); } public Rem getRemoteConnection() { return new RMode(); } publicvoid loadData() { if(local){ Loc conn = getLocalConnection(); data = conn.loadDB("db.db"); }else { Rem conn = getRemoteConnection(); conn.connect2WWW("www.some.where.com"); data = conn.loadDB("db.db"); } } public void setConnection(boolean a) { local = a; } } public class abstractpattern { public static void main(String[] args) { DManager d1 = new DManager(); Information[] in = null; String dbFileName = "db.db"; if (args.length == 0) { d1.setConnection(true); LMode lm = (LMode)d1.getLocalConnection(); in = lm.loadDB(dbFileName); } else { RMode rm = (RMode)d1.getRemoteConnection(); rm.connect2WWW("www.javacamp.org/db/"); in = rm.loadDB(dbFileName); } } }
Previous | Home | Next |