Beruflich Dokumente
Kultur Dokumente
What is the design pattern? If a problem occurs over and over again, a solution to that problem has been used effectively. That solution is described as a pattern. The design patterns are languageindependent strategies for solving common object-oriented design problems. When you make a design, you should know the names of some common solutions. Learning design patterns is good for people to communicate each other effectively. In fact, you may have been familiar with some design patterns, you may not use well-known names to describe them. SUN suggests GOF (Gang Of Four--four pioneer guys who wrote a book named "Design Patterns"- Elements of Reusable Object-Oriented Software), so we use that book as our guide to describe solutions. Please make you be familiar with these terms and learn how other people solve the code problems. How many design patterns? Many. A site says at least 250 existing patterns are used in OO world, including Spaghetti which refers to poor coding habits. The 23 design patterns by GOF are well known, and more are to be discovered on the way. Note that the design patterns are not idioms or algorithms or components. What is the relationship among these patterns? Generally, to build a system, you may need many patterns to fit together. Different designer may use different patterns to solve the same problem. Usually:
Some patterns naturally fit together One pattern may lead to another Some patterns are similar and alternative Patterns are discoverable and documentable Patterns are not methods or framework Patterns give you hint to solve a problem effectively
Command is an interface with execute method. It is the core of contract. A client creates an instance of a command implementation and associates it with a receiver. An invoker instructs the command to perform an action. A Command implementations instance creates a binding between the receiver and an action. Receiver is the object that knows the actual steps to perform the action. Command Pattern Discussion
It will be easier to understand all the above with an example. Let us think of developing a universal remote. Our UnversalRemote will have only two buttons, one is to power on and another is to mute all associated ConsumerElectronics. For ConsumerElectronics we will have couple of implementations Television and SoundSystem. Button is a class that is the invoker of an action. OnCommand is used to switch on a ConsumerElectronics. While instantaiting OnCommand client needs to set the receiver. Here receiver is either Television or SoundSystem. UniversalRemote class will maintain all the receivers and will provide a method to identify the currently active receiver. When an instance of a invoker is created, a concrete command is passed to it. Invoker does not know how to perform an action. All it does is honoring the agreement with the Command. Invoker calls the execute() method of the set concrete command.
Command Pattern Example Implementation Command.java package com.javapapers.designpattern.command; public interface Command { public abstract void execute(); } OnCommand.java package com.javapapers.designpattern.command; public class OnCommand implements Command { private ConsumerElectronics ce; public OnCommand(ConsumerElectronics ce) { this.ce = ce; }
Command pattern helps to decouple the invoker and the receiver. Receiver is the one which knows how to perform an action. Command helps to implement call back in java. Helps in terms of extensibility as we can add new command without changing existing code. Command defines the binding between receiver and action. A command should be able to implement undo and redo operations. That is resting the state of the receiver. It can be done from the support of receiver.
Definition The Model/View/Controller(MVC) is an architecture design pattern. Model means data, View means representation and Controller works on data and representation. MVC focuses on decouple the triad relationships among data, representation and controller. Where to use & benefits
Application architecture design. Any data related design, including non-visual application. Decouple complex object to improve maintainability. Increase object reusability. Achieve design flexibility. Related patterns include o Almost all patterns can be used with MVC.
History The Model/View/Controller(MVC) originates from Smalltalk, an OO programming language. Core issue MVC consists of three kind of objects. The Model is an internal representation of the data, the View is the screen presentation of GUI, and the Controller coordinates changes between the Model and View. SCJD project design To achieve the MVC architecture in your project, you have to decouple View and Model by Controller. Your GUI as View should be designed as a module. It can be launched separately. Your data related classes as Model should be treated as a module too. Your controller classes should response to any data change on the GUI. Such design will increase reusability and flexibility.
. Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. is the intent by GoF.
Real World Example In this article, let us take a real world example of part-whole hierarchy and use composite design pattern using java. As a kid, I have spent huge amount of time with lego building blocks. Last week I bought my son an assorted kit lego and we spent the whole weekend together building structures. Let us consider the game of building blocks to practice composite pattern. Assume that our kit has only three unique pieces ( 1, 2 and 4 blocks) and let us call these as primitive blocks as they will be the end nodes in the tree structure. Objective is to build a house and it will be a step by step process. First using primitive blocks, we should construct multiple windows, doors, walls, floor and let us call these structures. Then use all these structure to create a house.
Primitive blocks combined together gives a structure. Multiple structures assembled together gives a house. Important Points
Importance of composite pattern is, the group of objects should be treated similarly as a single object. Manipulating a single object should be as similar to manipulating a group of objects. In sync with our example, we join primitive blocks to create structures and similarly join structures to create house. Recursive formation and tree structure for composite should be noted. Clients access the whole hierarchy through the components and they are not aware about if they are dealing with leaf or composites.
Uml Design for Composite Pattern Component: (structure) 1. Component is at the top of hierarchy. It is an abstraction for the composite. 2. It declares the interface for objects in composition. 3. (optional) defines an interface for accessing a components parent in the recursive structure, and implements it if thats appropriate. Leaf: (primitive blocks) 1. The end nodes of the tree and will not have any child. 2. Defines the behaviour for single objects in the composition Composite: (group) 1. Consists of child components and defines behaviour for them 2. Implements the child related operations.
Composite Pattern Implementation package com.javapapers.designpattern.composite; public class Block implements Group { public void assemble() { System.out.println("Block"); } } package com.javapapers.designpattern.composite; public interface Group { public void assemble(); } package com.javapapers.designpattern.composite; import java.util.ArrayList; import java.util.List; public class Structure implements Group { // Collection of child groups. private List<Group> groups = new ArrayList<Group>(); public void assemble() { for (Group group : groups) { group.assemble(); }
Thank You
ARUMUGA BABU K
EMP ID: 362 102