(a) Explain the circumstances in which a Java developer might use I. The Composi
ID: 3866898 • Letter: #
Question
(a) Explain the circumstances in which a Java developer might use
I. The Composite Design Pattern
II. The Singleton Design Pattern
III. The Adapter Design Pattern
(b) The Java code below illustrates how a Drawing object may be composed of another Drawing object and Shape objects such as a Circle. Using an appropriate Design Pattern as your guide, write the remaining code that would be required to make this code run to produce the output shown below.
(c) Refactor your code so that the setColour method will throw an exception if it receives a parameter value other than “Green”, “White” or “Gold”. The method should not be case-sensitive either, so “green”, “white” or “gold” should be acceptable. Show how you handle the exception.
public static void main(String args) Shape tri new Triangle(); Shape tri2 = new Triangle(); Shape cir1 = new Circle(); Drawing drawing new Drawing(); Drawing drawing2 new Drawing(); Drawing drawing3 = new Drawing(); drawing3.add(tri2); drawing2.add(tril); drawing2.add drawing3); drawing1.add(cir1); drawingi.add(drawingz); drawing1.setColour(" Green") drawing2.setColour("White) drawing3.setColourC" Gold") drawing1.printO Output Drawing Circle with color Green Drawing Triangle with color White Drawing Triangle with color GoldExplanation / Answer
Ans (a)
Adapter Design Pattern : It works as a bridge between two incompatible interfaces. Whenever the object type are incompatible, to convert from one type to another type adapter design pattern is being used. Adapter design pattern makes both the types compatible to each other.
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interface.
sigleton Design Pattern : when ever there is only one instance is being required at that singleton pattern is used which avoids creating multiple duplicate object in memory.
It provide a global access point to get the instance. Singleton pattern can be used for logging, drivers objects, caching and thread pool.
getInstance() is used to create an instance, it always return the same object everytime it is called.
eg:
Composite Design Pattern : We use composite patter when we need to treat a group of objects in similar way as a single object. the pattern creates a class that contains group of its own objects.
When to use it:
Please ask diffenent question separtely as per Chegg guidelines.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.